You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have one java config class called FactoryBeanConfig which tries to extract properties value by using @Value annotation. However, it does not work when I use @ImportResource + AnnotationConfigApplicationContext. So, I change the main class to start Spring by ClassPathXmlApplicationContext + context:component-scan, and it works.
Can AnnotationConfigApplicationContext/JavaConfig support @Value to extract value from PropertyPlaceholderConfigurer?
My Test case, it is similar to the test case of #11268. @Configuration @ImportResource("classpath:propertiesConfig.xml")
public class FactoryBeanConfig { @Bean
public C3 c3() throws Exception { C3 c = new C3(); System.out.println("******* test=" + test); return c; }
Main.java, @Value cannot get value.
ApplicationContext context = new AnnotationConfigApplicationContext(FactoryBeanConfig.class);
However, when I use ClassPathXmlApplicationContext+context:component-scan, @Value works:
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
The problem, which caused the reported problem, is as it follows:
The AnnotationConfigApplicationContext does not consider the following beans, as possible BeanFactoryPostProcessor beans:
which are defined in its @Configuration components through the @Bean annotation
which are imported through @Import respectively through @ImportResource
Just the basic implementation of the invokeBeanFactoryPostProcessors( beanFactory ) method is used for AnnotationConfigApplicationContext, which is implemented in its parent AbstractApplicationConfig. This ignores the above mentioned beans.
Maybe the invokeBeanFactoryPostProcessors( beanFactory ) should consider beans provided by the @Configuration components also!
The current documentation of Spring 3.0 is confusing as it describes a non working behavior. I mean the last part of chapter 3.11.3.2, which describes the possibility of importing PropertyPlaceholderConfigurer to the AppConfig java-configuration.
Fixed through introducing a BeanDefinitionRegistryPostProcessor which gets invoked against the BeanDefinitionRegistry - before BeanFactoryPostProcessors get retrieved and applied. Any bean definitions registered by a BeanDefinitionRegistryPostProcessor are eligible for being run as BeanFactoryPostProcessors in the next phase. ConfigurationClassPostProcessors takes advantage of exactly that benefit, processing @Bean method in the BeanDefinitionRegistryPostProcessor phase now.
Liu, Yinwei David opened SPR-6611 and commented
I have one java config class called FactoryBeanConfig which tries to extract properties value by using
@Value
annotation. However, it does not work when I use@ImportResource
+ AnnotationConfigApplicationContext. So, I change the main class to start Spring by ClassPathXmlApplicationContext + context:component-scan, and it works.Can AnnotationConfigApplicationContext/JavaConfig support
@Value
to extract value from PropertyPlaceholderConfigurer?My Test case, it is similar to the test case of #11268.
@Configuration
@ImportResource
("classpath:propertiesConfig.xml")public class FactoryBeanConfig {
@Bean
public C3 c3() throws Exception { C3 c = new C3(); System.out.println("******* test=" + test); return c; }
@Value
("${test}") public String test;}
propertiesConfig.xml
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:myprop.properties"/>
</bean>
Main.java,
@Value
cannot get value.ApplicationContext context = new AnnotationConfigApplicationContext(FactoryBeanConfig.class);
However, when I use ClassPathXmlApplicationContext+context:component-scan,
@Value
works:ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
context.xml:
<import resource="classpath:spring3/config/propertiesConfig.xml"/>
<context:component-scan base-package="config"/>
Affects: 3.0 GA
Issue Links:
Referenced from: commits 8ab9da4, 6b2b5c4
1 votes, 2 watchers
The text was updated successfully, but these errors were encountered: