duxinglangzi
2022-04-22 de8c2b2a4654893dc2c80f1fe095c165485bee5f
提交 | 用户 | age
de8c2b 1 package com.duxinglangzi.canal.starter.configuration;
D 2
3
4 import com.duxinglangzi.canal.starter.annotation.CanalListener;
5 import com.duxinglangzi.canal.starter.factory.TransponderContainerFactory;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8 import org.springframework.aop.support.AopUtils;
9 import org.springframework.beans.BeansException;
10 import org.springframework.beans.factory.SmartInitializingSingleton;
11 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
12 import org.springframework.beans.factory.config.BeanPostProcessor;
13 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
14 import org.springframework.core.MethodIntrospector;
15 import org.springframework.core.annotation.AnnotatedElementUtils;
16
17 import java.lang.reflect.Method;
18 import java.util.Collections;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.stream.Collectors;
23
24 /**
25  * @author wuqiong 2022/4/11
26  * @description
27  */
28 public class CanalListenerAnnotationBeanPostProcessor implements
29         BeanPostProcessor, SmartInitializingSingleton, BeanFactoryPostProcessor {
30
31     private static final Logger logger = LoggerFactory.getLogger(CanalListenerAnnotationBeanPostProcessor.class);
32
33     private final Set<Class<?>> notAnnotatedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(256));
34     private Set<CanalListenerEndpointRegistrar> registrars = Collections.newSetFromMap(new ConcurrentHashMap<>());
35     private ConfigurableListableBeanFactory configurableListableBeanFactory;
36     private CanalAutoConfigurationProperties canalAutoConfigurationProperties;
37
38     @Override
39     public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
40         if (notAnnotatedClasses.contains(bean.getClass())) return bean;
41         Class<?> targetClass = AopUtils.getTargetClass(bean);
42         Map<Method, CanalListener> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
43                 (MethodIntrospector.MetadataLookup<CanalListener>) method -> findListenerAnnotations(method));
44         if (annotatedMethods.isEmpty()) {
45             this.notAnnotatedClasses.add(bean.getClass());
46         } else {
47             annotatedMethods.entrySet().stream()
48                     .filter(e -> e != null)
49                     .forEach(ele -> registrars.add(new CanalListenerEndpointRegistrar(bean, ele)));
50             logger.info("Registered @CanalListener methods processed on bean:{} , Methods :{} ", bean.getClass().getName(),
51                     annotatedMethods.keySet().stream().map(e -> e.getName()).collect(Collectors.toSet()));
52         }
53         return bean;
54     }
55
56
57     private CanalListener findListenerAnnotations(Method method) {
58         return AnnotatedElementUtils.findMergedAnnotation(method, CanalListener.class);
59     }
60
61
62     @Override
63     public void afterSingletonsInstantiated() {
64         canalAutoConfigurationProperties = configurableListableBeanFactory.getBean(CanalAutoConfigurationProperties.class);
65         TransponderContainerFactory.registerListenerContainer(configurableListableBeanFactory, canalAutoConfigurationProperties, registrars);
66     }
67
68     @Override
69     public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
70         this.configurableListableBeanFactory = configurableListableBeanFactory;
71     }
72
73
74 }