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);
    }
}

文章标签: SpringBoot
推荐指数:

真诚点赞 诚不我欺~

Spring Boot AOP 拦截自定义注解

点赞 收藏 评论

关于作者

青苗
青苗

青苗幼儿园园长

等级 LV5

粉丝 20

获赞 47

经验 1182