开发者社区> 问答> 正文

spring MVC 加入<tx:annotation-driven&g?报错

在做事务处理时,用注解的方式配置事务,增加下面标签就报错,注释掉启动就一切正常了。。。

<tx:annotation-driven transaction-manager="transactionManagerMySQL"/>

报错信息

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/springMVC-servlet.xml]; nested exception is java.lang.NoSuchMethodError: org.springframework.aop.config.AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(Lorg/springframework/beans/factory/xml/ParserContext;Ljava/lang/Object;)V
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:414)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
	at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
	at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
	at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:605)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:509)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4729)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodError: org.springframework.aop.config.AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(Lorg/springframework/beans/factory/xml/ParserContext;Ljava/lang/Object;)V
	at org.springframework.transaction.config.AnnotationDrivenBeanDefinitionParser.parseInternal(AnnotationDrivenBeanDefinitionParser.java:54)
	at org.springframework.beans.factory.xml.AbstractBeanDefinitionParser.parse(AbstractBeanDefinitionParser.java:61)
	at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1411)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1401)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:168)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:138)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)
	... 22 more

五月 21, 2016 11:38:44 下午 org.apache.catalina.core.StandardContext startInternal
严重: One or more listeners failed to start. Full details will be found in the appropriate container log file
五月 21, 2016 11:38:44 下午 org.apache.catalina.core.StandardContext startInternal
严重: Context [/phone-web] startup failed due to previous errors
五月 21, 2016 11:38:44 下午 org.apache.catalina.core.ApplicationContext log
信息: Closing Spring root WebApplicationContext
五月 21, 2016 11:38:44 下午 org.apache.catalina.core.StandardContext listenerStop
严重: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
	at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:170)
	at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1000)
	at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:976)
	at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:928)
	at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:583)
	at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:116)
	at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4776)
	at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5390)
	at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)



WEB.XML

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>电子产品管理系统</display-name>
	<description>电子产品增删改查等。</description>


	<!-- 加载全局的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/springMVC-servlet.xml
			classpath:org/phone/manager/resource/spring-hibernate-datasources.xml
		</param-value>
	</context-param>

	<!--Start 设置servlet编码 -->
	<filter>
		<filter-name>setCharacterEncoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<!--end 设置servlet编码 -->

	<!--Start filter-mapping -->
	<filter-mapping>
		<filter-name>setCharacterEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!--end filter-mapping -->

	<!-- 用于加载除*-servlet.xml以外的配置文件,具体路径在context-param中contextConfigLocation里配置,默认加载WEB-INF/applicationContext.xml -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- DispatcherServlet -->
	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- 访问静态资源 -->
<!-- <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/resource/*</url-pattern> 
		</servlet-mapping> -->

	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>/index</welcome-file>
	</welcome-file-list>
</web-app>

DispatcherServlet

<?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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

	<!-- controller必须在这里扫描 -->
	<context:component-scan base-package="org.phone.web.*" />

	<!-- <tx:annotation-driven transaction-manager="transactionManagerMySQL" /> -->

	<!-- 完成JSR-303校验支持、信息转换、对域格式化的支持等功能 -->
	<mvc:annotation-driven />

	<!-- 处理对静态资源的请求 -->
	<mvc:resources location="/resource" mapping="**/resource/**" />

	<!-- 如果被代理的目标实现了至少一个接口,则会使用JDK动态代理,所有该目标类型实现的接口都将被代理。 -->
	<!-- 如果用JDK动态代理,就必须为被代理的目标实现一个接口(要注意的地方是:需要将ctx.getBean()方法的返回值用接口类型接收); -->
	<!-- 如果使用CGLIB强制代理,就必选事先将CGLIB包导入项目, 设置beanNameAutoProxyCreator的proxyTargetClass属性为true。 -->
	<aop:aspectj-autoproxy proxy-target-class="true" />

	<!--start 解析Tiles视图,Tiles能将页面分成片段并在运行时组装成完成的页面 -->
	<bean class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />

	<bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
		<property name="definitions">
			<list>
				<value>/WEB-INF/views/**/views.xml</value>
			</list>
		</property>
	</bean>

	<!-- 当Tiles未找到相关视图时,使用些视图解析 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>

