springMVC 事务无效,context:component-scan 配置
用springMVC做项目,做到一半发现事务不好用,这可是大问题,苦苦google,终于解决,贴出来与大家分享。
本小猿得出的结论都是实验得来,应该是正确的,如果想得到更直接,更准确的答案,请去研究官方文档。
事务不好用前的代码
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>/pad</display-name> <!-- Spring ApplicationContext配置文件的路径�,可使用通配符,多个路径用�1,号分隔 此参数用于后面的Spring-Context loader --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/*.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/log4j.xml</param-value> </context-param> <context-param> <param-name>chekPower</param-name> <param-value>true</param-value> </context-param> <!-- Character Encoding filter --> <filter> <filter-name>CharacterEncodingFilter</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> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>pad</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>pad</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> <!-- Error Page定义 --> <error-page> <error-code>500</error-code> <location>/commons/error.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/commons/404.jsp</location> </error-page> <error-page> <error-code>403</error-code> <location>/commons/403.jsp</location> </error-page> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
pad-servlet.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" default-autowire="byName"> <context:annotation-config/> <context:component-scan base-package="com.zkhg.pad"> </context:component-scan> <mvc:annotation-driven /> <mvc:interceptors> <bean class="com.zkhg.pad.common.project.PadInterceptor" /> </mvc:interceptors> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"/> </bean> </beans>
applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="WEB-INF/datasource-config.properties" /> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${database.driver}" /> <property name="url" value="${database.url}" /> <property name="username" value="${database.username}" /> <property name="password" value="${database.password}" /> <!-- <property name="defaultAutoCommit" value="false" /> --> <property name="maxActive" value="${database.maxActive}" /> <property name="initialSize" value="${database.initialSize}" /> <property name="maxWait" value="${database.maxWait}" /> <property name="maxIdle" value="${database.maxIdle}" /> <property name="minIdle" value="${database.minIdle}" /> <property name="removeAbandoned" value="${database.removeAbandoned}" /> <property name="removeAbandonedTimeout" value="${database.removeAbandonedTimeout}" /> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:sqlMapConfig.xml</value> </property> <property name="mappingLocations"> <value>classpath*:com/zkhg/pad/sqlMap/*/*.xml</value> </property> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <tx:advice id="serviceAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="remove*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="edit*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="is*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="adjust*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="execute*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="servicePointcut" expression="execution(* com.zkhg.pad.service..*.*(..))" /> <aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice" /> </aop:config> </beans>
问题解决后的代码
web.xml没有改变
pad-servlet.xml(beans的头与上面代码一样):
[color=red] <context:component-scan base-package="com.zkhg.pad.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <mvc:annotation-driven /> <mvc:interceptors>[/color] <bean class="com.zkhg.pad.common.project.PadInterceptor" /> </mvc:interceptors> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"/> </bean>
applicationContext.xml(beans的头与上面代码一样):
[color=red] <context:component-scan base-package="com.zkhg.pad.dao" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> </context:component-scan> <context:component-scan base-package="com.zkhg.pad.service" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> <bean id="placeholderConfig" [/color]class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="WEB-INF/datasource-config.properties" /> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${database.driver}" /> <property name="url" value="${database.url}" /> <property name="username" value="${database.username}" /> <property name="password" value="${database.password}" /> <!-- <property name="defaultAutoCommit" value="false" /> --> <property name="maxActive" value="${database.maxActive}" /> <property name="initialSize" value="${database.initialSize}" /> <property name="maxWait" value="${database.maxWait}" /> <property name="maxIdle" value="${database.maxIdle}" /> <property name="minIdle" value="${database.minIdle}" /> <property name="removeAbandoned" value="${database.removeAbandoned}" /> <property name="removeAbandonedTimeout" value="${database.removeAbandonedTimeout}" /> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:sqlMapConfig.xml</value> </property> <property name="mappingLocations"> <value>classpath*:com/zkhg/pad/sqlMap/*/*.xml</value> </property> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <tx:advice id="serviceAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="remove*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="edit*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="is*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="adjust*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="execute*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="servicePointcut" expression="execution(* com.zkhg.pad.service..*.*(..))" /> <aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice" /> </aop:config>
注意红色的部分。
下面讲讲我解决的过程
首先在下面的文章找到了解决方法
(1)http://www.open-open.com/lib/view/1344438531093
但是我启动时发现,启动时间变长了,于是乎继续查询原因
我注意到可能是context:component-scan这个标签的问题
在http://jinnianshilongnian.iteye.com/blog/1762632中找到了相应的讲解
经过我认真的考虑和实验得出了结论
结论:默认扫描指定包下的全部@Component,exclude-filter指定的不扫描,include-filter指定的扫描,include-filter和exclude-filter没有指定的仍然扫描。
use-default-filters="true"时,include-filter无效。use-default-filters="false"时,exclude-filter无效。
原来是重复的扫描了包,导致启动变慢。
看到<context:annotation-config/>我就觉得别扭,想弄清它是干嘛的
于是查到http://mushiqianmeng.blog.51cto.com/3970029/723880
去掉了<context:annotation-config/>配置文件更清爽了,而且启动加快。
问题虽然解决,但是为什么事务会不好用,bug的原理是什么?
(1)中提到可能是servlet.xml和applicationContext.xml加载的顺序问题,没有深入的探讨。
我感觉是servlet和listener的启动,和servlet的依赖注入问题。
查到启动顺序listener>servlet
而context:component-scan就是扫描指定的包并创建实例,执行依赖注入。
而事务是配置在applicationContext.xml中即在listener作用域中
如果在pad-servlet中扫描了service包,这个包是在servlet中被创建的,servlet.xml是没有配置事务的。所以事务不起作用。
servlet在执行注入的时候,会拿到listener中的配置好事务的bean进行注入,这时事务才起作用。
到这里问题应该解决大部分,剩下的疑问只能留给以后,看源代码的时候了