Я использую Spring Framework 3.2.6.RELEASE.Как инициализировать свойство-заполнителя с помощью среды, прикрепленной к текущему контексту приложения весной?
Я пытаюсь установить свойство, используя интерфейс командной строки (используя JOptCommandLinePropertySource) через
PropertySource<?> propertySource = new JOptCommandLinePropertySource(options);
final GenericApplicationContext context = new GenericApplicationContext();
context.getEnvironment().getPropertySources().addFirst(propertSource);
...
У меня есть эта фасоль 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;
}
}
я начинаю свою программу с аргументом командной строки : --prop1 = prop_value
Если я инициализирую этот xml:
<beans>
<context:annotation-config />
<context:property-placeholder />
<context:component-scan base-package="com.example" />
</beans>
Тогда я получаю эту ошибку: Не удалось разрешить заполнитель 'prop1' в строковое значение "$ {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]
...
Но с этим XML все работает отлично:
<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>
Почему PropertySourcesPlaceholderConfigurer не проверяет текущую среду контекста приложения, как она определена в 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.
Какую версию весны вы используете? –
Spring Framework 3.2.6.RELEASE –
Какую версию xsd вы пытаетесь использовать в XML-файле? Другими словами, разместите заголовок своего файла конфигурации. –