關於AOP無法切入同類調用方法的問題


一、前言

  Spring AOP在使用過程中需要注意一些問題,也就是平時我們說的陷阱,這些陷阱的出現是由於Spring AOP的實現方式造成的。每一樣技術都或多或少有它的局限性,很難稱得上完美,只要掌握其實現原理,在使用時不要掉進陷阱就行,也就是進行規避。

對於Spring AOP的陷阱,我總結了以下兩個方面,現在分別進行介紹。

二、各種AOP失敗場景

2.1、(public)方法被嵌套使用而失效

Service中的方法調用同Service中的另一個方法時,如此調用並非調用的是代理類中的方法,是不會被切進去的。換言之,必須要調用代理類才會被切進去。 那么應該怎么破呢?既然只有調用代理類的方法才能切入,那我們拿到代理類不就好了嘛。嘗試性的在IDE里面搜Aop相關的類,一眼就看到一個叫AopContext的東西,看來游戲啊,里面有一個方法叫做currentProxy(),返回一個Object。但這樣做,需要修改Spring的默認配置expose-proxy="true"。

2.1.1、 問題場景

通過例子來講解這樣更好,首先加上注解配置:

<!-- 啟用注解式AOP -->
<aop:aspectj-autoproxy/>

然后定義一個切面,代碼如下:

@Aspect
@Component
public class AnnotationAspectTest {

@Pointcut("execution(* *.action(*))")
public void action() {
}

@Pointcut("execution(* *.work(*))")
public void work() {
}

@Pointcut("action() || work())")
public void compositePointcut() {
}

//前置通知
@Before("compositePointcut()")
public void beforeAdvice() {
    System.out.println("before advice.................");
}

//后置通知
@After("compositePointcut()")
public void doAfter() {
    System.out.println("after advice..................");
}
}

測試代碼:

//定義接口
public interface IPersonService {
String action(String msg);

String work(String msg);
}

//編寫實現類
@Service
public class PersonServiceImpl implements IPersonService {

public String action(String msg) {
System.out.println("FooService, method doing.");

this.work(msg); // *** 代碼 1 ***

return "[" + msg + "]";
}

@Override
public String work(String msg) {
System.out.println("work: * " + msg + " *");
return "* " + msg + " *";
}
}

//單元測試

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class FooServiceTest {

@Autowired
private IPersonService personService;

@Test
public void testAction() {
personService.action("hello world.");
}
}

測試結果:

說明嵌套在action方法內部的work方法沒有被進行切面增強,它沒有被“切中”。

2.1.2、 解決方案

A、在實現類中,如果注釋掉代碼1,將代碼1改為:

((IPersonService) AopContext.currentProxy()).work(msg); // *** 代碼 2 ***

B、並且在XML配置中加上expose-proxy="true",變為:<aop:aspectj-autoproxy expose-proxy="true"/>。或者在spring-boot中,目前都是通過annotation來代替配置文件的,所以我們必須找到一個annotation來代替這段配置,發現在ApplicationMain中加入@EnableAspectJAutoProxy(proxyTargetClass=true),需要增加spring-boot-starter-aop的依賴。

運行結果為:

嵌套在action方法內部的work方法被進行了切面增強,它被“切中”。

2.1.3、 原因分析

2.1.3.1 原理
以上結果的出現與Spring AOP的實現原理息息相關,由於Spring AOP采用了動態代理實現AOP,在Spring容器中的bean(也就是目標對象)會被代理對象代替,代理對象里加入了我們需要的增強邏輯,當調用代理對象的方法時,目標對象的方法就會被攔截。而上文中問題出現的症結也就是在這里,通過調用代理對象的action方法,在其內部會經過切面增強,然后方法被發射到目標對象,在目標對象上執行原有邏輯,如果在原有邏輯中嵌套調用了work方法,則此時work方法並沒有被進行切面增強,因為此時它已經在目標對象內部。

而解決方案很好地說明了,將嵌套方法發射到代理對象,這樣就完成了切面增強。

2.1.3.2 源代碼分析
接下來我們簡單看一下源代碼,Spring AOP的代碼邏輯相當清晰:

