提交 | 用户 | age
|
0b055a
|
1 |
package com.hz.canal.starter.configuration; |
R |
2 |
|
|
3 |
|
|
4 |
import com.hz.canal.starter.annotation.CanalListener; |
|
5 |
import com.hz.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.context.ApplicationContext; |
|
15 |
import org.springframework.context.ApplicationContextAware; |
|
16 |
import org.springframework.core.MethodIntrospector; |
|
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 |
|
30 |
BeanPostProcessor, SmartInitializingSingleton, BeanFactoryPostProcessor, ApplicationContextAware { |
|
31 |
|
|
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; |
|
38 |
private ApplicationContext applicationContext; |
|
39 |
|
|
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); |
|
44 |
// 只扫描类的方法,目前 CanalListener 只支持在方法上 |
|
45 |
Map<Method, CanalListener> annotatedMethods = MethodIntrospector.selectMethods(targetClass, |
|
46 |
(MethodIntrospector.MetadataLookup<CanalListener>) method -> findListenerAnnotations(method)); |
|
47 |
if (annotatedMethods.isEmpty()) { |
|
48 |
this.notAnnotatedClasses.add(bean.getClass()); |
|
49 |
} else { |
|
50 |
// 先加入到待注册里面 |
|
51 |
annotatedMethods.entrySet().stream() |
|
52 |
.filter(e -> e != null) |
|
53 |
.forEach(ele -> registrars.add(createRegistrar(bean, ele))); |
|
54 |
logger.info("Registered @CanalListener methods processed on bean:{} , Methods :{} ", beanName, |
|
55 |
annotatedMethods.keySet().stream().map(e -> e.getName()).collect(Collectors.toSet())); |
|
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 |
|
|
77 |
@Override |
|
78 |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
|
79 |
this.applicationContext = applicationContext; |
|
80 |
} |
|
81 |
|
|
82 |
private CanalListenerEndpointRegistrar createRegistrar(Object bean, Map.Entry<Method, CanalListener> entry) { |
|
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 |
} |
|
88 |
} |