Juan Reina Pascual
Juan Reina Pascual

Reputation: 4588

Rewriting spring context from xml to java

I'm rewriting my Spring context from XML to Java class, but this bean below I don't know. Can anyone help me? I'm using Spring Boot.

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="order" value="10"/>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property 
              name="properties" ref="externalConfigProperties">
    </property>
</bean>

Upvotes: 0

Views: 231

Answers (3)

M. Deinum
M. Deinum

Reputation: 124471

For newer versions of Spring it is recommended to use a PropertySourcesPlaceholderConfigurer instead of a PropertyPlaceholderConfigurer. When defining a BeanFactoryPostProcessor it should be registered as a public static @Bean method as mentioned in the docs.

To load your properties add a @PropertySource pointing to the location of your properties file.

@Configuration
@PropertySource("path/to/your/config.properties")
public class Config {

    @Bean
    public static PropertySourcePlaceholderConfigurer configurer(){
        PropertySourcePlaceholderConfigurer configurer = new PropertySourcePlaceholderConfigurer();            
        configurer.setOrder(10);
        configurer.setIgnoreUnresolvablePlaceholders(true);         
        return configurer;
    }   
}

Upvotes: 1

David Lavender
David Lavender

Reputation: 8311

Along these lines:

@Bean
@Autowired
public PropertyPlaceholderConfigurer properties(Properties externalProperties) {
    PropertyPlaceholderConfigurer propertiesBean = new PropertyPlaceholderConfigurer();
    propertiesBean.setIgnoreUnresolvablePlaceholders(true);
    propertiesBean.setSystemPropertiesModeName("SYSTEM_PROPERTIES_MODE_OVERRIDE");
    propertiesBean.setOrder("10");
    propertiesBean.setProperties(externalProperties);
    return propertiesBean;
}

Upvotes: 0

Harsh Poddar
Harsh Poddar

Reputation: 2554

Try this:

@Configuration
public class Config {

    @Autowired Properties externalConfigProperties;

    @Bean
    PropertyPlaceholderConfigurer configurer(){
        PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
        configurer.setSystemPropertiesModeName("SYSTEM_PROPERTIES_MODE_OVERRIDE");
        configurer.setOrder(10);
        configurer.setIgnoreUnresolvablePlaceholders(true);
        configurer.setProperties(externalConfigProperties);
        return configurer;
    }

}

Upvotes: 2

Related Questions