I'm using Spring Framework 3.2.6.RELEASE.
I'm trying to set properties using command line interface (using JOptCommandLinePropertySource) via
PropertySource<?> propertySource = new JOptCommandLinePropertySource(options);
final GenericApplicationContext context = new GenericApplicationContext();
context.getEnvironment().getPropertySources().addFirst(propertSource);
...
I have this beans configurer:
package com.example;
@Configuration
public class AppConfig {
@Value("${prop1}")
private String prop1;
@Bean
public MyBean myBean() {
MyBean ret = new MyBean();
ret.init(prop1);
return ret;
}
}
I'm starting my program with command line argument: --prop1=prop_value
If I do initialization with this xml:
<beans>
<context:annotation-config />
<context:property-placeholder />
<context:component-scan base-package="com.example" />
</beans>
Then I get this error: Could not resolve placeholder 'prop1' in string value "${prop1}"
13:47:36.932 [main] DEBUG o.s.b.f.annotation.InjectionMetadata - Processing injected method of bean 'AppConfig': AutowiredFieldElement for private java.lang.String com.example.AppConfig.prop1
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'AppConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.example.AppConfig.prop1; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'prop1' in string value "${prop1}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) ~[spring-beans-3.2.6.RELEASE.jar:3.2.6.RELEASE]
...
But with this xml all works fine:
<beans>
<context:annotation-config />
<context:component-scan base-package="com.example" />
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="environment" ref="environment" />
</bean>
</beans>
Why PropertySourcesPlaceholderConfigurer does not check current application context's environment as it defined in Javadoc?
Specialization of PlaceholderConfigurerSupport that resolves ${...} placeholders within bean definition property values and @Value annotations against the current Spring Environment and its set of PropertySources.