【问题标题】:Spring "The prefix "tx" for element "tx:annotation-driven" is not bound."Spring“元素“tx:annotation-driven”的前缀“tx”未绑定。”
【发布时间】:2012-05-09 12:50:40
【问题描述】:

我在“tx:annotation-driven”行中收到上述错误,但我已在 beans 文件的顶部声明了命名空间,为什么以下 XML 会导致此错误?

    <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />


    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <beans:property name="url" value="jdbc:mysql://localhost/assessme" />
        <beans:property name="username" value="assessme" />
        <beans:property name="password" value="assessme" />
    </beans:bean>

     <beans:bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>org.assessme.com.entity.User</beans:value>
        </beans:list>
    </beans:property>
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="packagesToScan" value="org.assessme.com.controller.entity" />
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
                <beans:prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
                <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

    <context:component-scan base-package="org.assessme.com." />

    <beans:bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <beans:property name="mediaTypes">
            <beans:map>
                <beans:entry key="html" value="text/html" />
                <beans:entry key="json" value="application/json" />
            </beans:map>
        </beans:property>
        <beans:property name="defaultViews">
            <beans:list>
                <beans:bean
                    class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                    <beans:property name="prefixJson" value="true" />
                </beans:bean>
            </beans:list>
        </beans:property>
    </beans:bean>
<beans:bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="sessionFactory" />
    </beans:bean>

</beans:beans>

【问题讨论】:

    标签: java xml spring


    【解决方案1】:

    你可以使用这些:

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    

    【讨论】:

      【解决方案2】:

      对于前一个解决方案无济于事,尝试将其放在顶部确保您不会忘记任何 xsi:schemaLocation

      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx.xsd"></beans>
      

      【讨论】:

        【解决方案3】:

        您可以查看here 了解更多信息:

        tx 标签处理在 Spring 中配置所有这些 bean 对交易的全面支持。这些标签包含在 标题为第 9 章,事务管理的章节。

        ...

        为了完整起见,要使用 tx 模式中的标签,您 需要在 Spring XML 的顶部有以下序言 配置文件;以下sn-p中加粗的文字 引用正确的模式,以便 tx 命名空间中的标签是 可供您使用。

        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:aop="http://www.springframework.org/schema/aop"
               xmlns:tx="http://www.springframework.org/schema/tx"
               xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
        
           <!-- <bean> definitions goes here -->
        
        </beans>
        

        【讨论】:

          【解决方案4】:

          你必须在你的项目中引入注解tx,你必须安装它们,不要使用xmlns,它不能正常工作,我看到你用注解开发,它的好习惯并使用它,

          http://www.springframework.org/schema/tx

          http://www.springframework.org/schema/tx/spring-tx.xsd

          【讨论】:

            【解决方案5】:

            就像您的其他 xmlns: 定义一样,您需要一个用于 xmlns:tx 的定义

            xmlns:tx="http://www.springframework.org/schema/tx"
            

            【讨论】:

            • 太棒了 - 谢谢,这有点奇怪,但添加后,现在我的控制器中的 MVC @requestmapping 都不起作用,我的应用程序只有 404,因为 dispatcherservlet 说它不能映射任何东西。你有什么想法吗?
            • 我不太了解 spring webframework,但很可能是因为我不认为 xmlns:tx 是您问题的根源。
              可能是您忘记包含 spring 事务+ WAR 中的 AOP 库。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-02-25
            • 2013-01-14
            • 2015-01-29
            • 2011-08-10
            • 2012-10-03
            • 1970-01-01
            相关资源
            最近更新 更多