I have an existing xml-based spring configuration using a PropertyPlaceholderConfigurer as follows:
<context:property-placeholder location="classpath:my.properties" />
<bean id="myBean" class="com.whatever.TestBean">
<property name="someValue" value="${myProps.value}" />
</bean>
Where myprops.value=classpath:configFile.xml
and the setter for 'someValue' property accepts a org.springframework.core.io.Resource.
This works fine - the PPC will convert between the String value and the Resource automatically.
I'm now trying to use Java Config and the @PropertySource annotation as follows:
@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;
}
}
The getProperty() method of the Spring Environment class provides an overload to support conversion to different types, which I've used, however this doesn't by default support converting the property to a Resource:
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)
Looking at the underlying source code, the Environment implementation uses a PropertySourcesPropertyResolver, which in turn uses a DefaultConversionService and this only registers very basic converters.
So I have two questions:
1) How can I get this to support the conversion to Resource?
2) Why should I need to when the original PPC does this for me?