第一步:
修改SpringMVC-servlet.xml中
删除下面这句话
<!-- 注册所有包 -->
<context:component-scan base-package="com.hs.control,com.hs.util,com.hs.daoImpl,com.hs.serviceImpl" />
加入下面这句话
<!-- 改为只注册控制层的包 -->
<context:component-scan base-package="com.hs"
use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
第二步:
修改application.xml
<!-- 注册除了控制层以外所有包 -->
<context:component-scan base-package="com.hs">
<context:exclude-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
<!-- 创建事务处理管理对象 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 声明使用注解式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
声明式 事务 (推荐)
第一步:
修改SpringMVC-servlet.xml中
删除下面这句话
<!-- 注册所有包 -->
<context:component-scan base-package="com.hs.control,com.hs.util,com.hs.daoImpl,com.hs.serviceImpl" />
加入下面这句话
<!-- 改为只注册控制层的包 -->
<context:component-scan base-package="com.hs"
use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
第二步:
修改applicationContext.xml
xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
<!-- 注册除了控制层以外所有包 -->
<context:component-scan base-package="com.hs">
<context:exclude-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
<!-- 创建事务处理管理对象 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 定义监听事务规则
所有以insert,update,delete开头的方法都会自动进行事务处理
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 定义事务机制作用于哪些类
目前作用于 com.hs.serviceImpl包下所有类的所有方法
总和上面的配置信息,
系统会给 com.hs.serviceImpl包下,所有类中,方法名
以insert,update,delete开头的方法,都自动进行事务处理。
-->
<aop:config>
<aop:pointcut id="ServiceMethod"
expression="execution(* com.hs.serviceImpl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="ServiceMethod" />
</aop:config>
java注解型 事务处理
最新推荐文章于 2024-05-12 01:30:33 发布