国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

詳解spring中aop不生效的幾種解決辦法

瀏覽:3日期:2023-08-31 15:54:31

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

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@Aspectpublic 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; @Componentpublic 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@EnableAspectJAutoProxypublic 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'); }}

輸出如下:

詳解spring中aop不生效的幾種解決辦法

顯然HelloService中的anotherSayHi方法,并未被aop增強。 原因其實很簡單,了解AOP原理的同學想必都知道,AOP的實現(xiàn)有二類,如果是基于接口的,會采用動態(tài)代理,生成一個代理類,如果是基于類的,會采用CGLib生成子類,然后在子類中擴展父類中的方法。

詳解spring中aop不生效的幾種解決辦法

本文中HelloService并不是一個接口,所以從上圖的斷點中可以看出,當Spring運行時,HelloService被增加為...EnhancerBySpringCGLib...。但是當調(diào)用到anotherSayHi時

詳解spring中aop不生效的幾種解決辦法

方法的調(diào)用方,其實是原始的HelloSerfvice實例,即:是未經(jīng)過Spring AOP增強的對象實例。所以解決問題的思路就有了,想辦法用增強后的HelloService實例來調(diào)用!

方法一:用Autowired 注入自身的實例

詳解spring中aop不生效的幾種解決辦法

這個方法,第一眼看上去感覺有些怪,自己注入自己,感覺有點象遞歸/死循環(huán)的搞法,但確實可以work,Spring在解決循環(huán)依賴上有自己的處理方式,避免了死循環(huán)。

方法二:從Spring上下文獲取增強后的實例引用

詳解spring中aop不生效的幾種解決辦法

原理與方法一其實類似,不多解釋。

方法三: 利用AopContext

詳解spring中aop不生效的幾種解決辦法

不過這個方法要注意的是,主類入口上,必須加上exporseProxy=true,參考下圖:

詳解spring中aop不生效的幾種解決辦法

最后來驗證下這3種方法是否生效:

詳解spring中aop不生效的幾種解決辦法

從運行結(jié)果上看,3種方法都可以解決這個問題。 

到此這篇關(guān)于詳解spring中aop不生效的幾種解決辦法的文章就介紹到這了,更多相關(guān)spring中aop不生效內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

作者:菩提樹下的楊過出處:http://yjmyzz.cnblogs.com

標簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 美女毛片免费看 | 久久中文字幕亚洲精品最新 | 成人久久18免费软件 | 草草视频免费在线观看 | 日韩欧美一区二区在线观看 | 国产91网址 | 国产精品李雅在线观看 | 亚洲国产日韩精品 | 国产精品久久久久久久久99热 | 亚洲一级免费视频 | 欧美黄网站免费观看 | 亚洲图片在线视频 | www.久操| 欧美三级做爰在线 | 一级一片免费播放 | 国产精品成人观看视频国产 | 欧美日韩综合网在线观看 | 999久久久精品视频在线观看 | 精品国产亚一区二区三区 | 韩日一级| 一级一片免费播放 | 久久免费福利 | 综合558欧美成人永久网站 | 一区二区三区免费在线观看 | 九九视频在线观看视频23 | 亚洲免费不卡 | 欧美国产成人精品一区二区三区 | 俄罗斯黄色一级片 | 亚洲gogo人体大胆西西安徽 | 91高端极品外围在线观看 | 欧美一区三区 | 国产aⅴ精品一区二区三区久久 | 免费观看一级特黄欧美大片 | 免费国产成人高清无线看软件 | 在线观看日本永久免费视频 | 亚洲综合91社区精品福利 | 青青爽国产手机在线观看免费 | 国产丝袜美女一区二区三区 | 亚洲人成在线观看 | 91欧美在线 | 在线精品亚洲欧洲第一页 |