스프링 프레임 워크 3.2.6.RELEASE를 사용하고 있습니다.Spring의 현재 응용 프로그램 컨텍스트에 연결된 환경으로 property-placeholder를 초기화하는 방법은 무엇입니까?
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;
}
}
내가 명령 행 인수와 함께 내 프로그램을 시작 했어 :
나는이 콩의 구성 장치가
PropertySource<?> propertySource = new JOptCommandLinePropertySource(options);
final GenericApplicationContext context = new GenericApplicationContext();
context.getEnvironment().getPropertySources().addFirst(propertSource);
...
를 통해 (JOptCommandLinePropertySource 사용) 명령 줄 인터페이스를 사용하여 속성을 설정하기 위해 노력하고있어 : - prop1 = prop_value
이 xml로 초기화하는 경우 :
그런 다음<beans>
<context:annotation-config />
<context:property-placeholder />
<context:component-scan base-package="com.example" />
</beans>
이 오류를 얻을 : 이
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을 모두 잘 작동 자리 문자열 값 'prop1' "$ {prop1을}"확인할 수 없습니다 :
<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.
어떤 봄 버전을 사용하고 있습니까? –
스프링 프레임 워크 3.2.6.RELEASE –
xml 파일에서 사용하는 xsd 버전은 무엇입니까? 즉, 구성 파일의 헤더를 게시하십시오. –