2013-11-28 6 views
0

나는 다음과 같이 PropertyPlaceholderConfigurer와를 사용하여 기존 XML 기반의 봄 구성이 :봄 @PropertySource 및 환경 유형 변환

myprops.value=classpath:configFile.xml와 'someValue와'속성에 대한 세터가 org.springframework.core을 받아
<context:property-placeholder location="classpath:my.properties" /> 

    <bean id="myBean" class="com.whatever.TestBean"> 
    <property name="someValue" value="${myProps.value}" /> 
    </bean> 

경우 .io.Resource.

이 작동합니다. PPC는 문자열 값과 리소스 사이를 자동으로 변환합니다. 내가 지금 다음과 같이 자바 구성과 @PropertySource 주석을 사용하려고 해요

:

@Configuration 
@PropertySource("classpath:my.properties") 
public class TestConfig { 

    @Autowired Environment environment; 

    @Bean 
    public TestBean testBean() throws Exception { 
     TestBean testBean = new TestBean(); 
     testBean.setSomeValue(environment.getProperty("myProps.value", Resource.class)); 
     return testBean; 
    } 

} 

봄 환경 클래스의 getProperty에() 메소드는 다른 유형의 변환을 지원하는 오버로드를 제공하는 I '그러나 이것은 자원에 속성을 변환 기본 지원으로하지 않습니다, 사용했습니다 :

Caused by: java.lang.IllegalArgumentException: Cannot convert value [classpath:configFile.xml] from source type [String] to target type [Resource] 
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:81) 
    at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:370) 
    at config.TestConfig.testBean(TestConfig.java:19) 

기본 소스 코드를 보면, 환경 구현 차례로 DefaultConversionService를 사용하는 PropertySourcesPropertyResolver를 사용 이것은 매우 기본적인 변환기 만 등록합니다.

그래서 두 가지 질문이 있습니다.
1) 어떻게 리소스로의 변환을 지원할 수 있습니까?
2) 원래 PPC가이 작업을 수행해야하는 이유는 무엇입니까?

답변

0

다음과 같이 해결했습니다.

리소스 번들에서 속성을 가져온 다음 bean에서 속성을 설정하는 것과 구분된다는 사실을 깨달았습니다. Spring은 관련 PropertyEditor (ResourceEditor)를 사용하여 속성을 설정하는 동안 변환을 수행합니다. 그래서 우리는이 단계를 수동으로 수행해야 :

@Configuration 
@PropertySource("classpath:my.properties") 
public class TestConfig { 

    @Autowired Environment environment; 

    @Bean 
    public TestBean testBean() throws Exception { 
     ResourceEditor editor = new ResourceEditor(); 
     editor.setAsText(environment.getProperty("myProps.value")); 
     TestBean testBean = new TestBean(); 
     testBean.setSomeValue((Resource)editor.getValue()); 
     return testBean; 
    } 

} 

그러나이 환경에 의해 내부적으로 사용되는 DefaultConversionService 자동으로 PropertyEditor를 데리러하지 않을 이유의 뛰어난 질문을 떠나지 않습니다. 이와 함께 할 수 : - 당신이이 XML에 해당 말한대로

https://jira.springsource.org/browse/SPR-6564

0

동일한 문제가 발생하여 context:property-placeholder은 속성 파일을로드 할뿐만 아니라 모든 파일을 처리하는 특수 콩 org.springframework.context.support.PropertySourcesPlaceholderConfigurer을 선언합니다. ${...} 속성을 해결하고이를 바꿉니다. 당신이 @PropertySource("classpath:my.properties") 주석을 제거해야한다는,

@Configuration 
public class TestConfig { 

    @Autowired Environment environment; 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer configurer(){ 
     PropertySourcesPlaceholderConfigurer postProcessor = new PropertySourcesPlaceholderConfigurer(); 
     postProcessor.setLocation(new ClassPathResource("my.properties")); 
     return postProcessor; 
    } 

... 

참고 :

를 고정 당신은 단순히 해당 인스턴스를 만들어야합니다.

+0

감사 n1ckolas, 내가 전에 이런 식으로 일을 보았다. 그러나 이것이 바로 @PropertySource 주석이 피하기로되어있는 것이라고 생각했습니다. –