Reputation: 591
I have a simple question about PropertySourcesPlaceholderConfigurer
and @PropertySource
annotation. I have simple class with bean. I would like to inject property from application.properties
and some another test.properties
wiht @Value
annotation. I read in several sources that @PropertySource
needs to define staticPropertySourcesPlaceholderConfigurer
bean. From documentation:
In order to resolve ${...} placeholders in definitions or @Value annotations using properties from a PropertySource, one must register a PropertySourcesPlaceholderConfigurer. This happens automatically when using in XML, but must be explicitly registered using a static @Bean method when using @Configuration classes.
But for me it works fine without this bean. Is Spring in some way auto configures PropertySourcesPlaceholderConfigurer
at application startup? Here is my simple example (first property is from application.properties
and second from test.properties
):
@Configuration
@PropertySource("classpath:test.properties")
public class AppConfiguration {
@Value("${first.property}")
private String firstProp;
@Value("${second.property}")
private String secondProp;
@Bean
public TestModel getModel() {
TestModel model = new TestModel();
model.setFirstProperty(firstProp);
model.setSecondProperty(secondProp);
return model;
}
}
Upvotes: 2
Views: 3973
Reputation: 44942
The static PropertySourcesPlaceholderConfigurer
bean is registered automatically when missing by PropertyPlaceholderAutoConfiguration
class which appears to be introduced in Spring Boot 1.5.0.RELEASE (as there is no on-line javadoc for it in the previous releases).
Interestingly this new auto configuration is not mentioned in Spring Boot 1.5 Release Notes.
Upvotes: 2