2323import org .springframework .aop .framework .ProxyFactory ;
2424import org .springframework .aop .support .NameMatchMethodPointcutAdvisor ;
2525import org .springframework .beans .BeansException ;
26+ import org .springframework .beans .factory .Aware ;
27+ import org .springframework .beans .factory .BeanClassLoaderAware ;
2628import org .springframework .beans .factory .BeanCreationNotAllowedException ;
2729import org .springframework .beans .factory .BeanFactory ;
2830import org .springframework .beans .factory .BeanFactoryAware ;
2931import org .springframework .beans .factory .BeanFactoryUtils ;
32+ import org .springframework .beans .factory .BeanNameAware ;
3033import org .springframework .beans .factory .SmartInitializingSingleton ;
3134import org .springframework .beans .factory .config .BeanDefinition ;
3235import org .springframework .beans .factory .config .BeanDefinitionCustomizer ;
3336import org .springframework .beans .factory .config .BeanPostProcessor ;
3437import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
38+ import org .springframework .beans .factory .config .EmbeddedValueResolver ;
3539import org .springframework .beans .factory .support .AbstractBeanDefinition ;
3640import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
3741import 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 ;
3850import org .springframework .context .SmartLifecycle ;
3951import org .springframework .core .io .DescriptiveResource ;
4052import org .springframework .integration .channel .AbstractMessageChannel ;
6072import org .springframework .util .Assert ;
6173import org .springframework .util .CollectionUtils ;
6274import org .springframework .util .StringUtils ;
75+ import org .springframework .util .StringValueResolver ;
6376
6477/**
6578 * A {@link BeanPostProcessor} to parse {@link IntegrationFlow} beans and
7285 * @since 5.0
7386 */
7487public class IntegrationFlowBeanPostProcessor
75- implements BeanPostProcessor , BeanFactoryAware , SmartInitializingSingleton {
88+ implements BeanPostProcessor , ApplicationContextAware , SmartInitializingSingleton {
89+
90+ private ConfigurableApplicationContext applicationContext ;
91+
92+ private StringValueResolver embeddedValueResolver ;
7693
7794 private ConfigurableListableBeanFactory beanFactory ;
7895
7996 private IntegrationFlowContext flowContext ;
8097
8198 @ 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."
86103 );
87104
88- this .beanFactory = (ConfigurableListableBeanFactory ) beanFactory ;
105+ this .applicationContext = (ConfigurableApplicationContext ) applicationContext ;
106+ this .beanFactory = this .applicationContext .getBeanFactory ();
107+ this .embeddedValueResolver = new EmbeddedValueResolver (this .beanFactory );
89108 this .flowContext = this .beanFactory .getBean (IntegrationFlowContext .class );
90109 Assert .notNull (this .flowContext , "There must be an IntegrationFlowContext in the application context" );
91110 }
@@ -99,7 +118,7 @@ else if (bean instanceof IntegrationFlow) {
99118 return processIntegrationFlowImpl ((IntegrationFlow ) bean , beanName );
100119 }
101120 if (bean instanceof IntegrationComponentSpec ) {
102- processIntegrationComponentSpec ((IntegrationComponentSpec <?, ?>) bean );
121+ processIntegrationComponentSpec (beanName , (IntegrationComponentSpec <?, ?>) bean );
103122 }
104123 return bean ;
105124 }
@@ -316,7 +335,11 @@ private Object processIntegrationFlowImpl(IntegrationFlow flow, String beanName)
316335 }
317336 }
318337
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+
320343 if (bean instanceof ComponentsRegistration ) {
321344 Map <Object , String > componentsToRegister = ((ComponentsRegistration ) bean ).getComponentsToRegister ();
322345 if (!CollectionUtils .isEmpty (componentsToRegister )) {
@@ -335,6 +358,38 @@ private void processIntegrationComponentSpec(IntegrationComponentSpec<?, ?> bean
335358 }
336359 }
337360
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+
338393 private void registerComponent (Object component , String beanName ) {
339394 registerComponent (component , beanName , null );
340395 }
0 commit comments