/**
* Implementation of {@code InvocationHandler.invoke}.
* <p>Callers will see exactly the exception thrown by the target,
* unless a hook method throws an exception.
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    ... ...

    Object retVal;

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

    ... ...

    // Get the interception chain for this method.
    List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

    // Check whether we have any advice. If we don't, we can fallback on direct
    // reflective invocation of the target, and avoid creating a MethodInvocation.
    if (chain.isEmpty()) {
        // We can skip creating a MethodInvocation: just invoke the target directly
        // Note that the final invoker must be an InvokerInterceptor so we know it does
        // nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
        retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args);
    }
    else {
        // We need to create a method invocation...
        invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
        // Proceed to the joinpoint through the interceptor chain.
        retVal = invocation.proceed();
    }

... ...
}

在代碼3處,如果配置了exposeProxy開關,則會將代理對象暴露在當前線程中,以供其它需要的地方使用。那么是怎么暴露的呢?答案很簡單,通過使用靜態的全局ThreadLocal變量就解決了問題。

2.2、Spring事務在多線程環境下失效

2.2.1 問題場景

沿用上面的代碼稍作修改,加上事務配置:

<!-- 數據庫的事務管理器配置 -->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="meilvDataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>

代碼如下所示:

@Service
@Transactional(propagation = Propagation.REQUIRED, timeout = 10000000)
public class PersonServiceImpl implements IPersonService {

    @Autowired
    IUserDAO userDAO;

    @Override
    public String action(final String msg) {

        new Thread(new Runnable() {
        @Override
        public void run() {
            (getThis()).work(msg);
        }
        }).start();

        UserDO userDO = new UserDO();
        userDO.setName("lanlan");
        userDAO.insert(userDO);

        return "[" + msg + "]";
    }

    @Override
    public String work(String msg) {
        System.out.println("work: * " + msg + " *");
        UserDO userDO = new UserDO();
        userDO.setName("yanyan");
        userDAO.insert(userDO);

        throw new RuntimeException();
    }

    private IPersonService getThis() {
        try {
            return (IPersonService) AopContext.currentProxy();
        } catch (IllegalStateException e) {
            return this;
        }
    }
}

結果:work方法中拋出異常,但是沒有影響事務的回滾,說明事務在子線程中失效了。

2.2.2 解決方案

只需要將多線程中的方法提出來,或者作為另一個Service類中的方法即可。

@Service
@Transactional(propagation = Propagation.REQUIRED, timeout = 10000000)
public class PersonServiceImpl implements IPersonService {

    @Autowired
    IUserDAO userDAO;

    @Override
    public String action(final String msg) {

        (getThis()).work(msg);

        UserDO userDO = new UserDO();
        userDO.setName("lanlan");
        userDAO.insert(userDO);

        return "[" + msg + "]";
    }

    @Override
    public String work(String msg) {
        System.out.println("work: * " + msg + " *");
        UserDO userDO = new UserDO();
        userDO.setName("yanyan");
        userDAO.insert(userDO);

        throw new RuntimeException();
    }

    private IPersonService getThis() {
        try {
            return (IPersonService) AopContext.currentProxy();
        } catch (IllegalStateException e) {
            return this;
        }
    }
}

上面只是一個簡單的例子,用於進行問題說明。

a、如果去掉多線程,將方法放在同一個類里,Spring則會根據事務的傳播配置參數,是否重新啟用新的事務。

b、如果將方法獨立出來放在新的類里,並且該方法也配置了事務,則會重新啟用新的事務。

2.2.3 原因分析

Spring的事務處理為了與數據訪問解耦,它提供了一套處理數據資源的機制,而這個機制與上文中的原理相差無幾,也是采用的ThreadLocal的方式。

在編程中,Service實例都是單例的無狀態的,事務管理則需要加入事務控制的相關狀態變量,使得Service實例不再是無狀態線程安全的,解決這個問題的方式就是使用ThreadLocal

通過使用ThreadLocal將數據源綁定在當前線程上,在當前線程的事務中,從設定的地方去取連接就會是同一個數據庫連接,這樣操作事務就會在同一個連接上進行。

但是,ThreadLocal的特性是,綁定在當前線程中的變量不會自動傳遞到其它線程中(當然,InheritableThreadLocal可以在父子線程中間傳遞變量值,但是這需要特殊的使用場景),所以當開啟子線程時,子線程並沒有父線程的數據庫連接資源。

對於上文提到的陷阱:如果另外開啟線程,那么在新線程中將獲取不到父線程的連接,事務要么失效,要么重新開啟一個新的。

源代碼如下:

public abstract class DataSourceUtils {

    public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {
    try {
        return doGetConnection(dataSource);
    }
    catch (SQLException ex) {
        throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
    }
    }

    public static Connection doGetConnection(DataSource dataSource) throws SQLException {
        ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
        if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) {
            conHolder.requested();
            if (!conHolder.hasConnection()) {
                logger.debug("Fetching resumed JDBC Connection from DataSource");
                conHolder.setConnection(dataSource.getConnection());
            }
            return conHolder.getConnection();
        }

        Connection con = dataSource.getConnection();

        //......

        return con;
    }
}


public abstract class TransactionSynchronizationManager {

    private static final ThreadLocal<Map<Object, Object>> resources =
    new NamedThreadLocal<Map<Object, Object>>("Transactional resources");

    /**
    * Retrieve a resource for the given key that is bound to the current thread.
    * @param key the key to check (usually the resource factory)
    * @return a value bound to the current thread (usually the active
    * resource object), or {@code null} if none
    * @see ResourceTransactionManager#getResourceFactory()
    */
    public static Object getResource(Object key) {
        Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
        Object value = doGetResource(actualKey);
        if (value != null && logger.isTraceEnabled()) {
            logger.trace("Retrieved value [" + value + "] for key [" + actualKey + "] bound to thread [" +
            Thread.currentThread().getName() + "]");
        }
        return value;
    }

    /**
    * Actually check the value of the resource that is bound for the given key.
    */
    private static Object doGetResource(Object actualKey) {
        Map<Object, Object> map = resources.get();
        if (map == null) {
            return null;
        }
        Object value = map.get(actualKey);
        // Transparently remove ResourceHolder that was marked as void...
        if (value instanceof ResourceHolder && ((ResourceHolder) value).isVoid()) {
            map.remove(actualKey);
            // Remove entire ThreadLocal if empty...
            if (map.isEmpty()) {
                resources.remove();
            }
            value = null;
        }
        return value;
    }
}

 

