Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading up properties file to a class in Spring
    text
    copied!<p>I am trying to load a property file (.properties) into my class, I am following the example in another thread here: <a href="https://stackoverflow.com/questions/9259819/how-to-read-values-from-properties-file">How to read values from properties file?</a> - but it's not working for me.</p> <p>here's my quick implementation:</p> <p><strong>applicationContext.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;context:annotation-config /&gt; &lt;!-- Load up properties --&gt; &lt;context:component-scan base-package="com.test"/&gt; &lt;context:property-placeholder location="file:///C:/dev/workspace/test-project/src/main/resources/appconfig.properties"/&gt; &lt;/beans&gt; </code></pre> <p><strong>TestConfig.java</strong></p> <pre><code>@Component public class TestConfig { @Value("${test.key1}") private String key1; public String getKey1() { return key1; } } </code></pre> <p><strong>src/main/resources/appconfig.properties</strong></p> <pre><code>test.key1=value test.key2=value </code></pre> <p>Starting up my tomcat, I see the following in my log:</p> <pre><code>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] </code></pre> <p>However, when I do getKey1(), I get "null".</p> <p>What am I missing?</p> <p>Question 2: If I use "classpath":</p> <pre><code>&lt;context:property-placeholder location="classpath:appconfig.properties"/&gt; </code></pre> <p>Which directory is that referring to? root of WEB-INF/classes?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload