duxinglangzi
2022-06-24 a87aa74a3af27960276ed02f4273386d25d2a231
提交 | 用户 | 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;
a87aa7 14 import org.springframework.context.ApplicationContext;
D 15 import org.springframework.context.ApplicationContextAware;
de8c2b 16 import org.springframework.core.MethodIntrospector;
D 17 import org.springframework.core.annotation.AnnotatedElementUtils;
18
19 import java.lang.reflect.Method;
20 import java.util.Collections;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.stream.Collectors;
25
26 /**
27  * @author wuqiong 2022/4/11
28  */
29 public class CanalListenerAnnotationBeanPostProcessor implements
a87aa7 30         BeanPostProcessor, SmartInitializingSingleton, BeanFactoryPostProcessor, ApplicationContextAware {
de8c2b 31
D 32     private static final Logger logger = LoggerFactory.getLogger(CanalListenerAnnotationBeanPostProcessor.class);
33
34     private final Set<Class<?>> notAnnotatedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(256));
35     private Set<CanalListenerEndpointRegistrar> registrars = Collections.newSetFromMap(new ConcurrentHashMap<>());
36     private ConfigurableListableBeanFactory configurableListableBeanFactory;
37     private CanalAutoConfigurationProperties canalAutoConfigurationProperties;
a87aa7 38     private ApplicationContext applicationContext;
de8c2b 39
D 40     @Override
41     public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
42         if (notAnnotatedClasses.contains(bean.getClass())) return bean;
43         Class<?> targetClass = AopUtils.getTargetClass(bean);
627420 44         // 只扫描类的方法,目前 CanalListener 只支持在方法上
de8c2b 45         Map<Method, CanalListener> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
D 46                 (MethodIntrospector.MetadataLookup<CanalListener>) method -> findListenerAnnotations(method));
47         if (annotatedMethods.isEmpty()) {
48             this.notAnnotatedClasses.add(bean.getClass());
49         } else {
627420 50             // 先加入到待注册里面
de8c2b 51             annotatedMethods.entrySet().stream()
D 52                     .filter(e -> e != null)
a87aa7 53                     .forEach(ele -> registrars.add(createRegistrar(bean, ele)));
627420 54             logger.info("Registered @CanalListener methods processed on bean:{} , Methods :{} ", beanName,
de8c2b 55                     annotatedMethods.keySet().stream().map(e -> e.getName()).collect(Collectors.toSet()));
D 56         }
57         return bean;
58     }
59
60
61     private CanalListener findListenerAnnotations(Method method) {
62         return AnnotatedElementUtils.findMergedAnnotation(method, CanalListener.class);
63     }
64
65
66     @Override
67     public void afterSingletonsInstantiated() {
68         canalAutoConfigurationProperties = configurableListableBeanFactory.getBean(CanalAutoConfigurationProperties.class);
69         TransponderContainerFactory.registerListenerContainer(configurableListableBeanFactory, canalAutoConfigurationProperties, registrars);
70     }
71
72     @Override
73     public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
74         this.configurableListableBeanFactory = configurableListableBeanFactory;
75     }
76
a87aa7 77     @Override
D 78     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
79         this.applicationContext = applicationContext;
80     }
de8c2b 81
a87aa7 82     private CanalListenerEndpointRegistrar createRegistrar(Object bean, Map.Entry<Method, CanalListener> entry) {
D 83         return new CanalListenerEndpointRegistrar(bean, entry.getKey(),
84                 applicationContext.getEnvironment().resolvePlaceholders(entry.getValue().destination()),
85                 applicationContext.getEnvironment().resolvePlaceholders(entry.getValue().database()),
86                 entry.getValue().table(), entry.getValue().eventType());
87     }
de8c2b 88 }