本文介绍springboot实现aop的两种方式
首先需要引入对应依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
</dependency>
在启动类上面加上注解
@EnableAspectJAutoProxy
其实不加这个注解也可以,aop照样会生效,我们查看spring-boot-autoconfigure的依赖,查看spring.factories文件会发现以下配置
然后查看AopAutoConfiguration类会发现,当yml没有对应配置时,默认为true
下面展示常规方式实现aop的示例:
/**
* @Description
* @Author maruko
* @Date 2022/11/22 17:04
* @Version 1.0
*/
@Aspect
@Component
public class AspectTest {
@Pointcut("execution(* com.zjf.demo.controller.UserController.*(..))||execution(* com.zjf.demo.controller.KafkaController.*(..))")
public void pointExpression() {
}
@Before("pointExpression()")
public void before(JoinPoint joinPoint) {
// System.err.println(joinPoint.toString());
// Object[] args = joinPoint.getArgs();
// for (Object arg : args) {
// System.err.println(arg);
// }
// System.err.println(joinPoint.getSignature().toLongString());
// System.err.println(joinPoint.getSignature().toShortString());
// System.err.println(joinPoint.getSignature().getName());
// System.err.println(joinPoint.getSignature().toString());
// System.err.println(joinPoint.getKind());
// System.err.println(joinPoint.getTarget().toString());
// System.err.println(joinPoint.getThis().toString());
// System.err.println(joinPoint.getStaticPart());
// System.err.println(joinPoint.getSourceLocation());
// System.err.println(joinPoint.toLongString());
// System.err.println(joinPoint.toShortString());
System.err.println("前置通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));
}
// @After("execution(* com.zjf.demo.controller.UserController.*(..))")
public void after(JoinPoint joinPoint) {
System.err.println("后置通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));
}
// @AfterReturning(pointcut = "execution(* com.zjf.demo.controller.UserController.*(..))", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
System.err.println("返回通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()) + " 返回结果为:" + result);
}
// @AfterThrowing(value = "execution(* com.zjf.demo.controller.UserController.*(..))", throwing = "exception")
public void afterThrowing(JoinPoint joinPoint, Exception exception) {
System.err.println("异常通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()) + " 异常为:" + exception);
}
// @Around("execution(* com.zjf.demo.controller.UserController.*(..))")
public Object aroundMethod(ProceedingJoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
Object result = null;
try {
//前置通知
System.err.println("前置通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));
result = joinPoint.proceed();
//返回通知
System.err.println("返回通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()) + " 返回结果为:" + result);
} catch (Throwable e) {
// 异常通知
System.err.println("异常通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()) + " 异常为:" + e);
throw new RuntimeException(e);
}
//后置通知
System.err.println("后置通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));
return result;
}
}
注解方式实现如下:
首先定义注解:
@Target(ElementType.METHOD) //定义注解的使用范围为方法
@Retention(RetentionPolicy.RUNTIME )
public @interface AopAnnotation {
}
示例如下:
/**
@Description
@Author maruko
@Date 2022/11/23 9:33
@Version 1.0
*/
@Aspect
@Component
@Order(1)
public class AspectAnnotationTest {
@Pointcut(“@annotation(com.zjf.demo.annotation.AopAnnotation)”)
public void pointExpression() {
}
@Before(“pointExpression()”)
public void before(JoinPoint joinPoint) {
// System.err.println(joinPoint.toString());
// Object[] args = joinPoint.getArgs();
// for (Object arg : args) {
// System.err.println(arg);
// }
// System.err.println(joinPoint.getSignature().toLongString());
// System.err.println(joinPoint.getSignature().toShortString());
// System.err.println(joinPoint.getSignature().getName());
// System.err.println(joinPoint.getSignature().toString());
// System.err.println(joinPoint.getKind());
// System.err.println(joinPoint.getTarget().toString());
// System.err.println(joinPoint.getThis().toString());
// System.err.println(joinPoint.getStaticPart());
// System.err.println(joinPoint.getSourceLocation());
// System.err.println(joinPoint.toLongString());
// System.err.println(joinPoint.toShortString());
System.err.println(“注解方式实现,前置通知,方法名为:” + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));
}
}