title: Spring @Value annotation and setter injection difference
body: <p>When I am setting a value using  @Value annotation from a property file, its not working, whereas setting using setter injection works.</p>
  
  <p>my properties file like this.</p>
  
  <pre><code>app.work.dir=file:${user.home}/WORK
  </code></pre>
  
  <p>class file like below.</p>
  
  <pre><code>@Value("#{configProperties['app.work.dir']}")
  private String workdir;
  </code></pre>
  
  <p>and this is my xml setting.</p>
  
  <pre><code>&lt;util:properties id="configProperties" location="classpath:config.properties" /&gt;
  </code></pre>
  
  <p>when use setter injection like below then it works fine.</p>
  
  <pre><code>&lt;bean id="sampleService" class="aaa.SampleService"&gt;
      &lt;property name="workdir" value="${app.work.dir}" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>I'm not sure why and if possible i want to use @Value annotation.</p>
  
  <p>please refer to junit test case below.</p>
  
  <p>xml configulation:</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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&gt;
  
      &lt;context:property-placeholder location="classpath:testconfig.properties" /&gt;
      &lt;util:properties id="sampleProperties" location="classpath:testconfig.properties" /&gt;
  
      &lt;bean id="exampleA" class="aa.sample.SampleA" /&gt;
  
      &lt;bean id="sampleB" class="aa.sample.SampleB" &gt;
          &lt;property name="workdir" value="${work.dir}" /&gt;
          &lt;property name="tempdir" value="${work.dir.test}" /&gt;
          &lt;property name="aaa" value="${aaa}" /&gt;
      &lt;/bean&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>sample service class:</p>
  
  <p>package aa.sample;</p>
  
  <pre><code>import org.springframework.beans.factory.annotation.Value;
  import org.springframework.stereotype.Service;
  
  @Service
  public class SampleA {
  
      @Value("#{sampleProperties['work.dir']}")
      private String workdir;
      @Value("#{sampleProperties['work.dir.test']}")
      private String tempdir;
      @Value("#{sampleProperties['aaa']}")
      private String aaa;
  
      //getter setter deleted
  
  }
  </code></pre>
  
  <p>properties file:</p>
  
  <pre><code>work.dir=file:${user.home}/WORK
  work.dir.test=${work.dir}/TEST
  aaa=bbb
  </code></pre>
  
  <p>and junit test class:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration({ "/testcontext.xml" })
  public class ExampleTest {
  
      @Inject
      private SampleA sampleA;
  
      @Inject
      private SampleB sampleB;
  
      @Test
      public void testValueAnnotation() {
  
          assertThat(sampleA.getAaa(), is("bbb"));//ok
          String tempdir = sampleA.getTempdir();
          String workdir = sampleA.getWorkdir();
          assertFalse("[sampleA] temp dir should not have ${work.dir}", tempdir.indexOf("${work.dir}") &gt;= 0);//ng
          assertFalse("[sampleA] workdir dir should not have ${user.home}", workdir.indexOf("${user.home}") &gt;= 0);//ng
      }
  
  
  }
  </code></pre>