zoukankan      html  css  js  c++  java
  • Spring AOP注解为什么失效?90%Java程序员不知道

    使用Spring Aop注解的时候,如@Transactional, @Cacheable等注解一般需要在类方法第一个入口的地方加,不然不会生效。

    如下面几种场景

    1、Controller直接调用Service B方法:Controller > Service A 

    在Service A 上加@Transactional的时候可以正常实现AOP功能。

    2、Controller调用Service A方法,A再调用B方法:Controller > Service A > Service B

    在Service B上加@Transactional的时候不能实现AOP功能,因为在Service A方法中调用Service B方法想当于使用this.B(),this代表的是Service类本身,并不是真实的代理Service对象,所以这种不能实现代理功能。

    所以,如果不是直接调用的方式,是不能实现代理功能的,非常需要注意。

    但确实有这种不是直接调用的试,也需要实现代理功能,怎么做呢?很简单,只需要暴露当前代理对象给当前线程就行了,如下配置,注解粗体的部分。

    <!-- aspect -->

    <aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"/>

    protected final T proxy() {

    return (T) AopContext.currentProxy();

    }

    这样就能拿到代理对象了,在Service A中可以通过proxy().B()即可正常实现B方法上面的代理功能。

    看下AopContext源码,Spring会将当前代理对象绑定到当前线程ThreadLocal上面。

  • 相关阅读:
    remove white space from read
    optimize the access speed of django website
    dowload image from requests
    run jupyter from command
    crawl wechat page
    python version 2.7 required which was not found in the registry windows 7
    health
    alternate rows shading using conditional formatting
    word
    【JAVA基础】static 关键字
  • 原文地址:https://www.cnblogs.com/zhangyu1024/p/9121880.html
Copyright © 2011-2022 走看看