Accessing Spring Boot application.properties using the @Value annotation
<p>I have read through just about everything I can find and for some reason I cannot quite wrap my brain around how to make this work.  After struggling in an existing project for 2 days I downloaded the Getting Started Content for Rest from Spring.io</p>

<p><a href="https://spring.io/guides/gs/rest-service/" rel="nofollow">https://spring.io/guides/gs/rest-service/</a></p>

<p>I created a resources folder in the main folder and added a file called application.properties with the following:</p>

<pre><code>my.setting = HELLOTHERE
</code></pre>

<p>I then modified the Greeting class as follows:</p>

<pre><code>    package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
public class Greeting {

    private long id;
    private String content;
    private String dooDad;
    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public Greeting() {
        // TODO Auto-generated constructor stub
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }

    public String getDooDad() {
        return dooDad;
    }
    @Autowired
    public void setDooDad(@Value("${my.setting}")String dooDad) {
        this.dooDad = dooDad;
    }
}
</code></pre>

<p>I modified the Application class to have the @Configuration annotation as follows:</p>

<pre><code>package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@Configuration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
</code></pre>

<p>I didn't modify the GreetingController class but will include it for completeness:</p>

<pre><code>package hello;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}
</code></pre>

<p>When I run the application and look at the output I expected to see a value for dooDad, instead it is always null.</p>

<pre><code>{"id":1,"content":"Hello, mike!","dooDad":null}
</code></pre>

<p>Any help in getting this figured out would be appreciated.  I did discover that if I put the @Value annotation on a variable in the Application class it would populate that variable with the correct value, just not when it is in the Greeting Class?</p>