Spring AOP注解案例及基本原理詳解
切面:Aspect
切面=切入點+通知。在老的spring版本中通常用xml配置,現(xiàn)在通常是一個類帶上@Aspect注解。切面負責將 橫切邏輯(通知) 編織 到指定的連接點中。
目標對象:Target
將要被增強的對象。
連接點:JoinPoint
可以被攔截到的程序執(zhí)行點,在spring中就是類中的方法。
切入點:PointCut
需要執(zhí)行攔截的方法,也就是具體實施了橫切邏輯的方法。切入點的規(guī)則在spring中通過AspectJ pointcut expression language來描述。
切入點與連接點的區(qū)別:連接點是所有可以被"切"的點;切入點是真正要切的點。
通知:Advice
針對切入點的橫切邏輯,包含“around”、“before”和“after”等不同類型的通知。
通知的作用點如其命名:
- before:在切入點之前執(zhí)行
- after:在切入點之后執(zhí)行
- around:在切入點攔截方法,自定義前后,更靈活
還有一些異常處理的通知,這里不一一舉例
織入:Weaving
將切面和目標對象連接起來,創(chuàng)建代理對象的過程。spring中用的是動態(tài)代理。假如目標對象有接口,使用jdk動態(tài)代理;否則使用cglib動態(tài)代理。
說了這么多概念,看看代碼實現(xiàn)可能會使讀者理解的更深刻一些,這里簡單寫一個通過注解增強方法的AOP-Demo。
首先是切面類:
package com.example.demo.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* @author Fcb
* @date 2020/6/20
* @description 切面類=切入點+通知
*/
@Aspect
@Component
public class LogAspect {
//這個方法定義了切入點
@Pointcut("@annotation(com.example.demo.aop.anno.MyLog)")
public void pointCut() {}
//這個方法定義了具體的通知
@After("pointCut()")
public void recordRequestParam(JoinPoint joinPoint) {
for (Object s : joinPoint.getArgs()) {
//打印所有參數(shù),實際中就是記錄日志了
System.out.println("after advice : " + s);
}
}
//這個方法定義了具體的通知
@Before("pointCut()")
public void startRecord(JoinPoint joinPoint) {
for (Object s : joinPoint.getArgs()) {
//打印所有參數(shù)
System.out.println("before advice : " + s);
}
}
//這個方法定義了具體的通知
@Around("pointCut()")
public Object aroundRecord(ProceedingJoinPoint pjp) throws Throwable {
for (Object s : pjp.getArgs()) {
//打印所有參數(shù)
System.out.println("around advice : " + s);
}
return pjp.proceed();
}
}
注解:
package com.example.demo.aop.anno;
import java.lang.annotation.*;
/**
* @author Fcb
* @date 2020/6/20
* @description
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MyLog {
}
目標類:
package com.example.demo.aop.target;
import com.example.demo.aop.anno.MyLog;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Fcb
* @date 2020/6/20
* @description
*/
@RestController
public class MockController {
@RequestMapping("/hello")
@MyLog
public String helloAop(@RequestParam String key) {
System.out.println("do something...");
return "hello world";
}
}
最后是測試類:
package com.example.demo.aop.target;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @author Fcb
* @date 2020/6/20
* @description
*/
@SpringBootTest
class MockControllerTest {
@Autowired
MockController mockController;
@Test
void helloAop() {
mockController.helloAop("aop");
}
}
控制臺結(jié)果:
around advice : aop
before advice : aop
do something...
after advice : aop
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合Hibernate Validator實現(xiàn)參數(shù)驗證功能
這篇文章主要介紹了SpringBoot整合Hibernate Validator實現(xiàn)參數(shù)驗證功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-06-06
Java模擬單鏈表和雙端鏈表數(shù)據(jù)結(jié)構(gòu)的實例講解
這篇文章主要介紹了Java模擬單鏈表和雙端鏈表數(shù)據(jù)結(jié)構(gòu)的實例,注意這里的雙端鏈表不是雙向鏈表,是在單鏈表的基礎(chǔ)上保存有對最后一個鏈接點的引用,需要的朋友可以參考下2016-04-04
Java多線程之異步Future機制的原理和實現(xiàn)
這篇文章主要為大家詳細介紹了Java多線程之異步Future機制的原理和實現(xiàn),感興趣的小伙伴們可以參考一下2016-08-08
java高并發(fā)下CopyOnWriteArrayList替代ArrayList
這篇文章主要為大家介紹了java高并發(fā)下CopyOnWriteArrayList替代ArrayList的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12

