AOP在Service失效問(wèn)題

先看下這個(gè)問(wèn)題的背景:假設(shè)有一個(gè)spring應(yīng)用,開(kāi)發(fā)人員希望自定義一個(gè)注解@Log,可以加到指定的方法上,實(shí)現(xiàn)自動(dòng)記錄日志(入?yún)?、出參、響?yīng)耗時(shí)這些)

package com.cnblogs.yjmyzz.springbootdemo.aspect;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Log {
 
}

然后再寫(xiě)一個(gè)Aspect來(lái)解析這個(gè)注解,對(duì)打了Log注解的方法進(jìn)行增強(qiáng)處理

package com.cnblogs.yjmyzz.springbootdemo.aspect;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Method;
 
@Component
@Aspect
public class LogAspect {
 
    @Pointcut("execution (* com.cnblogs.yjmyzz.springbootdemo.service..*.*(..))")
    public void logPointcut() {
 
    }
 
    @Around("logPointcut()")
    public void around(JoinPoint point) {
        String methodName = point.getSignature().getName();
        Object[] args = point.getArgs();
        Class<?>[] argTypes = new Class[point.getArgs().length];
        for (int i = 0; i < args.length; i++) {
            argTypes[i] = args[i].getClass();
        }
        Method method = null;
        try {
            method = point.getTarget().getClass().getMethod(methodName, argTypes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //獲取方法上的注解
        Log log = method.getAnnotation(Log.class);
        if (log != null) {
            //演示方法執(zhí)行前,記錄一行日志
            System.out.println("before:" + methodName);
        }
        try {
            //執(zhí)行方法
            ((ProceedingJoinPoint) point).proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            if (log != null) {
                //演示方法執(zhí)行后,記錄一行日志
                System.out.println("after:" + methodName);
            }
        }
    }
}

寫(xiě)一個(gè)測(cè)試Service類(lèi):

package com.cnblogs.yjmyzz.springbootdemo.service;
 
import com.cnblogs.yjmyzz.springbootdemo.aspect.Log;
import org.springframework.stereotype.Component;
 
@Component
public class HelloService {
     
    @Log
    public void sayHi(String msg) {
        System.out.println("\tsayHi:" + msg);
    }
 
    public void anotherSayHi(String msg) {
        this.sayHi(msg);
    }
 
}

最后來(lái)跑一把:

package com.cnblogs.yjmyzz.springbootdemo;
 
import com.cnblogs.yjmyzz.springbootdemo.service.HelloService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
/**
 * @author 菩提樹(shù)下的楊過(guò)
 */
@ComponentScan("com.cnblogs.yjmyzz")
@Configuration
@EnableAspectJAutoProxy
public class SampleApplication {
 
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SampleApplication.class);
        HelloService helloService = context.getBean(HelloService.class);
        helloService.sayHi("hi-1");
        System.out.println("\n");
        helloService.anotherSayHi("hi-2");
    }
}

輸出如下:

image.png

顯然HelloService中的anotherSayHi方法,并未被aop增強(qiáng)。 原因其實(shí)很簡(jiǎn)單,了解AOP原理的同學(xué)想必都知道,AOP的實(shí)現(xiàn)有二類(lèi),如果是基于接口的,會(huì)采用動(dòng)態(tài)代理,生成一個(gè)代理類(lèi),如果是基于類(lèi)的,會(huì)采用CGLib生成子類(lèi),然后在子類(lèi)中擴(kuò)展父類(lèi)中的方法。
image.png

本文中HelloService并不是一個(gè)接口,所以從上圖的斷點(diǎn)中可以看出,當(dāng)Spring運(yùn)行時(shí),HelloService被增加為...EnhancerBySpringCGLib...。但是當(dāng)調(diào)用到anotherSayHi時(shí)方法的調(diào)用方,其實(shí)是原始的HelloSerfvice實(shí)例,即:是未經(jīng)過(guò)Spring AOP增強(qiáng)的對(duì)象實(shí)例。所以解決問(wèn)題的思路就有了,想辦法用增強(qiáng)后的HelloService實(shí)例來(lái)調(diào)用!
image.png

方法一:用Autowired 注入自身的實(shí)例
image.png

這個(gè)方法,第一眼看上去感覺(jué)有些怪,自己注入自己,感覺(jué)有點(diǎn)象遞歸/死循環(huán)的搞法,但確實(shí)可以work,Spring在解決循環(huán)依賴(lài)上有自己的處理方式,避免了死循環(huán)。
方法二:從Spring上下文獲取增強(qiáng)后的實(shí)例引用
image.png

原理與方法一其實(shí)類(lèi)似,不多解釋。
方法三: 利用AopContext
image.png

不過(guò)這個(gè)方法要注意的是,主類(lèi)入口上,必須加上exporseProxy=true,參考下圖:
image.png

最后來(lái)驗(yàn)證下這3種方法是否生效:
image.png

從運(yùn)行結(jié)果上看,3種方法都可以解決這個(gè)問(wèn)題。  
補(bǔ)充:AOP只能作用與publicprotected方法

作者:菩提樹(shù)下的楊過(guò)
出處:http://yjmyzz.cnblogs.com

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容