나는 다음과 같이 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가이 작업을 수행해야하는 이유는 무엇입니까?
감사 n1ckolas, 내가 전에 이런 식으로 일을 보았다. 그러나 이것이 바로 @PropertySource 주석이 피하기로되어있는 것이라고 생각했습니다. –