为啥spring插件没有tx的命名空间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为啥spring插件没有tx的命名空间相关的知识,希望对你有一定的参考价值。
参考技术A 在使用spring时,对于一些功能的配置可以通过Spring提供的XML命名空间进行配置,也可以通过提供的注解进行配置。以事务管理为例
@EnableTransactionManagement注解启用了事务管理功能。
[java] view plain copy
@Configuration
@EnableTransactionManagement
public class AppConfig
@Bean
public FooRepository fooRepository()
// configure and return a class having @Transactional methods
return new JdbcFooRepository(dataSource());
@Bean
public DataSource dataSource()
// configure and return the necessary JDBC DataSource
@Bean
public PlatformTransactionManager txManager()
return new DataSourceTransactionManager(dataSource());
上面的代码与以下的XML配置是等效的:
[html] view plain copy
<beans>
<tx:annotation-driven/>
<bean id="fooRepository" class="com.foo.JdbcFooRepository">
<constructor-arg ref="dataSource"/>
</bean>
<bean id="dataSource" class="com.vendor.VendorDataSource"/>
<bean id="transactionManager" class="org.sfwk...DataSourceTransactionManager">
<constructor-arg ref="dataSource"/>
</bean>
</beans>本回答被提问者采纳
Spring“前缀”tx“for element”tx:annotation-driven“未绑定。”
我在“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>
答案
就像你的其他xmlns:definations一样,你需要一个xmlns:tx
xmlns:tx="http://www.springframework.org/schema/tx"
另一答案
您可以查看here以获取更多信息:
tx标签处理Spring对事务的全面支持中所有这些bean的配置。这些标签在第9章“事务管理”一章中介绍。
...
为了完整性,要在tx模式中使用标记,您需要在Spring XML配置文件的顶部有以下前导码;以下代码段中的emboldened文本引用了正确的架构,以便您可以使用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>
另一答案
你必须在你的项目中引入注释tx
,你必须安装它们,不要使用xmlns
,它不能正常工作,我看到你用注释开发,它的良好实践并使用它,
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
另一答案
对于前一个解决方案没有帮助尝试这个把它放在顶部确保你不要忘记任何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>
以上是关于为啥spring插件没有tx的命名空间的主要内容,如果未能解决你的问题,请参考以下文章