@Value not resolved when using @PropertySource annotation. How to configure PropertySourcesPlaceholderConfigurer?
as @cwash said;
@Configuration
@PropertySource("classpath:/test-config.properties")
public class TestConfig {
@Value("${name}")
public String name;
//You need this
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
If you use @PropertySource, properties have to be retrieved with:
@Autowired
Environment env;
// ...
String subject = env.getProperty("mail.subject");
If you want to retrieve with @Value("${mail.subject}"), you have to register the prop placeholder by xml.
Reason: https://jira.springsource.org/browse/SPR-8539
I found the reason @value
was not working for me is, @value
requires PropertySourcesPlaceholderConfigurer
instead of a PropertyPlaceholderConfigurer
. I did the same changes and it worked for me, I am using spring 4.0.3 release. I configured this using below code in my configuration file.
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}