Skip to content

@Value does not work with AnnotationConfigApplicationContext [SPR-6611] #11277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Dec 23, 2009 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Dec 23, 2009

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

@spring-projects-issues
Copy link
Collaborator Author

Gabriel Forro commented

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.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

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.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Liu, Yinwei David commented

Thanks Juergen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants