I\'m working on an application library with a utility class called \"Config\" which is backed by the Spring Environment object and provides strongl
The following worked for me with Spring 3.2.4 .
PropertySourcesPlaceholderConfigurer must be registered statically in order to process the placeholders.
The custom property source is registered in the init method and as the default property sources are already registered, it can itself be parameterized using placeholders.
JavaConfig class:
@Configuration
@PropertySource("classpath:propertiesTest2.properties")
public class TestConfig {
@Autowired
private ConfigurableEnvironment env;
@Value("${param:NOVALUE}")
private String param;
@PostConstruct
public void init() {
env.getPropertySources().addFirst(new CustomPropertySource(param));
}
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public TestBean1 testBean1() {
return new TestBean1();
}
}
Custom property source:
public class CustomPropertySource extends PropertySource
Test bean (getValue() will output "IT WORKS"):
public class TestBean1 {
@Value("${value:NOVALUE}")
private String value;
public String getValue() {
return value;
}
}