spring 事务代理的方法

由于 Spring 事务管理是基于接口代理或动态字节码技术,通过 AOP 实施事务增强的。虽然,Spring 还支持 AspectJ LTW 在类加载期实施增强,但这种方法很少使用,所以我们不予关注。
对于基于接口动态代理的 AOP 事务增强来说,由于接口的方法是 public 的,这就要求实现类的实现方法必须是 public 的(不能是 protected,private 等),同时不能使用 static 的修饰符。所以,可以实施接口动态代理的方法只能是使用“public”或“public final”修饰符的方法,其它方法不可能被动态代理,相应的也就不能实施 AOP 增强,也不能进行 Spring 事务增强了。
基于 CGLib 字节码动态代理的方案是通过扩展被增强类,动态创建子类的方式进行 AOP 增强植入的。由于使用 final、static、private 修饰符的方法都不能被子类覆盖,相应的,这些方法将不能被实施 AOP 增强。所以,必须特别注意这些修饰符的使用,以免不小心成为事务管理的漏网之鱼。
下面通过具体的实例说明基于 CGLib 字节码动态代理无法享受 Spring AOP 事务增强的特殊方法。
清单 6.UserService.java:4 个不同修饰符的方法
package user.special;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {
   
//① private方法因访问权限的限制,无法被子类覆盖
    private void method1() {
        System.out.println("method1");
    }
   
//② final方法无法被子类覆盖
    public final void method2() {
        System.out.println("method2");
    }

    //③ static是类级别的方法,无法被子类覆盖
    public static void method3() {
        System.out.println("method3");
    }
   
//④ public方法可以被子类覆盖,因此可以被动态字节码增强
    public void method4() {
        System.out.println("method4");
    }
}
Spring 通过 CGLib 动态代理技术对 UserService Bean 实施 AOP 事务增强的配置如下所示:
清单 7.applicationContext.xml:对 UserService 用 CGLib 实施事务增强
<?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">

    <!-- 省略声明数据源及DataSourceTransactionManager事务管理器-->
    …
    <aop:config proxy-target-class="true">
    <!-- ①显式使用CGLib动态代理 -->
        <!-- ②希望对UserService所有方法实施事务增强 -->
        <aop:pointcut id="serviceJdbcMethod"
            expression="execution(* user.special.UserService.*(..))"/>
        <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>
在 ① 处,我们通过 proxy-target-class="true"显式使用 CGLib 动态代理技术,在 ② 处通过 AspjectJ 切点表达式表达 UserService 所有的方法,希望对 UserService 所有方法都实施 Spring AOP 事务增强。
在 UserService 添加一个可执行的方法,如下所示:
清单 8.UserService.java 添加 main 方法
package user.special;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {
    …
    public static void main(String[] args) {
        ApplicationContext ctx =
            new ClassPathXmlApplicationContext("user/special/applicationContext.xml");
       
UserService service = (UserService) ctx.getBean("userService");

        System.out.println("before method1");
        service.method1();
        System.out.println("after method1");

        System.out.println("before method2");
        service.method2();
        System.out.println("after method2");

        System.out.println("before method3");
        service.method3();
        System.out.println("after method3");

        System.out.println("before method4");
        service.method4();
        System.out.println("after method4");

    }
}
在运行 UserService 之前,将 Log4J 日志级别设置为 DEBUG,运行以上代码查看输出日志,如下所示:
17:24:10,953  (AbstractBeanFactory.java:241)
    - Returning cached instance of singleton bean 'userService'

before method1
method1
after method1
before method2
method2
after method2
before method3
method3
after method3
before method4

17:24:10,953  (AbstractPlatformTransactionManager.java:365)
    - Creating new transaction with name [user.special.UserService.method4]:
PROPAGATION_REQUIRED,ISOLATION_DEFAULT

17:24:11,109  (DataSourceTransactionManager.java:205)
    - Acquired Connection [org.apache.commons.dbcp.PoolableConnection@165b7e]
for JDBC transaction



17:24:11,109  (DataSourceTransactionManager.java:265)
    - Committing JDBC transaction on Connection
[org.apache.commons.dbcp.PoolableConnection@165b7e]

17:24:11,125  (DataSourceTransactionManager.java:323)
    - Releasing JDBC Connection [org.apache.commons.dbcp.PoolableConnection@165b7e]
after transaction

17:24:11,125  (DataSourceUtils.java:312)
    - Returning JDBC Connection to DataSource

after method4
观察以上输出日志,很容易发现 method1~method3 这 3 个方法都没有被实施 Spring 的事务增强,只有 method4 被实施了事务增强。这个结果刚才验证了我们前面的论述。
我们通过下表描述哪些特殊方法将成为 Spring AOP 事务增强的漏网之鱼:
表 2. 不能被 Spring AOP 事务增强的方法
动态代理策略 不能被事务增强的方法
基于接口的动态代理 除 public 外的其它所有的方法,此外 public static 也不能被增强
基于 CGLib 的动态代理 private、static、final 的方法
不过,需要特别指出的是,这些不能被 Spring 事务增强的特殊方法并非就不工作在事务环境下。只要它们被外层的事务方法调用了,由于 Spring 的事务管理的传播特殊,内部方法也可以工作在外部方法所启动的事务上下文中。我们说,这些方法不能被 Spring 进行 AOP 事务增强,是指这些方法不能启动事务,但是外层方法的事务上下文依就可以顺利地传播到这些方法中。
这些不能被 Spring 事务增强的方法和可被 Spring 事务增强的方法唯一的区别在 “是否可以主动启动一个新事务”:前者不能而后者可以。对于事务传播行为来说,二者是完全相同的,前者也和后者一样不会造成数据连接的泄漏问题。换句话说,如果这些“特殊方法”被无事务上下文的方法调用,则它们就工作在无事务上下文中;反之,如果被具有事务上下文的方法调用,则它们就工作在事务上下文中。
对于 private 的方法,由于最终都会被 public 方法封装后再开放给外部调用,而 public 方法是可以被事务增强的,所以基本上没有什么问题。在实际开发中,最容易造成隐患的是基于 CGLib 的动态代理时的“public static”和“public final”这两种特殊方法。原因是它们本身是 public 的,所以可以直接被外部类(如 Web 层的 Controller 类)调用,只要调用者没有事务上下文,这些特殊方法也就以无事务的方式运作。

猜你喜欢

转载自john201314.iteye.com/blog/2206904
今日推荐