`
chenchangqun
  • 浏览: 56069 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

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进行注入,这时事务才起作用。

到这里问题应该解决大部分,剩下的疑问只能留给以后,看源代码的时候了


















分享到:
评论

相关推荐

    Spring+SpringMVC配置事务管理无效原因及解决办法详解

    Spring+SpringMVC配置事务管理无效原因及解决办法详解 Spring 框架是 Java 企业级应用程序的核心框架,提供了事务管理机制,以确保业务逻辑的原子性和一致性。然而,在使用 Spring 和 SpringMVC 配置事务管理时,...

    集成springmvc spring hibernate的配置

    5. **事务管理器配置**:使用`@Transactional`注解进行声明式事务管理,配置事务管理器`txManager`: ```xml &lt;tx:annotation-driven transaction-manager="txManager" /&gt; ``` 接下来,配置Spring MVC的`...

    springMVC技术概述

    配置使用注解的Handler和Service等等使用&lt;context:component-scan&gt; 不过springBoot已经省略了这些配置 常用注解:@Controller @RestController(Controller+ResponseBody) @Service @Transactional @Mapper @...

    SpringMVC------从HelloWorld开始

    `&lt;context:component-scan&gt;`扫描指定包下的所有类,找到带有`@Controller`等注解的类。`&lt;mvc:annotation-driven/&gt;`开启对注解的支持,使得我们可以使用`@RequestMapping`等注解。 然后,为了运行我们的应用,我们...

    SpringMVC+Hibernate实例

    &lt;context:component-scan base-package="com.bbs"/&gt; &lt;!--注解支持--&gt; &lt;mvc:annotation-driven/&gt; &lt;!--视图解析--&gt; ...

    SpringMVC和Spring的配置文件扫描包详解

    &lt;context:component-scan&gt; 标签就是这样的一种配置方式,它可以自动扫描指定包下的 Java 文件,如果扫描到有 @Component、@Controller、@Service 等这些注解的类,则把这些类注册为 Bean。 &lt;context:component-scan...

    springmvc mybatis整合

    - 引入Spring的JDBC和事务管理器:添加`&lt;context:component-scan&gt;`标签来扫描我们的bean,包括DAO接口和实现。 - 配置数据源:使用`&lt;bean&gt;`定义数据源,如`com.zaxxer.hikari.HikariConfig`和`...

    spring

    context:component-scan:作用是可以使用@ Component,@ Controller,@ Service等等来省略xml配置文件中的bean元素,简化配置 context:component-scan是上下文:annotation-config的超集,配置了前者则不需要配置...

    springmvc注解

    3. **方式三:使用&lt;context:component-scan/&gt;进行组件扫描** ```xml &lt;context:component-scan base-package="com.example.package"/&gt; ``` 通过这种方式,可以指定需要扫描的包路径,并且可以进一步配置过滤...

    SPRING MVC3.2案例讲解---配置

    在上述配置中,`context:component-scan`标签用于扫描指定包下的所有带有@Controller注解的类,使Spring能够自动管理这些类。`InternalResourceViewResolver`是视图解析器,它定义了视图文件的前缀和后缀。`mvc:...

    SpringMVC核心配置文件示例.rar

    在`&lt;context:component-scan&gt;`标签中,你可以指定要扫描的包,以便Spring自动发现并管理Bean。例如: ```xml &lt;context:component-scan base-package="com.example.web" /&gt; ``` 这会告诉Spring在`...

    ssm 框架配置

    &lt;/context:component-scan&gt; ``` 这里配置了对`Controller`、`Service`和`Repository`注解的扫描,从而实现了对相应组件的自动装配。 #### 三、数据类型转换配置 在SSM框架中,数据类型转换主要是为了将前端传来的...

    (代码)SpringMVC第1讲:HelloWorld

    &lt;context:component-scan base-package="com.yourpackage"/&gt; &lt;mvc:annotation-driven/&gt; &lt;property name="prefix" value="/WEB-INF/views/"/&gt; ``` 配置完成后,我们需要创建一个处理器类,即Controller。...

    spring springmvc mybatis配置文件

    它定义了Bean的生命周期和依赖关系,包括数据源、事务管理器、SpringMVC的DispatcherServlet配置以及MyBatis的SqlSessionFactory。例如,你可以看到如下配置: ```xml &lt;!-- 数据库连接配置 --&gt; &lt;!-- ...

    SpringMVC第一天.pdf

    但是,通常我们会手动配置 `&lt;init-param&gt;` 来指定SpringMVC的配置文件路径。例如: ```xml &lt;servlet-name&gt;jqk&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-...

    springmvcwendang

    &lt;context:component-scan base-package="com.createnets.springmvc.web" /&gt; (2)新建一个Student类 用于测试注解 (3)配置注解 @Controller @RequestMapping("/list") (3)配置视图名称 &lt;!-- 配置视图名称 -...

    SpringMVC+MyBatis声明式事务管理

    总结起来,SpringMVC与MyBatis结合的项目中,声明式事务管理通过Spring的`DataSourceTransactionManager`、注解@Transactional以及正确配置的组件扫描,实现了事务的自动化管理,极大地简化了事务处理的工作,同时也...

    SpringMVC入门

    &lt;context:component-scan base-package="test.SpringMVC"/&gt; &lt;!-- 不处理静态资源 --&gt; &lt;mvc:default-servlet-handler/&gt; &lt;!-- 如果使用注解,则必须配置如下设置 --&gt; &lt;mvc:annotation-driven/&gt; &lt;!-- 配置...

    Spring MVC 注解自动扫描失效原因分析

    在原始配置中,仅包含了`&lt;context:component-scan&gt;`元素,虽然指定了扫描@Controller注解,但没有明确指出。因此,Spring可能会忽略这些控制器类。修复这个问题的方法是添加一个`&lt;context:include-filter&gt;`子元素,...

    springMVC.doc

    - `&lt;context:component-scan&gt;` 指定包扫描范围,找到带有 @Controller 注解的类。 例如: ```xml &lt;context:component-scan base-package="com.example.web.controllers" /&gt; &lt;mvc:annotation-driven /&gt; &lt;!-- ...

Global site tag (gtag.js) - Google Analytics