Spring boot 设置AOP

Spring boot 设置AOP

根据xml演化而来的有两种方式:

  1. 使用@Aspect注解

  2. 使用DefaultPointcutAdvisor方式

SampleCode:

@Aspect方式

@Aspect
@Component
public class LockInterceptor {

    private final ParameterNameDiscoverer paramNameDiscoverer = new DefaultParameterNameDiscoverer();

    @Around(value = "@annotation(lock)")
    public Object proxy(ProceedingJoinPoint joinPoint, Lock lock) throws Throwable {
        ....
    }


}

DefaultPointcutAdvisor方式

@Bean
    public DefaultPointcutAdvisor lockAnnotationClassPointCut() {
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        advisor.setOrder(500);
        AnnotationMatchingPointcut pointcut = new AnnotationMatchingPointcut(Lock.class, true);
        ...
        advisor.setPointcut(pointcut);
        advisor.setAdvice(interceptor);
        return advisor;
    }

你可能感兴趣的:(JAVA)