23
23
import org .springframework .aop .framework .ProxyFactory ;
24
24
import org .springframework .aop .support .NameMatchMethodPointcutAdvisor ;
25
25
import org .springframework .beans .BeansException ;
26
+ import org .springframework .beans .factory .Aware ;
27
+ import org .springframework .beans .factory .BeanClassLoaderAware ;
26
28
import org .springframework .beans .factory .BeanCreationNotAllowedException ;
27
29
import org .springframework .beans .factory .BeanFactory ;
28
30
import org .springframework .beans .factory .BeanFactoryAware ;
29
31
import org .springframework .beans .factory .BeanFactoryUtils ;
32
+ import org .springframework .beans .factory .BeanNameAware ;
30
33
import org .springframework .beans .factory .SmartInitializingSingleton ;
31
34
import org .springframework .beans .factory .config .BeanDefinition ;
32
35
import org .springframework .beans .factory .config .BeanDefinitionCustomizer ;
33
36
import org .springframework .beans .factory .config .BeanPostProcessor ;
34
37
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
38
+ import org .springframework .beans .factory .config .EmbeddedValueResolver ;
35
39
import org .springframework .beans .factory .support .AbstractBeanDefinition ;
36
40
import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
37
41
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
42
+ import org .springframework .context .ApplicationContext ;
43
+ import org .springframework .context .ApplicationContextAware ;
44
+ import org .springframework .context .ApplicationEventPublisherAware ;
45
+ import org .springframework .context .ConfigurableApplicationContext ;
46
+ import org .springframework .context .EmbeddedValueResolverAware ;
47
+ import org .springframework .context .EnvironmentAware ;
48
+ import org .springframework .context .MessageSourceAware ;
49
+ import org .springframework .context .ResourceLoaderAware ;
38
50
import org .springframework .context .SmartLifecycle ;
39
51
import org .springframework .core .io .DescriptiveResource ;
40
52
import org .springframework .integration .channel .AbstractMessageChannel ;
60
72
import org .springframework .util .Assert ;
61
73
import org .springframework .util .CollectionUtils ;
62
74
import org .springframework .util .StringUtils ;
75
+ import org .springframework .util .StringValueResolver ;
63
76
64
77
/**
65
78
* A {@link BeanPostProcessor} to parse {@link IntegrationFlow} beans and
72
85
* @since 5.0
73
86
*/
74
87
public class IntegrationFlowBeanPostProcessor
75
- implements BeanPostProcessor , BeanFactoryAware , SmartInitializingSingleton {
88
+ implements BeanPostProcessor , ApplicationContextAware , SmartInitializingSingleton {
89
+
90
+ private ConfigurableApplicationContext applicationContext ;
91
+
92
+ private StringValueResolver embeddedValueResolver ;
76
93
77
94
private ConfigurableListableBeanFactory beanFactory ;
78
95
79
96
private IntegrationFlowContext flowContext ;
80
97
81
98
@ Override
82
- public void setBeanFactory ( BeanFactory beanFactory ) throws BeansException {
83
- Assert .isInstanceOf (ConfigurableListableBeanFactory .class , beanFactory ,
84
- "To use Spring Integration Java DSL the 'beanFactory ' has to be an instance of " +
85
- "'ConfigurableListableBeanFactory '. Consider using 'GenericApplicationContext' implementation."
99
+ public void setApplicationContext ( ApplicationContext applicationContext ) throws BeansException {
100
+ Assert .isInstanceOf (ConfigurableApplicationContext .class , applicationContext ,
101
+ "To use Spring Integration Java DSL the 'applicationContext ' has to be an instance of " +
102
+ "'ConfigurableApplicationContext '. Consider using 'GenericApplicationContext' implementation."
86
103
);
87
104
88
- this .beanFactory = (ConfigurableListableBeanFactory ) beanFactory ;
105
+ this .applicationContext = (ConfigurableApplicationContext ) applicationContext ;
106
+ this .beanFactory = this .applicationContext .getBeanFactory ();
107
+ this .embeddedValueResolver = new EmbeddedValueResolver (this .beanFactory );
89
108
this .flowContext = this .beanFactory .getBean (IntegrationFlowContext .class );
90
109
Assert .notNull (this .flowContext , "There must be an IntegrationFlowContext in the application context" );
91
110
}
@@ -99,7 +118,7 @@ else if (bean instanceof IntegrationFlow) {
99
118
return processIntegrationFlowImpl ((IntegrationFlow ) bean , beanName );
100
119
}
101
120
if (bean instanceof IntegrationComponentSpec ) {
102
- processIntegrationComponentSpec ((IntegrationComponentSpec <?, ?>) bean );
121
+ processIntegrationComponentSpec (beanName , (IntegrationComponentSpec <?, ?>) bean );
103
122
}
104
123
return bean ;
105
124
}
@@ -316,7 +335,11 @@ private Object processIntegrationFlowImpl(IntegrationFlow flow, String beanName)
316
335
}
317
336
}
318
337
319
- private void processIntegrationComponentSpec (IntegrationComponentSpec <?, ?> bean ) {
338
+ private void processIntegrationComponentSpec (String beanName , IntegrationComponentSpec <?, ?> bean ) {
339
+ Object target = bean .get ();
340
+
341
+ invokeBeanInitializationHooks (beanName , target );
342
+
320
343
if (bean instanceof ComponentsRegistration ) {
321
344
Map <Object , String > componentsToRegister = ((ComponentsRegistration ) bean ).getComponentsToRegister ();
322
345
if (!CollectionUtils .isEmpty (componentsToRegister )) {
@@ -335,6 +358,38 @@ private void processIntegrationComponentSpec(IntegrationComponentSpec<?, ?> bean
335
358
}
336
359
}
337
360
361
+ private void invokeBeanInitializationHooks (final String beanName , final Object bean ) {
362
+ if (bean instanceof Aware ) {
363
+ if (bean instanceof BeanNameAware ) {
364
+ ((BeanNameAware ) bean ).setBeanName (beanName );
365
+ }
366
+ if (bean instanceof BeanClassLoaderAware ) {
367
+ ((BeanClassLoaderAware ) bean ).setBeanClassLoader (this .beanFactory .getBeanClassLoader ());
368
+ }
369
+ if (bean instanceof BeanFactoryAware ) {
370
+ ((BeanFactoryAware ) bean ).setBeanFactory (this .beanFactory );
371
+ }
372
+ if (bean instanceof EnvironmentAware ) {
373
+ ((EnvironmentAware ) bean ).setEnvironment (this .applicationContext .getEnvironment ());
374
+ }
375
+ if (bean instanceof EmbeddedValueResolverAware ) {
376
+ ((EmbeddedValueResolverAware ) bean ).setEmbeddedValueResolver (this .embeddedValueResolver );
377
+ }
378
+ if (bean instanceof ResourceLoaderAware ) {
379
+ ((ResourceLoaderAware ) bean ).setResourceLoader (this .applicationContext );
380
+ }
381
+ if (bean instanceof ApplicationEventPublisherAware ) {
382
+ ((ApplicationEventPublisherAware ) bean ).setApplicationEventPublisher (this .applicationContext );
383
+ }
384
+ if (bean instanceof MessageSourceAware ) {
385
+ ((MessageSourceAware ) bean ).setMessageSource (this .applicationContext );
386
+ }
387
+ if (bean instanceof ApplicationContextAware ) {
388
+ ((ApplicationContextAware ) bean ).setApplicationContext (this .applicationContext );
389
+ }
390
+ }
391
+ }
392
+
338
393
private void registerComponent (Object component , String beanName ) {
339
394
registerComponent (component , beanName , null );
340
395
}
0 commit comments