亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

關于Spring AOP使用時的一些問題匯總

瀏覽:32日期:2023-08-06 10:59:49

在使用AOP的時候遇到了一些問題,特此記錄一下

首先寫一個常用的AOP切片

切片類AopLog

package com.mantis.aop.aspect;import com.fasterxml.jackson.databind.ObjectMapper;import com.mantis.aop.common.util.DataUtil;import eu.bitwalker.useragentutils.UserAgent;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import org.springframework.validation.BeanPropertyBindingResult;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.ArrayList;import java.util.Arrays;import java.util.Enumeration;import java.util.List;import java.util.stream.Collectors;/** * @Description:執行順序 正常順序 Around Before Method.invoke Around After AfterReturning * 異常順序 Around Before Method.invoke After AfterThrowing * @author: wei.wang * @since: 2020/4/4 13:47 * @history: 1.2020/4/4 created by wei.wang */@Aspect@Componentpublic class AopLog { private static Logger logger = LoggerFactory.getLogger(AopLog.class); /** * 定義切點,切點為com.smec.fin.controller包和子包里任意方法的執行和service層所有方法的執行 */ @Pointcut('execution(public * com.mantis.aop.controller..*.*(..))') public void log() { // 定義切點 } /** * 定義切點,切點為com.smec.fin.controller包和子包里任意方法的執行和service層所有方法的執行 */ @Pointcut('execution(public * com.mantis.aop.service.impl..*.*(..))') public void log2() { // 定義切點 } /** * 環繞通知 * * @param point * @return * @throws Throwable */ @Around('log2()') public Object aroundLog(ProceedingJoinPoint point) throws Throwable { logger.info('開始執行環繞操作'); Object result = point.proceed(); logger.info('執行環繞操作結束,返回值:{}', result); return result; }}

服務類AopServiceImpl

package com.mantis.aop.service.impl;import com.mantis.aop.service.AopService;import org.springframework.aop.framework.AopContext;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;/** * @Description: * @author: wei.wang * @since: 2020/10/24 12:45 * @history: 1.2020/10/24 created by wei.wang */@Servicepublic class AopServiceImpl implements AopService { @Override public void testAop(String str) { System.out.println('com.mantis.aop.service.AopService.AopServiceImpl.testAop' + str); this.testAop2('testFinalMethod'); } @Override public void testAop2(String str) { //this.testFinalMethod('testFinalMethod'); System.out.println('com.mantis.aop.service.AopService.AopServiceImpl.' + str); } public final void testFinalMethod(String str) { System.out.println('com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod' + str); } public void testFinalMethod2(String str) { System.out.println('com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod' + str); }}

1.同方法運行問題

在使用AOP時我們發現存在同類中調用時切點失效問題,在執行時我們發現只執行了testAop的切點,testAop2的切點沒有執行,這是因為經過AOP代理后的對象都已經不是原來的對象了,而是加入了增強方法的代理對象,使用代理對象調用時可以執行增強方法,但是這里是使用this調用的,也就是AopServiceImpl,因為不是代理對象就沒有增強方法。

2020-10-24 13:31:06.261 INFO 13344 --- [nio-9000-exec-1] com.mantis.aop.controller.AopController : 方法開始執行2020-10-24 13:31:06.264 INFO 13344 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 開始執行環繞操作com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod2020-10-24 13:31:06.274 INFO 13344 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 執行環繞操作結束,返回值:null

用@Autowired或Resource引入自身依賴

使用注解方法引入自身代理依賴,注意使用構造的方式會有循環依賴問題

@Autowired AopServiceImpl aopService; @Override public void testAop(String str) { System.out.println('com.mantis.aop.service.AopService.AopServiceImpl.testAop' + str); aopService.testAop2('testFinalMethod'); System.out.println(); }

2020-10-24 13:36:33.477 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.controller.AopController : 方法開始執行2020-10-24 13:36:33.480 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 開始執行環繞操作com.mantis.aop.service.AopService.AopServiceImpl.testAoptest22020-10-24 13:36:33.488 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 開始執行環繞操作com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod2020-10-24 13:36:33.488 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 執行環繞操作結束,返回值:null2020-10-24 13:36:33.490 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 執行環繞操作結束,返回值:null

開啟暴露代理類,AopContext.currentProxy()方式獲取代理類

通過暴露代理類方式,實際原理是把代理類添加到當前請求的ThreadLocal里面,然后在使用時從ThreadLocal中獲取代理類,再調用對應的方法

在主方法上加入@EnableAspectJAutoProxy(exposeProxy=true),然后從AOP上下文中獲取代理

@Override public void testAop(String str) { System.out.println('com.mantis.aop.service.AopService.AopServiceImpl.testAop' + str); AopServiceImpl service = AopContext.currentProxy() != null ? (AopServiceImpl) AopContext.currentProxy() : this; service.testAop2('testFinalMethod'); System.out.println(); }

2020-10-24 13:38:31.031 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.controller.AopController : 方法開始執行2020-10-24 13:38:31.035 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 開始執行環繞操作com.mantis.aop.service.AopService.AopServiceImpl.testAoptest22020-10-24 13:38:31.047 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 開始執行環繞操作com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod2020-10-24 13:38:31.048 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 執行環繞操作結束,返回值:null2020-10-24 13:38:31.050 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 執行環繞操作結束,返回值:null

2.final關鍵字問題

Spring AOP默認使用cglib,會生成目標對象的子類代理對象。調用目標對象的方法,實際上是調用代理對象的方法。由于子類能夠繼承父類的方法,因此一般情況下目標類的方法,代理對象都會有。但是當目標類中某個方法帶有final關鍵字時,這個方法不能被重寫,因此代理對象中沒有這個方法,因此會調用目標對象的方法。

帶final關鍵字

因為testFinalMethod方法中存在final關鍵字,導致無法重寫,結果代理對象就無法生成這個方法,因此調用目標對象方法,導致沒有被增強

@Override public void testAop(String str) { System.out.println('com.mantis.aop.service.AopService.AopServiceImpl.testAop' + str); AopServiceImpl service = AopContext.currentProxy() != null ? (AopServiceImpl) AopContext.currentProxy() : this; service.testFinalMethod('testFinalMethod'); System.out.println(); } public final void testFinalMethod(String str) { System.out.println('com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod' + str); }

2020-10-24 13:47:46.907 INFO 15204 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 開始執行環繞操作com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethodtestFinalMethod2020-10-24 13:47:46.916 INFO 15204 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 執行環繞操作結束,返回值:null

不帶final關鍵字

因為testFinalMethod方法中不存在final關鍵字,所以正常執行增強。

@Override public void testAop(String str) { System.out.println('com.mantis.aop.service.AopService.AopServiceImpl.testAop' + str); AopServiceImpl service = AopContext.currentProxy() != null ? (AopServiceImpl) AopContext.currentProxy() : this; service.testFinalMethod2('testFinalMethod'); System.out.println(); } public final void testFinalMethod2(String str) { System.out.println('com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod' + str); }

2020-10-24 13:50:51.018 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.controller.AopController : 方法開始執行2020-10-24 13:50:51.021 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog : 開始執行環繞操作com.mantis.aop.service.AopService.AopServiceImpl.testAoptest22020-10-24 13:50:51.029 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog : 開始執行環繞操作com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethodtestFinalMethod2020-10-24 13:50:51.030 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog : 執行環繞操作結束,返回值:null2020-10-24 13:50:51.031 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog : 執行環繞操作結束,返回值:null

總結

到此這篇關于Spring AOP使用時的一些問題匯總的文章就介紹到這了,更多相關Spring AOP使用時的問題內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产精品视频99 | 欧美日韩国产综合视频在线看 | 日韩不卡 | 久草在线视频免费资源观看 | 国产日韩在线观看视频 | 1024cc香蕉在线观看播放中文看 | 国产白领丝袜办公室在线视频 | 成人精品美女隐私漫画 | 欧美日韩高清性色生活片 | 久久婷婷丁香 | 性激烈的欧美三级高清视频 | 一级女性全黄生活片免费看 | 性大片免费观看yy | 一级黄色大片免费观看 | 国语对白清晰好大好白在线 | 欧美精品国产综合久久 | 成片免费观看视频在线网 | 成年人免费黄色 | 鲁大师7视频在线观看 | 一区二区视频在线观看高清视频在线 | 久久99精品久久久久久首页 | a毛片全部免费播放 | 国产私拍视频 | 一级毛片a女人刺激视频免费 | 久久99青青久久99久久 | 综合亚洲一区二区三区 | 亚洲伦理在线 | 国产欧美精品国产国产专区 | 国产一区二区成人 | 久久精品成人免费看 | 好吊色青青青国产欧美日韩 | 中文字幕一区二区三区在线播放 | 亚洲精品网站在线观看不卡无广告 | 九九精品在线播放 | 99精品视频在线 | 成人欧美一区二区三区视频xxx | 一区二区在线视频免费观看 | 亚洲国产日韩a在线播放 | 成人片子| 啪视频在线观看高清 | 在线播放一区二区三区 |