• 2.基于AOP自定义注解Annotation的实现


    上一篇中分析了AOP的实现原理,

    总结为:

    •  判断对象是否需要被代理?@Aspect注解的实现是根据切入点表达式
    •  代理之后需要做什么,就是那些通知,本质上是实现了MethodInterceptor的拦截器
    •  如何让一个自定义的通知器生效?就是注入一个实现Advisor的bean

    我们在使用@Aspect时通常都是使用表达式,基本上都是基于package级别的切,如果我们只想切某些较为分散的方法,这个时候配置表达式的话可能就不太方便啦,那么可以通过自定义注解,在需要的方法上加上这个注解就ok了

    根据这些信息,我们可以自己仿写一个类@Aspect的功能

    1.注入一个通知器,基于表达式的

    @Component
    public class MethodCustomAdvisor extends AbstractPointcutAdvisor {
    
        @Override
        public Pointcut getPointcut() {
            AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
    //定义切入点表达式 pointcut.setExpression("切入点表达式");
    //基于method级别的注解 // AnnotationMatchingPointcut.forMethodAnnotation(MethodCustomAnnotation.class);
         //基于class级别的注解 // AnnotationMatchingPointcut.forClassAnnotation(ClazzCustomAnnotation.class); return pointcut; } //绑定某个通知 @Override public Advice getAdvice() { return new MethodCustomInterceptor(); } }

     2. 定义一个拦截器(通知),表明这个通知要做什么,这样完全就可以啦

    @Slf4j
    public class MethodCustomInterceptor implements MethodInterceptor, Serializable {
    
        private Map<Method, Boolean> map = new ConcurrentHashMap<>();
    
        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            log.error("ff========");
    //譬如记录日志等等...... return invocation.proceed(); } }

     3.基于注解的实现

    基于注解的话,完全就是将上面的表达式的判断部分改为注解的相关判断就可以啦,aop本身提供了方法级别和类级别的注解判断,只要自己定义一个注解写上去就OK啦

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface MethodCustomAnnotation {
    
        String name() default "name";
     
        String value();
    
    }
    
  • 相关阅读:
    为https请求配置ssl(不用keystore,直接用证书,rsa私钥,java代码)
    http请求对于List类型参数的处理
    java中string转ByteBuffer
    lua for循环如何从第0位开始
    lua中的cjson简单使用
    mongodb返回方便读的数据
    markdown简单插入图片
    #问题#java报Annotation processing is not supported for module cycles
    #问题#java报can't create non-static inner class instance
    git commit+push的完整步骤
  • 原文地址:https://www.cnblogs.com/Hleaves/p/11337518.html
Copyright © 2020-2023  润新知