2014-01-23 1 views
0

스프링 프레임 워크 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.

+0

어떤 봄 버전을 사용하고 있습니까? –

+0

스프링 프레임 워크 3.2.6.RELEASE –

+1

xml 파일에서 사용하는 xsd 버전은 무엇입니까? 즉, 구성 파일의 헤더를 게시하십시오. –

답변

2

네임 스페이스 구성을 사용할 때 버전이없는 xsd를 사용하는 것이 좋습니다. 따라서 http://www.springframework.org/schema/context/spring-context-2.5.xsd 대신 http://www.springframework.org/schema/context/spring-context.xsd을 사용하는 것이 좋습니다. 이렇게하면 스프링이 클래스 패스에서 사용할 수있는 최신 버전의 xsd를 사용하게됩니다.

<context:property-placeholder />과 관련하여, 기본 설정과 관련하여 Spring 3.1에서 변경된 부분 (?)이 Spring 3.1에서 변경되었습니다. Spring 3.1 이전의 system-properties-mode 속성의 기본값은 FALLBACK이었으며, Spring 3.1에서는 PropertySourcesPlaceholderConfigurer 대신에 PropertyPlaceholderConfigurer이 생성되었습니다. 기본값은 특정 xsd에서 구성됩니다. (스프링 3.1처럼 그것은 환경입니다).

따라서 오래된 xsd를 사용하면 xsd의 해당 특정 버전에 속하는 기본 동작으로 이어지며 3.2 xsd 또는 버전없는 xsd로 전환됩니다. (언급 한 바와 같이 후자가 권장됩니다).

관련 문제