Bhaskar Verma
Bhaskar Verma

Reputation: 29

PropertyPlaceHolderConfigurer Alternative

My code is in PropertyPlaceHolderConfigurer and it is depreceated .Any alternative. Code is below -

public void init(Properties properties) {

    // SPECIFIC properties (database connection, handlers)
    if (properties != null) {
          ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] { 
                 CONTEXT }, false);
             PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();--Problem 
                                                                                                here
           configurer.setProperties(properties);
      
           context.addBeanFactoryPostProcessor(configurer);
            context.refresh();
           this.DefinitionDao = (DefinitionDao) 
           context.getBean("DefinitionDao");
            this.logger = (Logger) context.getBean("Logger");
    }

Upvotes: 2

Views: 9854

Answers (2)

Bojan Antonović
Bojan Antonović

Reputation: 11

Just use:

final var configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setProperties(properties);

Side note: It even works as a replacement for configuring properties over the XML configuration. See https://github.com/bojanantonovic/spring-properties-demo/blob/master/src/main/resources/my-config.xml.

Upvotes: 1

M. Deinum
M. Deinum

Reputation: 125312

If you read the javadocs that tells you what the replacement is.

Deprecated. as of 5.2; use org.springframework.context.support.PropertySourcesPlaceholderConfigurer instead which is more flexible through taking advantage of the Environment and PropertySource mechanisms.

In short use the PropertySourcesPlaceholderConfigurer.

Rule of thumb for the next deprecation you encounter, generally the replacement is mentioned in the java docs of the deprecated class.

Upvotes: 1

Related Questions