I have following configuration file:
@Configuration
public class PropertyPlaceholderConfigurerConfig {
@Value("${property:defaultValue}")
private String property;
@Bean
public static PropertyPlaceholderConfigurer ppc() throws IOException {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(new ClassPathResource("properties/" + property + ".properties"));
ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc;
}
}
I run my application with following VM option:
-Dproperty=propertyValue
So I'd like my application to load specific property file on startup. But for some reason at this stage @Value
annotations are not processed and property is null
. On the other hand if I have PropertyPlaceholderConfigurer
configured via xml file - everything works perfectly as expected. Xml file example:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"/>
<property name="location">
<value>classpath:properties/${property:defaultValue}.properties</value>
</property>
</bean>
If I try to to inject property value in another Spring configuration file - it is properly injected. If I move my PropertyPlaceholderConfigurer
bean creation to that configuration file - field value is null again.
As workaround, I use this line of code:
System.getProperties().getProperty("property", "defaultValue")
Which is also works, but I'd like to know why such behavior is occurs and maybe it is possible to rewrite it in other way but without xml?