</beans>


spring-hibernate-datasource.xml

<?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:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

	<!-- 如果被代理的目标实现了至少一个接口,则会使用JDK动态代理,所有该目标类型实现的接口都将被代理。 -->
	<!-- 如果用JDK动态代理,就必须为被代理的目标实现一个接口(要注意的地方是:需要将ctx.getBean()方法的返回值用接口类型接收); -->
	<!-- 如果使用CGLIB强制代理,就必选事先将CGLIB包导入项目, 设置beanNameAutoProxyCreator的proxyTargetClass属性为true。 -->
	<aop:aspectj-autoproxy proxy-target-class="true" />

	<!-- 扫描bean -->
	<context:component-scan base-package="org.phone.manager.*" />

	<!-- 配置spring异常转换功能 -->
	<bean
		class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

	<!-- 加载classpath中的配置文件,从中获取连接数据库的参数 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:org/phone/manager/resource/jdbc.properties
				</value>
			</list>
		</property>
	</bean>

	<!--start 配置DATASOURCES -->
	<bean id="dataSourceMySQL" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<!-- <property name="name" value="${mysql.driverClassName}" /> 可根据URL自动判断出DBDRIVER -->
		<property name="url" value="${mysql.dailyreport_url}" />
		<property name="username" value="${mysql.username}" />
		<property name="password" value="${mysql.password}" />

		<property name="maxActive" value="${druid.max_size}" />
		<property name="filters" value="config" />
		<!-- 配置数据库加密 -->
		<property name="connectionProperties"
			value="config.decrypt=true;config.decrypt.key=${mysql.publickey}" />
	</bean>
	<!--end 配置DATASOURCES -->

	<!--start 配置SessionFactory -->
	<bean id="sessionFactoryMySQL"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSourceMySQL"></property>
		<!-- 注解的实体类通过此扫描的方式注入 -->
		<property name="packagesToScan" value="org.phone.manager.*" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<!-- hibernate.current_session_context_class此处与Spring2.x不同,不能为thread,否则报错:?? 
					is not valid without active transaction -->
				<prop key="hibernate.current_session_context_class">
					org.springframework.orm.hibernate5.SpringSessionContext
				</prop>
			</props>
		</property>
	</bean>
	<!--end 配置SessionFactory -->

	<!--Start 声明式事务 -->
	<bean id="transactionManagerMySQL"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactoryMySQL" />
	</bean>

	<!-- 注解式扫描 -->
	<tx:annotation-driven transaction-manager="transactionManagerMySQL"/>

	<!-- <aop:config> <aop:pointcut expression="execution(* org.phone.manager.dao.*.*(..))" 
		id="mysqlPointCut" /> <aop:advisor pointcut-ref="mysqlPointCut" advice-ref="txAdviceMySQL" 
		/> </aop:config> <tx:advice id="txAdviceMySQL" transaction-manager="transactionManagerMySQL"> 
		<tx:attributes> <tx:method name="*Entity" propagation="REQUIRED" /> <tx:method 
		name="get*" propagation="REQUIRED" /> <tx:method name="show*" propagation="REQUIRED" 
		/> <tx:method name="*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> 
		</tx:advice> -->
	<!--Start 声明式事务 -->
</beans>


展开
收起
爱吃鱼的程序员 2020-06-09 14:29:00 906 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    解决了。。

    包冲突了。

    http://stackoverflow.com/questions/25616778/transactionmanager-spring-4-hibernate-4-nosuchmethoderror

    删除掉 spring-dao包就可以了

    2020-06-09 14:29:16
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
workshop专场-微服务专场-开发者动手实践营-微服务-Spring Cloud Alibaba 微服务全家桶体验 立即下载
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载

相关实验场景

更多