@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:
@AutowiredEnvironment 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.
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.
@Beanpublic static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer();}