@Async、@Transational、AOP 方法内部调用失效的解决方法

遇到过 方法A 内调用Aop修饰的方法B 失效、方法A 内调用@Async修饰的方法C 失效,百度谷歌都没看到一个好的解决,这里分享一个我的解决方案。

为什么失效

这个百度上很多解答,分析的也很好,其实就是Spring代理机制造成的。

简单的说,就是通过spring容器获取的类对象,很多情况下并不是原类,而是被spring修饰过了的代理类。

例如你执行 A类对象的方法A.invoke(),而spring对A类做了修饰:

proxyAbean.invoke():

before

invoke(bean,A)

after

实际你运行的是spring修饰过的代理类proxyAbean.invoke()方法。

这样就会造成一个问题,如果你在invoke()中调用A类的其余方法invoke2(),此时invoke2()是直接调用的原类的 A.invoke2(),而不是代理类proxyAbean.invoke2(),spring对方法做的修饰增强(@Async@TransationalAOP)全部不会实现。

如何解决

百度上都讲,将调用方法放入另外一个类就行了,这种方法其实走了弯路。

既然是因为没有调用到代理类的方法造成的,那我们重新获取一遍代理类,调用方法不就行了吗?

public class A{
    public void aMethod() {
        System.out.println("method a start");

//      bMethod();   //直接调用方法b,@Async不会生效

        A a = context.getBean(A.class); //从spring容器中重新获取A的代理对象,再调用b方法注解即生效
        a.bMethod();
        System.out.println("method a end");
    }

    @Async
    public void bMethod() {
        Thread.sleep(1000);
        System.out.println("我是异步方法!");
    }
}

代理类的获取很简单,通过spring容器context.getBean()即可。一般的spring项目都会全局保持一个context:

/**
 * 持有spring上下文的工具类,一个系统只能有一个SpringContextHolder。
 * 

该工具类主要用于: 通过spring上下文获取bean

*/
public class SpringContextHolder implements ApplicationContextAware,DisposableBean{ protected static final Log log = LogFactory.getLog(SpringContextHolder.class); private static ApplicationContext applicationContext; /** * 将spring容器上下文:applicationContext注入 */ @Override public void setApplicationContext(ApplicationContext context) throws BeansException { if(applicationContext != null) throw new IllegalStateException("ApplicationContextHolder already holded 'applicationContext'."); log.info("Injecting 'applicationContext' to " + SpringContextHolder.class.getSimpleName() + ", applicationContext=" + context); applicationContext = context; } private static ApplicationContext getApplicationContext() { return applicationContext; } /** * 本类SpringContextHolder被销毁时,将spring上下文置空 */ @Override public void destroy() throws Exception { applicationContext = null; } /** * 根据class获取spring容器中的bean */ public static T getBean(Class c){ return applicationContext.getBean(c); } /** * 根据class名称获取spring中的bean */ @SuppressWarnings("unchecked") public static T getBean(String beanName){ return (T)getApplicationContext().getBean(beanName); } }

补充:spring官方文档上的解决

spring的官方文档上针对AOP方法内部调用还提供了一种解决方案:
1. 在配置文件加入如下配置,使代理类暴露给线程。注意该配置要spring3.0以上:

<aop:aspectj-autoproxy expose-proxy="true"/>
  1. 手动调用代理类运行方法B:
if (null != AopContext.currentProxy()) {
            rs=((Bean)AopContext.currentProxy()).method(...);
        } else {
            rs=method(...);
        }

你可能感兴趣的:(JavaEE)