javaspringspring-bootspring-mvcproperties

Use of PropertySourcesPlaceholderConfigurer?


I am starting to learn Spring annotations. I'm currently using the @PropertySource annotation in my Configuration Class to resolve properties values and everything works fine, but then I read about PropertySourcesPlaceholderConfigurer.

When or why I should use that?


Solution

  • From @PropertySource javadoc

    ...
    Note, however, that explicit registration of a PropertySourcesPlaceholderConfigurer via a static @Bean method is typically only required if you need to customize configuration such as the placeholder syntax, etc. See the "Working with externalized values" section of Configuration @Configuration's javadocs and "a note on BeanFactoryPostProcessor-returning @Bean methods of Bean @Bean's javadocs for details and examples. Specifically, if no bean post-processor (such as a PropertySourcesPlaceholderConfigurer) has registered an embedded value resolver for the ApplicationContext, Spring will register a default embedded value resolver which resolves placeholders against property sources registered in the Environment.
    ...

    Well, in simple words if you need to set up or get more control over a property configuration bean such as PropertySourcesPlaceholderConfigurer you could define it. Otherwise, it can be ommited.But if you use a version of Spring prior 4.3.0, this bean has to be declared for resolve @Value.

    To get more details:

    (I hope it'll help you a little)