Reputation: 6073
I don't really understand the problem here. I'm trying to use a properties file in my Spring Application. The file seems to get read but if I try to read the values I get null.
This is (part) of my context xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/cloud/aws/context
http://www.springframework.org/schema/cloud/spring-cloud-aws-context.xsd">
<tx:annotation-driven />
<context:component-scan base-package="my.package" />
<context:annotation-config />
<context:property-placeholder location="classpath:test.properties" />
<!-- OTHER STUFF HERE -->
</beans>
I have my test.properties under src/main/resources
When I try to run it like this:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:META-INF/spring/context-test.xml")
public class SimpleTest {
@Autowired
private ApplicationContext context;
@Autowired
private Environment environment;
@Test
public void test() throws Exception {
System.out.println(environment);
System.out.println(environment.getProperty("my.test"));
}
}
The output will be:
StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[systemProperties,systemEnvironment]}
null
So I assume I should see something in "propertySources" there besides the system properties right?
Also if I change the xml line to this:
<context:property-placeholder location="classpath:test.properties2" />
I get a FileNotFound exception which means the file itself is loaded? So why can't I load the properties from it?
In the test.properties I have only this:
my.test=123456
What could be the problem here?
Upvotes: 0
Views: 2724
Reputation: 5857
Property-placeholder doesn't automatically expose properties to the environment. You should inject your property like this:
@Value("${my.test}")
private String myTest;
Upvotes: 1