第一步,创建 xml 文件,并声明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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 在这里添加配置 -->
</beans>
第二步,配置 spring 事务管理器的 bean。
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
第三步,我们已经有了事务管理器,接下来配置它的生效规则。
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
对上面的配置作下说明:
id: 唯一标识。虽然配置了事务管理器对方法进行拦截的规则,但是还没有和切入点联系起来,比如要拦截哪个包下方法?哪个类下的方法?拥有哪些参数列表的方法?这些都要等着第四步的切入点配置完成后才能确定。
transaction-manager: 事务管理器。毋庸置疑,是我们第二步中配置的事务管理bean。
tx:method 中的 name: 对哪些方法进行拦截。
- get* 表示所有名称以 get 开头的方法。对于这些方法,不用开启事务,并且是只读操作,不会修改数据。
-
- 表示所有方法,propagation 不写会使用默认值 REQUIRED, 意思是开启事务。read-only 不写默认值是 false,表示会对数据进行写操作。
上面两个通配符,匹配范围小的优先级高。所有, 当匹配到 get* 时,优先使用 get* 的配置,也就是不开启事务。
第四步,配置 aop。事务管理器也是通过 aop 原理来工作的,这和其它的切面类并没有区别。既然其它的切面类是通过 aop来配置,它也不能例外。
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.zfy.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>
aop:pointcut
定义了切入点。切入点是以方法为最小单位的。其中的 expression 就定义了这个切点包含的方法。可以看到使用了通配符匹配了 com.zfy.service.impl 包下的所有方法。
aop:advisor 通过两个 ref 属性将事务管理器的相应规则应用到定义好的切点上。
这样,Spring 事务管理就配置成功了。至于其它的 bean 配置,比如 JdbcTemplate, DataSource,可以参考下面的完整配置:
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<bean id="accountService" class="com.zfy.service.impl.AccountServiceImpl">
<property name="dao" ref="accountDao"/>
</bean>
<bean id="accountDao" class="com.zfy.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ioc"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置事务管理bean-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置 aop-->
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.zfy.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>
</beans>