美文网首页
Spring-Aop 内部this调用本类方法不能被切面增强

Spring-Aop 内部this调用本类方法不能被切面增强

作者: 半支铅笔半块橡皮 | 来源:发表于2019-11-01 16:04 被阅读0次

    为了更形象的说明清楚问题,结合下面的例子:

    问题描述

    定义了一个增强类,对IDemoService类的所有方法进行增强

    @Aspect
    @Component
    public class AspectDemo {
    
        /**
         * 定义切点:如果有此注解的地方
         */
        @Pointcut("execution(public * com.yummon.braveglory.service.IDemoService.*())")
        public void serviceAspect() {
        }
        
        @Before(value = "serviceAspect()")
        public void before(){
            System.out.println("===before===");
        }
        
        @After(value = "serviceAspect()")
        public void after(){
            System.out.println("===after===");
        }
    }
    

    定义被增强类:

    public interface IDemoService {
        String demoHello();
    
        String normalHello();
    }
    
    @Service
    public class DemoService implements IDemoService {
        public String demoHello(){
            System.out.println("hello");
            normalHello();
            return "hello";
        }
        
        public String normalHello(){
            System.out.println("normal");
            return "normal";
        }
    }
    

    测试代码:

    @EnableAspectJAutoProxy
    @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
    public class PlayertoolApplication {
    
        public static void main(String[] args) throws IOException {
            final ConfigurableApplicationContext run = SpringApplication.run(PlayertoolApplication.class, args);
            final IDemoService bean = run.getBean(IDemoService.class);
            bean.demoHello();
            System.in.read();
        }
    }
    

    DemoService 类的demoHello 方法中调用本类的normalHello 方法,这就是标题所说的this调用本类方法,但是最后的结果是normal 方法并没有被增强。
    执行结果如下:

    ===before===
    hello
    normal
    ===after===
    

    解决办法

    思路:在SpringAop场景下,对于被代理类demoService会基于相应的代理类demoServiceProxy,在上面的测试代码中获得的final IDemoService bean就是代理对象。所以调用代理对象的demoHello()方法当然能得到增强,但是在demoHello 内部调用normalHello的时候已经进去了原生的demoService这个bean中,相当于直接调用demoService.normalHello方法,所以并不会增强。
    基于上面的思路有两种解决办法

    方法一:直接从BeanFactory中获取再次代理Bean

    示例代码如下:

    @Service
    public class DemoService implements IDemoService {
        
        @Autowired
        private ApplicationContext context;
        
        public String demoHello(){
            System.out.println("hello");
            final IDemoService bean = context.getBean(IDemoService.class);
            bean.normalHello();
            return "hello";
        }
    }
    

    即继续调用代理类的normalHello()方法。

    方法二:从AopContext中获取代理Bean

    SpringAop有一个配置参数exposeProxy 如果配置为true ,可以让代理对象的方法被调用时把代理对象放入AopContext,这个配置的定义如下(ps:看看这个配置的注释说明):

    //org.springframework.context.annotation.EnableAspectJAutoProxy
    /**
     * Indicate that the proxy should be exposed by the AOP framework as a {@code ThreadLocal}
     * for retrieval via the {@link org.springframework.aop.framework.AopContext} class.
     * Off by default, i.e. no guarantees that {@code AopContext} access will work.
     * @since 4.3.1
     */
    boolean exposeProxy() default false;
    

    所以代码还可以这样写:
    启动类EnableAspectJAutoProxy注解改为

    @EnableAspectJAutoProxy(exposeProxy = true)
    

    DemoServicedemoHello()方法改为:

        public String demoHello(){
            System.out.println("hello");
            final IDemoService bean = (IDemoService) AopContext.currentProxy();
            bean.normalHello();
            return "hello";
        }
    

    exposeProxy 属性如何生效的

    在这里拓展一点点,关于Aop有两个很重要的方法, 代理类增强方法被调用时会进入这两个个方法中的其中一个,基于代理方式:
    CglibAopProxy.DynamicAdvisedInterceptor#intercept //Cglib代理方式
    JdkDynamicAopProxy#invoke //JDK 代理方式

    这两个被代理类的方法在执行的时候都有这么一段代码:

    if (this.advised.exposeProxy) {
        // Make invocation available if necessary.
        oldProxy = AopContext.setCurrentProxy(proxy);
        setProxyContext = true;
    }
    

    因为进入了intercept或者invoke方法,那么表示正在调用proxy这个代理类的某个方法,那么就把代理类放入Aop 上下文:

    总结

    1. 本篇文章展示了Aop环境下 this 调用本类方法无法被增强的一个例子。
    2. 展示两种解决这种问题的方法,其实本质都是由this调用本类方法,改为调用代理类的对应增强方法。

    相关文章

      网友评论

          本文标题:Spring-Aop 内部this调用本类方法不能被切面增强

          本文链接:https://www.haomeiwen.com/subject/zhgfbctx.html