1. Overview
In this article, we will learn the alternative to deprecated PropertyPlaceholderConfigurer.
It is deprecated as of 5.2 and recommended to use PropertySourcesPlaceholderConfigurer
instead which is more flexible through taking advantage of the Environment
and PropertySource
mechanisms.
2. PropertyPlaceholderConfigurer alternative
PropertySourcesPlaceholderConfigurer is more flexible by taking advantage of the Environment
and PropertySource
mechanisms.
PropertyPlaceholderConfigurer
is still appropriate for use when:
- the spring-context module is not available (Application using the
BeanFactory
API instead of theApplicationContext
). To know the differences between BeanFactory and ApplicationContext, see this article. - Existing configuration makes use of the
systemPropertiesMode
,systemPropertiesModeName
properties. We encourage users to avoid using these settings, and rather configure property source search order through the container’sEnvironment
.
PropertySourcesPlaceholderConfigurer is designed as a general replacement for PropertyPlaceholderConfigurer
in Spring 3.1 applications. The spring-context-3.1 library uses PropertySourcesPlaceholderConfigurer as the default property placeholder.
2.1. PropertySourcesPlaceholderConfigurer Example
For example, the PropertySourcesPlaceholderConfigurer
uses the rabbitmq.properties
as property source and overrides the local properties.
@Bean(name = "propertySourcesConfigurer") public static PropertySourcesPlaceholderConfigurer configurer() { PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setLocation(new ClassPathResource("rabbitmq.properties")); ppc.setLocalOverride(true); ppc.setProperties(ApplicationProperties.getProperties()); return ppc; }
3. Conclusion
To sum up, we have learned the alternative to the deprecated PropertyPlaceholderConfigurer.