Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring placeholder doesn't resolve properties in JavaConfig

Currently i have a Spring xml configuration (Spring 4) which load a properties file.

context.properties

my.app.service = myService
my.app.other = ${my.app.service}/sample

Spring xml configuration

<bean id="contextProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="fileEncoding" value="UTF-8" />
    <property name="locations">
        <list>
            <value>classpath:context.properties</value>
        </list>
    </property>
</bean>
<bean id="placeholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="properties" ref="contextProperties" />
    <property name="nullValue" value="@null" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</bean>

Bean which use the properties

@Component
public class MyComponent {

    @Value("${my.app.other}")
    private String others;

}

This works perfectly and the others value is MyService/sample, as excepted. But when i try to replace this configuration by a JavaConfig, the @Value in my component doesn't works in the same way. the value isn't myService/sample but ${my.app.service}/sample.

@Configuration
@PropertySource(name="contextProperties", ignoreResourceNotFound=true, value={"classpath:context.properties"})
public class PropertiesConfiguration {

    @Bean
    public static PropertyPlaceholderConfigurer placeholder() throws IOException {
        PropertyPlaceholderConfigurer placeholder = new PropertyPlaceholderConfigurer();
        placeholder.setNullValue("@null");
        placeholder.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
        return placeholder;
    }

}

Did i miss something in the conversion from xml to Javaconfig ?

ps : I also try to instantiate a PropertySourcesPlaceholderConfigurer instead of a PropertyPlaceholderConfigurer without more success.

like image 252
herau Avatar asked Mar 19 '15 09:03

herau


People also ask

What is a property placeholder in Spring Boot?

Usually represented as an instance of org.springframework.beans.factory.config.PropertyPlaceholderConfigurer class, the properties placeholder is an object able to retreive the values of defined properties directly in Spring's application context.

What does it mean when a properties placeholder returns a value?

It means that if you define a properties placeholder and you are looking for an properties variable, Spring will check if this variable doesn't exist in properties placeholder specified files. If it exists, its value will be returned.

How does spring spring configproperties bind to the hostname?

Spring will automatically bind any property defined in our property file that has the prefix mail and the same name as one of the fields in the ConfigProperties class. Spring uses some relaxed rules for binding properties. As a result, the following variations are all bound to the property hostName:

What is a properties variable in Spring Boot?

It means that if you define a properties placeholder and you are looking for an properties variable, Spring will check if this variable doesn't exist in properties placeholder specified files. If it exists, its value will be returned. An variable looks like PHP's concatenated variables into Strings, ie: $ {variableName}.


1 Answers

Update to use configure PropertySourcesPlaceholderConfigurer. Just having @PropertySource annotation will not be sufficient:

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
    return new PropertySourcesPlaceholderConfigurer();
}

@PropertySource annotation does not automatically register a PropertySourcesPlaceholderConfigurer with Spring. Hence we need to explicitly configure PropertySourcesPlaceholderConfigurer

Below JIRA ticket has more information on the rationale behind this design:

https://jira.spring.io/browse/SPR-8539

UPDATE: Created simple Spring boot application to use nested properties. It is working fine with the above configuration.

https://github.com/mgooty/property-configurer/tree/master/complete

like image 162
Mithun Avatar answered Sep 27 '22 20:09

Mithun