2.3、Spring Cache失效(同2.1aop類內部調用攔截失效相同)

我們知道緩存方法的調用是通過spring aop切入的調用的。在一個類調用另一個類中的方法可以直接的簡單調用,但是如果在同一個類中調用自己已經通過spring托管的類中的方法該如何實現呢?

先來段代碼:

public List<Long> getSkuIdsBySpuId(long spuId) {
   ItemComposite itemComposite = this.getItemComposite(spuId);///能走下面的緩存嗎?
   if (itemComposite!=null) {
       if ( CollectionUtils.isNotEmpty(itemComposite.getItemSkus())) {
           return itemComposite.getItemSkus().stream().map(itemSku -> itemSku.getId()).collect(Collectors.toList());
       }
   }
   return Collections.emptyList();
}

@Cacheable(value = "getItemComposite", key = "#spuId")
public ItemComposite getItemComposite(long spuId) {
   //select from db...
}

結果是這種方式是無法走到下面的getItemComposite緩存方法的,原因就是上面說的類內部無法通過直接調用方法來調用spring托管的bean,必須在當前類中拿到其代理類。通過查找資料修改如下:

public List<Long> getSkuIdsBySpuId(long spuId) {
    ItemCacheManager itemCacheManager = (ItemCacheManager)AopContext.currentProxy();
    if (itemComposite!=null) {
        if ( CollectionUtils.isNotEmpty(itemComposite.getItemSkus())) {
            return itemComposite.getItemSkus().stream().map(itemSku -> itemSku.getId()).collect(Collectors.toList());
        }
    }
    return Collections.emptyList();
}

@Cacheable(value = "getItemComposite", key = "#spuId")
public ItemComposite getItemComposite(long spuId) {
    //select from db...
}

可以看到修改的地方是通過調用AopContext.currentProxy的方式去拿到代理類來調用getItemComposite方法。這樣就結束了?不是,通過調試發現會拋出異常:java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.

繼續查找資料,csdn上一篇文章正好有篇文章http://blog.csdn.net/z69183787/article/details/45622821是講述這個問題的,他給的解決方法是在applicationContext.xml中添加一段<aop:aspectj-autoproxy proxy-target-class="true"expose-proxy="true"/>。但是與我們的系統不同的是,我們系統是通過spring-boot來啟動的,目前都是通過annotation來代替配置文件的,所以我們必須找到一個annotation來代替這段配置,發現在ApplicationMain中加入@EnableAspectJAutoProxy(proxyTargetClass=true)然后添加maven依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

可以解決我們的問題,這時候你一定認為事情可以大功告成了,但是真正的坑來了:我們的spring-boot版本是1.3.5,版本過低,這種注解必須是高版本才能支持。

還是想想csdn上的那篇文章,通過配置文件是可以解決的,那么我們就在spring boot中導入配置文件應該就沒問題了啊。

於是我們可以配置一個aop.xml文件,文件內容如下:

<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

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

</beans>

然后在ApplicationMain中添加注解如下:
@ImportResource(locations = "aop.xml")
OK.

 2.4、@Async失效

在同一個類中,一個方法調用另外一個有注解(比如@Async,@Transational)的方法,注解是不會生效的。

比如,下面代碼例子中,有兩方法,一個有@Async注解,一個沒有。第一次如果調用了有注解的test()方法,會啟動@Async注解作用;第一次如果調用testAsync(),因為它內部調用了有注解的test(),如果你以為系統也會為它啟動Async作用,那就錯了,實際上是沒有的。

    @Service
    public class TestAsyncService {
     
    public void testAsync() throws Exception {
    test();
    }
     
    @Async
    public void test() throws InterruptedException{
    Thread.sleep(10000);//讓線程休眠,根據輸出結果判斷主線程和從線程是同步還是異步
    System.out.println("異步threadId:"+Thread.currentThread().getId());
    }
    }

 

運行結果:testAsync()主線程和從線程()test()從線程同步執行。 
原因:spring 在掃描bean的時候會掃描方法上是否包含@Async注解,如果包含,spring會為這個bean動態地生成一個子類(即代理類,proxy),代理類是繼承原來那個bean的。此時,當這個有注解的方法被調用的時候,實際上是由代理類來調用的,代理類在調用時增加異步作用。然而,如果這個有注解的方法是被同一個類中的其他方法調用的,那么該方法的調用並沒有通過代理類,而是直接通過原來的那個bean,所以就沒有增加異步作用,我們看到的現象就是@Async注解無效。

三.aop類內部調用攔截失效的解決方案

3.1 方案一--從beanFactory中獲取對象

   剛剛上面說到controller中的UserService是代理對象,它是從beanFactory中得來的,那么service類內調用其他方法時,也先從beanFacotry中拿出來就OK了。

public void insert02(User u){
    getService().insert01(u);
}
private UserService getService(){
    return SpringContextUtil.getBean(this.getClass());
}

 3.2 方案二--獲取代理對象

private UserService getService(){

    // 采取這種方式的話,
//@EnableAspectJAutoProxy(exposeProxy=true,proxyTargetClass=true) //必須設置為true return AopContext.currentProxy() != null ? (UserService)AopContext.currentProxy() : this; }

 

  如果aop是使用注解的話,那需要@EnableAspectJAutoProxy(exposeProxy=true,proxyTargetClass=true),如果是xml配置的,把expose-proxy設置為true,如

<aop:config expose-proxy="true">
       <aop:aspect ref="XXX">
          <!-- 省略--->
       </aop:aspect>
</aop:config>

3.3方案三--將項目轉為aspectJ項目

將項目轉為aspectJ項目,aop轉為aspect 類。

spring AOP 之二:@AspectJ注解的3種配置

3.4 方案四--BeanPostProcessor   

    通過BeanPostProcessor 在目標對象中注入代理對象,定義InjectBeanSelfProcessor類,實現BeanPostProcessor。也不具體寫了

spring AOP 之二:@AspectJ注解的3種配置》 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM