0

I am trying to load a property file (.properties) into my class, I am following the example in another thread here: How to read values from properties file? - but it's not working for me.

here's my quick implementation:

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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: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/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


    <context:annotation-config />

    <!-- Load up properties -->
    <context:component-scan base-package="com.test"/>
    <context:property-placeholder location="file:///C:/dev/workspace/test-project/src/main/resources/appconfig.properties"/>
</beans>

TestConfig.java

@Component
public class TestConfig
{

    @Value("${test.key1}")
    private String key1;

    public String getKey1()
    {
        return key1;
    }

}

src/main/resources/appconfig.properties

test.key1=value
test.key2=value

Starting up my tomcat, I see the following in my log:

00:11:41,985 [localhost-startStop-1]  INFO PropertyPlaceholderConfigurer - Loading properties file from URL [file:/C:/dev/workspace/test-project/src/main/resources/appconfig.properties]

However, when I do getKey1(), I get "null".

What am I missing?

Question 2: If I use "classpath":

<context:property-placeholder location="classpath:appconfig.properties"/>

Which directory is that referring to? root of WEB-INF/classes?

Community
  • 1
  • 1
TS-
  • 4,311
  • 8
  • 40
  • 52

2 Answers2

0

I hope you are using IDE like Eclipse.

  • Check if the resources directory is added to the classpath and it includes all the files within it as well , if eclipse you have to add . in the inclusion pattern

  • build your project and check if the the property file is available in WEB-INF/classes

To answer your second question

classpath:appconfig.properties -Yes spring will look for the file in WEB-INF/classes

Sudhakar
  • 4,823
  • 2
  • 35
  • 42
  • I am - The first one it fails, it is using local computer file:// reference. I build the project using Maven and deploy it to Tomcat 7. I checked the war generated and it has appconfig.properties in \WEB-INF\classes – TS- Feb 23 '13 at 05:44
  • thanks for the edit, so even if i use classpath:appconfig.properties, it should be good because the properties file is there in the war file. So somehow my @Component class is not initialized? – TS- Feb 23 '13 at 13:30
0

This was silly ...

When I get the TestConfig object, I was doing:

TestConfig config = new TestConfig();
config.getKey1();

Which of course the config object is a brand new object and never instantiated (or injected) with anything.

Instead I am injecting it so it got initialized by Spring framework:

@Autowired
private TestConfig config;
TS-
  • 4,311
  • 8
  • 40
  • 52