Spring placeholder does not allow properties in JavaConfig

I currently have a Spring xml configuration (Spring 4) that load the 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 that use properties

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

This works fine, and the value of others MyService/sample is excluded. But when I try to replace this JavaConfig configuration, @Value in my component does not work like that. the value is not 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; } } 

Am I missing something in the conversion from xml to Javaconfig?

ps: I am also trying to create an instance of PropertySourcesPlaceholderConfigurer instead of PropertyPlaceholderConfigurer without any success.

+7
java spring spring-java-config properties-file
source share
2 answers

Update to use configure PropertySourcesPlaceholderConfigurer . Just having the @PropertySource annotation @PropertySource be insufficient:

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

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

Below the JIRA ticket there is more information about the rationale for this design:

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

UPDATE: Created a simple Spring boot application to use nested properties. It works great with the above configuration.

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

+3
source share

Another option is to import PropertyPlaceholderAutoConfiguration.class.

 import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; @Import(PropertyPlaceholderAutoConfiguration.class) 

Annotations include PropertySourcesPlaceholderConfigurer in context if it does not exist.

+1
source share

All Articles