
切面:Aspect
切面=切入點 通知。在舊的spring版本中通常用xml來配置,現在通常是一個類別帶上@Aspect註解。切面負責將 橫切邏輯(通知) 編織 到指定的連接點。
目標物件:Target
將要被增強的物件。
連接點:JoinPoint
可以被攔截到的程式執行點,在spring中就是類別中的方法。
切入點:PointCut
需要執行攔截的方法,也就是具體實作了橫切邏輯的方法。切入點的規則在spring中透過AspectJ pointcut expression language來描述。
切入點與連接點的區別:連接點是所有可以被"切"的點;切入點是真正要切的點。
通知:Advice
針對切入點的橫切邏輯,包含「around」、「before」和「after」等不同類型的通知。
通知的作用點如其命名:
before:在切入點之前執行
織入:Weaving
首先是切面類別:
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()) {
//打印所有参数,实际中就是记录日志了
System.out.println("after advice : " + s);
}
}
//这个方法定义了具体的通知
@Before("pointCut()")
public void startRecord(JoinPoint joinPoint) {
for (Object s : joinPoint.getArgs()) {
//打印所有参数
System.out.println("before advice : " + s);
}
}
//这个方法定义了具体的通知
@Around("pointCut()")
public Object aroundRecord(ProceedingJoinPoint pjp) throws Throwable {
for (Object s : pjp.getArgs()) {
//打印所有参数
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");
}
}around advice : aop推薦教學:《before advice : aop
do something...
after advice : aop
Java教程》
以上是Spring AOP 註解詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!