關(guān)于Spring AOP使用時(shí)的一些問(wèn)題匯總
在使用AOP的時(shí)候遇到了一些問(wèn)題,特此記錄一下
首先寫(xiě)一個(gè)常用的AOP切片
切片類(lèi)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:執(zhí)行順序 正常順序 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
@Component
public class AopLog {
private static Logger logger = LoggerFactory.getLogger(AopLog.class);
/**
* 定義切點(diǎn),切點(diǎn)為com.smec.fin.controller包和子包里任意方法的執(zhí)行和service層所有方法的執(zhí)行
*/
@Pointcut("execution(public * com.mantis.aop.controller..*.*(..))")
public void log() {
// 定義切點(diǎn)
}
/**
* 定義切點(diǎn),切點(diǎn)為com.smec.fin.controller包和子包里任意方法的執(zhí)行和service層所有方法的執(zhí)行
*/
@Pointcut("execution(public * com.mantis.aop.service.impl..*.*(..))")
public void log2() {
// 定義切點(diǎn)
}
/**
* 環(huán)繞通知
*
* @param point
* @return
* @throws Throwable
*/
@Around("log2()")
public Object aroundLog(ProceedingJoinPoint point) throws Throwable {
logger.info("開(kāi)始執(zhí)行環(huán)繞操作");
Object result = point.proceed();
logger.info("執(zhí)行環(huán)繞操作結(jié)束,返回值:{}", result);
return result;
}
}
服務(wù)類(lèi)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
*/
@Service
public 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.同方法運(yùn)行問(wèn)題
在使用AOP時(shí)我們發(fā)現(xiàn)存在同類(lèi)中調(diào)用時(shí)切點(diǎn)失效問(wèn)題,在執(zhí)行時(shí)我們發(fā)現(xiàn)只執(zhí)行了testAop的切點(diǎn),testAop2的切點(diǎn)沒(méi)有執(zhí)行,這是因?yàn)榻?jīng)過(guò)AOP代理后的對(duì)象都已經(jīng)不是原來(lái)的對(duì)象了,而是加入了增強(qiáng)方法的代理對(duì)象,使用代理對(duì)象調(diào)用時(shí)可以執(zhí)行增強(qiáng)方法,但是這里是使用this調(diào)用的,也就是AopServiceImpl,因?yàn)椴皇谴韺?duì)象就沒(méi)有增強(qiáng)方法。
2020-10-24 13:31:06.261 INFO 13344 --- [nio-9000-exec-1] com.mantis.aop.controller.AopController : 方法開(kāi)始執(zhí)行 2020-10-24 13:31:06.264 INFO 13344 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 開(kāi)始執(zhí)行環(huán)繞操作 com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2 com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod 2020-10-24 13:31:06.274 INFO 13344 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null
用@Autowired或Resource引入自身依賴(lài)
使用注解方法引入自身代理依賴(lài),注意使用構(gòu)造的方式會(huì)有循環(huán)依賴(lài)問(wèn)題
@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 : 方法開(kāi)始執(zhí)行 2020-10-24 13:36:33.480 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 開(kāi)始執(zhí)行環(huán)繞操作 com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2 2020-10-24 13:36:33.488 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 開(kāi)始執(zhí)行環(huán)繞操作 com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod 2020-10-24 13:36:33.488 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null 2020-10-24 13:36:33.490 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null
開(kāi)啟暴露代理類(lèi),AopContext.currentProxy()方式獲取代理類(lèi)
通過(guò)暴露代理類(lèi)方式,實(shí)際原理是把代理類(lèi)添加到當(dāng)前請(qǐng)求的ThreadLocal里面,然后在使用時(shí)從ThreadLocal中獲取代理類(lèi),再調(diào)用對(duì)應(yīng)的方法
在主方法上加入@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 : 方法開(kāi)始執(zhí)行 2020-10-24 13:38:31.035 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 開(kāi)始執(zhí)行環(huán)繞操作 com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2 2020-10-24 13:38:31.047 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 開(kāi)始執(zhí)行環(huán)繞操作 com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod 2020-10-24 13:38:31.048 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null 2020-10-24 13:38:31.050 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null
2.final關(guān)鍵字問(wèn)題
Spring AOP默認(rèn)使用cglib,會(huì)生成目標(biāo)對(duì)象的子類(lèi)代理對(duì)象。調(diào)用目標(biāo)對(duì)象的方法,實(shí)際上是調(diào)用代理對(duì)象的方法。由于子類(lèi)能夠繼承父類(lèi)的方法,因此一般情況下目標(biāo)類(lèi)的方法,代理對(duì)象都會(huì)有。但是當(dāng)目標(biāo)類(lèi)中某個(gè)方法帶有final關(guān)鍵字時(shí),這個(gè)方法不能被重寫(xiě),因此代理對(duì)象中沒(méi)有這個(gè)方法,因此會(huì)調(diào)用目標(biāo)對(duì)象的方法。
帶final關(guān)鍵字
因?yàn)閠estFinalMethod方法中存在final關(guān)鍵字,導(dǎo)致無(wú)法重寫(xiě),結(jié)果代理對(duì)象就無(wú)法生成這個(gè)方法,因此調(diào)用目標(biāo)對(duì)象方法,導(dǎo)致沒(méi)有被增強(qiáng)
@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 : 開(kāi)始執(zhí)行環(huán)繞操作 com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2 com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethodtestFinalMethod 2020-10-24 13:47:46.916 INFO 15204 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null
不帶final關(guān)鍵字
因?yàn)閠estFinalMethod方法中不存在final關(guān)鍵字,所以正常執(zhí)行增強(qiáng)。
@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 : 方法開(kāi)始執(zhí)行 2020-10-24 13:50:51.021 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog : 開(kāi)始執(zhí)行環(huán)繞操作 com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2 2020-10-24 13:50:51.029 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog : 開(kāi)始執(zhí)行環(huán)繞操作 com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethodtestFinalMethod 2020-10-24 13:50:51.030 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null 2020-10-24 13:50:51.031 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null
總結(jié)
到此這篇關(guān)于Spring AOP使用時(shí)的一些問(wèn)題匯總的文章就介紹到這了,更多相關(guān)Spring AOP使用時(shí)的問(wèn)題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用SpringMVC返回json字符串的實(shí)例講解
下面小編就為大家分享一篇使用SpringMVC返回json字符串的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
基于Java編寫(xiě)emoji表情處理工具類(lèi)
這篇文章主要為大家詳細(xì)介紹了如何基于Java編寫(xiě)一個(gè)emoji表情處理工具類(lèi),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
Java中的do while循環(huán)控制語(yǔ)句基本使用
這篇文章主要介紹了Java中的do while循環(huán)控制語(yǔ)句基本使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Java中invokedynamic字節(jié)碼指令問(wèn)題
這篇文章主要介紹了Java中invokedynamic字節(jié)碼指令問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
完美解決idea沒(méi)有tomcat server選項(xiàng)的問(wèn)題
這篇文章主要介紹了完美解決idea沒(méi)有tomcat server選項(xiàng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
新手小白入門(mén)必學(xué)JAVA面向?qū)ο笾鄳B(tài)
說(shuō)到多態(tài),一定離不開(kāi)其它兩大特性:封裝和繼承,下面這篇文章主要給大家介紹了關(guān)于新手小白入門(mén)必學(xué)JAVA面向?qū)ο笾鄳B(tài)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
基于@AllArgsConstructor與@Value共用的問(wèn)題解決
這篇文章主要介紹了基于@AllArgsConstructor與@Value共用的問(wèn)題解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09

