文章

Aop实现

更适合框架封装的AOP实现.

Aop实现

定义注解

1
2
3
4
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAop {}

注解注册到AOP配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
@Slf4j
@Configuration(proxyBeanMethods = false)
public class AopConfig {
    
    @Bean
    public AopAnnotationPostProcessor aopAnnotationPostProcessor() {
        return new AopAnnotationPostProcessor(new AopMethodInterceptor(LogAop.class));
    }
    
    
    public static class AopAnnotationPostProcessor extends AbstractBeanFactoryAwareAdvisingPostProcessor {
        
        private final AopMethodInterceptor aopMethodInterceptor;
        
        public AopAnnotationPostProcessor(AopMethodInterceptor aopMethodInterceptor) {
            this.aopMethodInterceptor = aopMethodInterceptor;
        }
        
        @Override
        public void setBeanFactory(BeanFactory beanFactory) {
            super.setBeanFactory(beanFactory);
            this.advisor = new AopPointcutAdvisor(aopMethodInterceptor, aopMethodInterceptor.getAnnotationType());
        }
    }
    
    
    public static class AopMethodInterceptor implements MethodInterceptor, Ordered {
        
        private final Class<? extends Annotation> annotationType;
        
        public AopMethodInterceptor(Class<? extends Annotation> annotationType) {
            this.annotationType = annotationType;
        }
        
        @Override
        public Object invoke(final MethodInvocation invocation) throws Throwable {
            final Method targetMethod = invocation.getMethod();
            final Object[] arguments = invocation.getArguments();
            log.info("方法信息 :{}", targetMethod.getDeclaringClass());
            log.info("请求参数 :{}", arguments);
            //TODO 业务处理
            return invocation.proceed();
        }
        
        public Class<? extends Annotation> getAnnotationType() {
            return annotationType;
        }
        
        @Override
        public int getOrder() {
            return 0;
        }
        
    }
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Inherited
    @Documented
    public @interface LogAop {
    
    }
    
    public static class AopPointcutAdvisor extends AbstractPointcutAdvisor {
        
        private final Advice advice;
        
        private final Pointcut pointcut;
        
        public AopPointcutAdvisor(Advice advice, Class<? extends Annotation> asyncAnnotationType) {
            this.advice = advice;
            this.pointcut = buildPointcut(Stream.of(asyncAnnotationType).collect(Collectors.toSet()));
        }
        
        @Override
        public Pointcut getPointcut() {
            return pointcut;
        }
        
        
        @Override
        public Advice getAdvice() {
            return advice;
        }
        
        private Pointcut buildPointcut(Set<Class<? extends Annotation>> asyncAnnotationTypes) {
            ComposablePointcut result = null;
            for (Class<? extends Annotation> asyncAnnotationType : asyncAnnotationTypes) {
                Pointcut cpc = new AnnotationMatchingPointcut(asyncAnnotationType, true);
                Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(asyncAnnotationType);
                if (result == null) {
                    result = new ComposablePointcut(cpc).union(mpc);
                } else {
                    result.union(cpc).union(mpc);
                }
            }
            return result;
        }
    }
}

热门标签

_include_path : comment.html