Spring Boot AOP 拦截自定义注解
青苗 | 212 |
2023-08-10
自定义注解 Test
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Test {
String value() default "";
}
AOP TestAspect
@Aspect
@Component
public class TestAspect {
/**
* 切入点
*/
@Pointcut("@annotation(com.aizuda.annotation.Test)")
public void testPointCut() {
}
@Before("testPointCut()")
public void doBefore(JoinPoint joinPoint) {
Test test = this.getTest(joinPoint);
System.out.println(test.value());
}
@After("shardingPointCut()")
public void doAfter(JoinPoint joinPoint) {
Test test = this.getTest(joinPoint);
System.out.println(test.value());
}
public Test getTest(JoinPoint joinPoint) {
// 生产可以考虑缓存注解提取
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
return method.getAnnotation(Test.class);
}
}
推荐指数:
真诚点赞 诚不我欺~