UserService#logon()方法内部调用了ScoreService#addScore()的方法,二者都分别经过Spring AOP进行了事务加强,则它们工做于同一事务中。来看具体的代码: java
package com.baobaotao.nestcall; … @Service("userService") public class UserService extends BaseService { @Autowired private JdbcTemplate jdbcTemplate; @Autowired private ScoreService scoreService; //①该方法嵌套调用了本类的其余方法及其余服务类的方法 public void logon(String userName) { System.out.println("before userService.updateLastLogonTime..."); updateLastLogonTime(userName);//①-1本服务类的其余方法 System.out.println("after userService.updateLastLogonTime..."); System.out.println("before scoreService.addScore..."); scoreService.addScore(userName, 20); //①-2其余服务类的其余方法 System.out.println("after scoreService.addScore..."); } public void updateLastLogonTime(String userName) { String sql = "UPDATE t_user u SET u.last_logon_time = ? WHERE user_name =?"; jdbcTemplate.update(sql, System.currentTimeMillis(), userName); }
UserService中注入了ScoreService的Bean,而ScoreService的代码以下所示: mysql
package com.baobaotao.nestcall; … @Service("scoreUserService") public class ScoreService extends BaseService{ @Autowired private JdbcTemplate jdbcTemplate; public void addScore(String userName, int toAdd) { String sql = "UPDATE t_user u SET u.score = u.score + ? WHERE user_name =?"; jdbcTemplate.update(sql, toAdd, userName); } }
经过Spring配置为ScoreService及UserService中全部公有方法都添加Spring AOP的事务加强,让UserService的logon()和updateLastLogonTime()及ScoreService的 addScore()方法都工做于事务环境下。下面是关键的配置代码: spring
<?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:component-scan base-package="com.baobaotao.nestcall"/> … <bean id="jdbcManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <!--①经过如下配置为全部继承BaseService类的全部子类的全部public方法都添加事务加强--> <aop:config proxy-target-class="true"> <aop:pointcut id="serviceJdbcMethod" expression="within(com.baobaotao.nestcall.BaseService+)"/> <aop:advisor pointcut-ref="serviceJdbcMethod" advice-ref="jdbcAdvice" order="0"/> </aop:config> <tx:advice id="jdbcAdvice" transaction-manager="jdbcManager"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice> </beans>
将日志级别设置为DEBUG,启动Spring容器并执行UserService#logon()的方法,仔细观察以下输出日志:sql