I’m doing some test on a very, very simple application using Spring.
My app have only have one bean and I’m injecting a simple String to this class and printing this value. So far all working.
What I need:
I want to get this String from a configuration file, so I create the file inside /src/main/resource
What I did:
1) On my application-context.xml I add:
<context:property-placeholder location="classpath:myConfigFile.properties" />
2) On my application-context.xml I change from the simple String to use ${name_test}:
<bean id="hello" class="com.dummy.SayHello">
<property name="name" value="${name_test}" />
</bean>
3) I double check myConfigFile.properties and contains the “name_test=JackTheRipper”
4) But my output is not ‘translating’ the value from the config file, I have this output when I run my app:
Hello ${name_test}
And I’m stuck here, any clue, tips???
Just FYI
- I use THIS tutorial for my tests, maybe could help.
- I add the log4j maven dependencies and log4j config file and all works fine! So Spring and log4j are finding the files inside “src/main/resource”
- I’m using maven, and to run my app, I’m using:
mvn clean compile exec:java
SOLUTION EXPLANATION:
The root cause was how I was getting the application-context.xml on my java class.
I was doing:
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("application-context.xml"));
and then after this post I change it to:
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
- A good place to understand and read is HERE
- Thanks all for the help!
The only problem I can imagine here is that you use
BeanFactoryinstead ofApplicationContext. Compared toApplicationContext,BeanFactorymisses some advanced features including automatic registration of postprocessors, that is necessary for<context:property-placeholder>.