切面编程
springboot项目 pom.xml追加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
在Spring Boot应用程序中使用@Aspect注解来定义切面类,注意使用@Component注解,一开始我没有追加,运行起来不生效。
package org.example.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* desc:
* author:wangqiangac
* date:2023/5/6 11:22
*/
@Component
@Aspect
@Order(1)
public class AopDemo {
// @Pointcut(value = "execution(* org.example.controller.HelloController.*(..))")
@Pointcut(value = "execution(* org.example.controller.HelloController.hello(..))")
private void beforePointcut(){
//切面,方法里的内容不会执行
}
@Before(value = "beforePointcut()")
public void before(JoinPoint joinPoint){
//@Before是在方法执行前无法终止原方法执行
System.out.println("前置通知。。。"+joinPoint);
}
@Pointcut(value = "execution(* org.example.controller.HelloController.hello(..))")
private void afterPointcut(){}
@AfterReturning(value = "afterPointcut()",returning = "result")
public void after(Object result){
System.out.println("后置通知。。。"+result);
}
@Pointcut(value = "execution(* org.example.controller.HelloController.hello(..))")
private void aroundPointcut(){};
@Around(value = "aroundPointcut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕前:");
Object proceed = proceedingJoinPoint.proceed();
System.out.println("环绕后:");
return proceed;
}
@Pointcut(value = "execution(* org.example.controller.HelloController.hello(..))")
private void afterThrowingPointcut(){}
@AfterThrowing(value = " afterThrowingPointcut()" , throwing = "e")
public void afterThrowing(Throwable e){
System.out.println("异常通知:"+ e.getMessage());
}
}
该切面类作用于我自己写的controller中的function
package org.example.controller;
import org.example.aop.AopLog;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* desc:
* author:wangqiangac
* date:2022/11/1 20:16
*/
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
@AopLog
public String hello(){
String aa = null;
// if(aa.equals("execution(@)")){
//
// }
return "Hello Word";
}
@ResponseBody
@RequestMapping("/come")
@AopLog
public String come(){
String aa = null;
return "come come come come";
}
}
运行结果:
通过注解实现,定义注解,实现切面类,内部通过一个Pointcut来指定在带有我们上面自定义注解类AopLog注解的所有方法上进行拦截。
package org.example.aop;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* desc:
* author:wangqiangac
* date:2023/5/6 16:14
*/
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.CONSTRUCTOR,ElementType.METHOD})
public @interface AopLog {}
package org.example.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* desc:
* author:wangqiangac
* date:2023/5/6 16:21
*/
@Component
@Aspect
public class AopLogPointCut {
@Pointcut("execution(@org.example.aop.AopLog * *(..))")
public void logPointCut(){};
@After("logPointCut()")
public void onLogPointCutAfter(JoinPoint joinPoint){
System.out.println("注解切面:"+joinPoint.getSignature());
}
}
注解切面运行结果: