Spring?BOOT?AOP基礎(chǔ)應(yīng)用教程
面試課題 Spring boot AOP
Spring boot 中 AOP是其中 重要的特性,其實現(xiàn)的方式借助的攔截器 + Proxy 動態(tài)代理,在AOP主要用于日志打印,安全攔截,事務(wù)處理,異常處理和性能統(tǒng)計,要向深刻了解Spring boot AOP 原理,從 Spring 動態(tài)代理的原理講起
Spring boot 動態(tài)代理
原理:
動態(tài)代理底層實現(xiàn)借助 java.lang.reflect.Proxy 的 newProxyInstance的方法
其有是三個參數(shù):
1.Class的類加載器
2.接口方法
3.h 增強方式
在代碼中 定于 interface , interfaceImpl 具體的實現(xiàn)類 ,使用 java 代理代碼方式進行處理:
Proxy.newProxyInstance(Main.Class.getClassLoader(), new Class[] {UserDao.class}, new InvocationHandler() { }) //在 InvocationHandler() 調(diào)用方法之前增強添加預(yù)處理 和 方法調(diào)用后的處理東西
public interface UserDao { public int add(int a, int b); } public class UserDaoImpl implements UserDao { @Override public int add(int a, int b) { System.out.println("add 方法執(zhí)行了"); return a+b; } } import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Arrays; public class Main { public static void main(String[] args) { Class[] interfaces = {UserDao.class}; UserDaoImpl userDaoImpl = new UserDaoImpl(); //創(chuàng)建接口實現(xiàn)類代理對象 //此處用UserDao作為返回值的類型,是因為我們傳入的interfaces就是UserDao.class UserDao dao = (UserDao) Proxy.newProxyInstance(Main.class.getClassLoader(), interfaces, new InvocationHandler() { //把想要代理的對象傳遞進來 private Object object = userDaoImpl; //增強的邏輯 @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //方法之前 System.out.println("方法之前執(zhí)行 : " + method.getName() + "; 傳遞的參數(shù):" + Arrays.toString(args) + "; object:" + object); //被增強的方法執(zhí)行,填寫要增強的對象、參數(shù) Object res = method.invoke(object, args); //方法之后 System.out.println("方法之后執(zhí)行 : " + method.getName() + "; 傳遞的參數(shù):" + Arrays.toString(args) + "; object:" + object); return res; } }); int res = dao.add(1, 2); System.out.println("這個是res: " + res); } }
總結(jié)
Spring boot中 能夠?qū)崿F(xiàn)AOP的底層原理,之上的代碼屬于靜態(tài)編碼方式 ,需要相同的邏輯抽象出來,因此誕生了AOP,在Spring boot中 動態(tài)代理有兩種
基于接口的JDK-動態(tài)代理(返回類型屬于接口類型);
基于父類的cglib 代理,通過繼承關(guān)系代理(不管是接口還是實現(xiàn)類 OK)
在實際使用過程中 Spring boot 默認 cglib動態(tài)代理 ,使用范圍更加廣泛
AOP 切面
基本知識
- pointcut: 切入點: execution… 表示需要在哪些方法上生效,對哪些方法進行增強 – 使用正則表達式
- Advice: 通知: 自定義處理 ,通知 分為BeforAdvice, AfterAdvice, ThrowAdvice
- Advisor: 將 PointCut 與 Advice 進行連接起來定義哪些通知在哪些方法增強生效 – 對切面XXAOP 使用@Ascpect 注解進行生效定義
@Component @Aspect public class BookAop { // 定義切入點 public static final String POINT_CUT = "execution(* com.example.bootaop.dao..*.*(..))"; @Before(POINT_CUT) public void before() { System.out.println("----------添加圖書方法前[校驗]-----------"); } @After(POINT_CUT) public void after(JoinPoint jp) { System.out.println("----------添加圖書成功后-----------"); System.out.println(jp.getTarget().getClass()); System.out.println(Arrays.asList(jp.getArgs())); } }
自定義注解
如上顯示是 AOP的切面,但是AOP切面有個使用不好定法在于 pointcut 寫正則表達式 無法準(zhǔn)確的表達,最好有個插拔式方式 ,引入到自定義注解,自定義注解彌補這一缺陷
元注解
元注解是 java 自帶的類型
@Retention 注解 保留策略(SOURCE,CLASS,RUNTIME)
@Retention(RetentionPolicy.SOURCE) 僅存在于源碼中
@Retention(RetentionPolicy.CLASS) 存在于class字節(jié)碼中,但運行時無法獲取
@Retention(RetentionPolicy.RUNTIME) 存在于class字節(jié)碼中,運行時可以通過反射獲取
Target 注解 作用范圍
@Target(ElementType.TYPE) 接口、類等
@Target(ElementType.FIELD) 字段
@Target(ElementType.METHOD) 方法
@Target(ElementType.PARAMETER) 方法參數(shù)
@Target(ElementType.CONSTRUCTOR) 構(gòu)造函數(shù)
@Target(ElementType.LOCAL_VARIABLE) 局部變量
@Target(ElementType.ANNOTATION_TYPE) 注解
@Target(ElementType.PACKAGE) 包
自定義注解
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyLog { String value() default ""; }
如何在AOP引用
在 pointcut = “@annotation(MyLog)”
@Slf4j @Aspect @Component public class LogAspect { @Around("@annotation(myLog)") public Object around(ProceedingJoinPoint point, MyLog myLog) throws Throwable{ String className = point.getTarget().getClass().getName(); String methodName = point.getSignature().getName(); String value = myLog.value(); log.info("類名:{},方法名:{},注解值:{}",className,methodName,value); log.info("方法之前執(zhí)行"); long startTime = System.currentTimeMillis(); Object proceed = point.proceed(); long endTime = System.currentTimeMillis(); long time = endTime - startTime; log.info("方法之后執(zhí)行"); log.info("方法耗時:{}", time); return proceed; } }
到此這篇關(guān)于Spring BOOT AOP基礎(chǔ)應(yīng)用教程的文章就介紹到這了,更多相關(guān)Spring BOOT AOP內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文搞懂接口參數(shù)簽名與驗簽(附含java python php版)
這篇文章主要為大家介紹了java python php不同版的接口參數(shù)簽名與驗簽示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06Java中關(guān)于String StringBuffer StringBuilder特性深度解析
這篇文章主要介紹了Java中關(guān)于String StringBuffer StringBuilder特性深度解析,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09SpringBoot實現(xiàn)讀取YML,yaml,properties文件
yml,yaml,properties三種文件都是用來存放配置的文件,一些靜態(tài)數(shù)據(jù),配置的數(shù)據(jù)都會存放到里邊。本文主要為大家整理了SpringBoot實現(xiàn)讀取YML,yaml,properties文件的方法,需要的可以參考一下2023-04-04springboot調(diào)用支付寶第三方接口(沙箱環(huán)境)
這篇文章主要介紹了springboot+調(diào)用支付寶第三方接口(沙箱環(huán)境),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Spring Cloud Gateway + Nacos 實現(xiàn)動態(tài)路由
這篇文章主要介紹了Spring Cloud Gateway + Nacos 實現(xiàn)動態(tài)路由的方法,幫助大家實現(xiàn)路由信息的自動更新,感興趣的朋友可以了解下2020-10-10