Skip to content

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

Closed
@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions