<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager"></tx:advice>
這兩個(gè)transactionManager互相引用
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--1.配置包掃描 -->
<context:component-scan base-package="com.jt"></context:component-scan>
<!--2.配置數(shù)據(jù)源,它將被mybatis引用 -->
<!--2.1導(dǎo)入pro配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<!-- private Resource[] locations; -->
<list>
<value>classpath:/property/jdbc.properties</value>
</list>
</property>
</bean>
<!--2.2配置數(shù)據(jù)源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--3.配置事務(wù)控制 -->
<tx:annotation-driven />
<!--3.1定義事務(wù)管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--3.2定義事務(wù)策略
propagation="REQUIRED" 執(zhí)行該操作必須添加事務(wù)
propagation="SUPPORTS" 事務(wù)支持的,原來(lái)的操作有事務(wù),則添加事務(wù)
原有的操作沒(méi)事務(wù),不添加事務(wù)
propagation="NEVER"從不添加事務(wù)
propagation="REQUIRES_NEW" 不管之前有沒(méi)有事務(wù),都會(huì)創(chuàng)建一個(gè)新的事務(wù)
read-only="true" 該操作只讀
*除此之外的方法只讀
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--3.3定義事務(wù)切面
Content Model : (pointcut*, advisor*, aspect*)
連接點(diǎn), 通知, 自定義切面
expression 切入點(diǎn)表達(dá)式
within(包名.類(lèi)名) 按類(lèi)匹配-控制粒度-粗粒度
execution(返回值類(lèi)型 包名.類(lèi)名.方法名(參數(shù)列表))
execution(返回值類(lèi)型 包名.類(lèi)名.方法名(int))
execution(返回值類(lèi)型 包名.類(lèi)名.方法名(String))
execution(返回值類(lèi)型 包名.類(lèi)名.方法名(int,String))
-->
<aop:config>
<aop:pointcut expression="execution(* com.example.manage.service..*.*(..))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
</beans>