In spring JavaConfig, I can define property source and inject into Environment
@PropertySource("classpath:application.properties")
@Inject private Environment environment;
How do I do that if in xml?
I am using context:property-placeholder, and on the JavaConfig class @ImportResource to import the xml. But I cannot retrieve property defined in the properties file using environment.getProperty(“xx”)
<context:property-placeholder location="classpath:application.properties" />
AFAIK, there is no way of doing this by pure XML. Anyways, here is a little code I did this morning:
First, the test:
Then the class:
And the testContext.xml:
And the props.properties file:
It is pretty simple, just use a
ApplicationContextAwarebean and get theConfigurableEnvironmentfrom the(Web)ApplicationContext. Then just add aPropertiesPropertySourceto theMutablePropertySources