Spring aop失效的幾種解決方案
先看下這個問題的背景:假設有一個spring應用,開發(fā)人員希望自定義一個注解@Log,可以加到指定的方法上,實現(xiàn)自動記錄日志(入?yún)?、出參、響應耗時這些)
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 { }
然后再寫一個Aspect來解析這個注解,對打了Log注解的方法進行增強處理
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); } } } }
寫一個測試Service類:
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); } }
最后來跑一把:
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 菩提樹下的楊過 */ @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"); } }
輸出如下:
顯然HelloService中的anotherSayHi方法,并未被aop增強。 原因其實很簡單,了解AOP原理的同學想必都知道,AOP的實現(xiàn)有二類,如果是基于接口的,會采用動態(tài)代理,生成一個代理類,如果是基于類的,會采用CGLib生成子類,然后在子類中擴展父類中的方法。
本文中HelloService并不是一個接口,所以從上圖的斷點中可以看出,當Spring運行時,HelloService被增加為...EnhancerBySpringCGLib...。但是當調(diào)用到anotherSayHi時
方法的調(diào)用方,其實是原始的HelloSerfvice實例,即:是未經(jīng)過Spring AOP增強的對象實例。所以解決問題的思路就有了,想辦法用增強后的HelloService實例來調(diào)用!
方法一:用Autowired 注入自身的實例
這個方法,第一眼看上去感覺有些怪,自己注入自己,感覺有點象遞歸/死循環(huán)的搞法,但確實可以work,Spring在解決循環(huán)依賴上有自己的處理方式,避免了死循環(huán)。
方法二:從Spring上下文獲取增強后的實例引用
原理與方法一其實類似,不多解釋。
方法三: 利用AopContext
不過這個方法要注意的是,主類入口上,必須加上exporseProxy=true,參考下圖:
最后來驗證下這3種方法是否生效:
從運行結果上看,3種方法都可以解決這個問題?!?/p>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java 實戰(zhàn)項目錘煉之IT設備固定資產(chǎn)管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java+SSM+jsp+mysql+maven實現(xiàn)一個IT設備固定資產(chǎn)管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11基于spring+quartz的分布式定時任務框架實現(xiàn)
在Spring中的定時任務功能,最好的辦法當然是使用Quartz來實現(xiàn)。這篇文章主要介紹了基于spring+quartz的分布式定時任務框架實現(xiàn),有興趣的可以了解一下。2017-01-01SSH框架網(wǎng)上商城項目第29戰(zhàn)之使用JsChart技術顯示商品銷售報表
這篇文章主要為大家詳細介紹了SSH框架網(wǎng)上商城項目第29戰(zhàn)之使用JsChart技術顯示商品銷售報表,感興趣的小伙伴們可以參考一下2016-06-06解決@Cacheable在同一個類中方法調(diào)用不起作用的問題
這篇文章主要介紹了解決@Cacheable在同一個類中方法調(diào)用不起作用的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07使用maven-assembly-plugin如何打包多模塊項目
這篇文章主要介紹了使用maven-assembly-plugin如何打包多模塊項目,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Spring類型轉換 ConversionSerivce Convertor解析
這篇文章主要介紹了Spring類型轉換 ConversionSerivce Convertor的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11