1616 */
1717package org .apache .rocketmq .spring .annotation ;
1818
19+ import java .lang .reflect .AnnotatedElement ;
20+ import java .util .List ;
21+ import java .util .Map ;
22+ import java .util .function .BiFunction ;
23+ import java .util .stream .Collectors ;
1924import org .apache .rocketmq .spring .support .RocketMQMessageListenerContainerRegistrar ;
2025import org .springframework .aop .support .AopUtils ;
2126import org .springframework .beans .BeansException ;
22- import org .springframework .beans .factory .InitializingBean ;
27+ import org .springframework .beans .factory .ObjectProvider ;
2328import org .springframework .beans .factory .config .BeanPostProcessor ;
24- import org .springframework .context .ApplicationContext ;
25- import org .springframework .context .ApplicationContextAware ;
2629import org .springframework .context .SmartLifecycle ;
2730import org .springframework .core .OrderComparator ;
2831import org .springframework .core .annotation .AnnotationUtils ;
2932
30- import java .lang .reflect .AnnotatedElement ;
31- import java .util .List ;
32- import java .util .Map ;
33- import java .util .function .BiFunction ;
34- import java .util .stream .Collectors ;
35-
36- public class RocketMQMessageListenerBeanPostProcessor implements ApplicationContextAware , BeanPostProcessor , InitializingBean , SmartLifecycle {
37-
38- private ApplicationContext applicationContext ;
33+ public class RocketMQMessageListenerBeanPostProcessor implements BeanPostProcessor , SmartLifecycle {
3934
4035 private AnnotationEnhancer enhancer ;
4136
42- private RocketMQMessageListenerContainerRegistrar listenerContainerRegistrar ;
37+ private final ObjectProvider < RocketMQMessageListenerContainerRegistrar > registrarObjectProvider ;
4338
4439 private boolean running = false ;
4540
41+ public RocketMQMessageListenerBeanPostProcessor (List <AnnotationEnhancer > enhancers ,
42+ ObjectProvider <RocketMQMessageListenerContainerRegistrar > provider ) {
43+ List <AnnotationEnhancer > sortedEnhancers = enhancers
44+ .stream ()
45+ .sorted (new OrderComparator ())
46+ .collect (Collectors .toList ());
47+ this .enhancer = (attrs , element ) -> {
48+ Map <String , Object > newAttrs = attrs ;
49+ for (AnnotationEnhancer enh : sortedEnhancers ) {
50+ newAttrs = enh .apply (newAttrs , element );
51+ }
52+ return attrs ;
53+ };
54+ registrarObjectProvider = provider ;
55+ }
56+
4657 @ Override
4758 public Object postProcessBeforeInitialization (Object bean , String beanName ) throws BeansException {
4859 return bean ;
@@ -54,9 +65,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
5465 RocketMQMessageListener ann = targetClass .getAnnotation (RocketMQMessageListener .class );
5566 if (ann != null ) {
5667 RocketMQMessageListener enhance = enhance (targetClass , ann );
57- if (listenerContainerRegistrar != null ) {
58- listenerContainerRegistrar .registerContainer (beanName , bean , enhance );
59- }
68+ registrarObjectProvider .ifAvailable (registrar -> registrar .registerContainer (beanName , bean , enhance ));
6069 }
6170 return bean ;
6271 }
@@ -70,7 +79,7 @@ public int getPhase() {
7079 public void start () {
7180 if (!isRunning ()) {
7281 this .setRunning (true );
73- listenerContainerRegistrar . startContainer ( );
82+ registrarObjectProvider . ifAvailable ( RocketMQMessageListenerContainerRegistrar :: startContainer );
7483 }
7584 }
7685
@@ -83,49 +92,18 @@ public void setRunning(boolean running) {
8392 this .running = running ;
8493 }
8594
86-
8795 @ Override
8896 public boolean isRunning () {
8997 return running ;
9098 }
9199
92- @ Override
93- public void setApplicationContext (ApplicationContext applicationContext ) throws BeansException {
94- this .applicationContext = applicationContext ;
95- }
96-
97- @ Override
98- public void afterPropertiesSet () throws Exception {
99- buildEnhancer ();
100- this .listenerContainerRegistrar = this .applicationContext .getBean (RocketMQMessageListenerContainerRegistrar .class );
101- }
102-
103- private void buildEnhancer () {
104- if (this .applicationContext != null ) {
105- Map <String , AnnotationEnhancer > enhancersMap =
106- this .applicationContext .getBeansOfType (AnnotationEnhancer .class , false , false );
107- if (enhancersMap .size () > 0 ) {
108- List <AnnotationEnhancer > enhancers = enhancersMap .values ()
109- .stream ()
110- .sorted (new OrderComparator ())
111- .collect (Collectors .toList ());
112- this .enhancer = (attrs , element ) -> {
113- Map <String , Object > newAttrs = attrs ;
114- for (AnnotationEnhancer enh : enhancers ) {
115- newAttrs = enh .apply (newAttrs , element );
116- }
117- return attrs ;
118- };
119- }
120- }
121- }
122-
123100 private RocketMQMessageListener enhance (AnnotatedElement element , RocketMQMessageListener ann ) {
124101 if (this .enhancer == null ) {
125102 return ann ;
126- } else {
103+ }
104+ else {
127105 return AnnotationUtils .synthesizeAnnotation (
128- this .enhancer .apply (AnnotationUtils .getAnnotationAttributes (ann ), element ), RocketMQMessageListener .class , null );
106+ this .enhancer .apply (AnnotationUtils .getAnnotationAttributes (ann ), element ), RocketMQMessageListener .class , null );
129107 }
130108 }
131109
0 commit comments