<p>New to spring boot.</p>

<p>While exploring spring boot env variables, came to know that, 
env variables can be accessed by <strong><em><code>${KeyName}</code></em></strong> from code.</p>

<p>Got a question like, </p>

<p><strong>Case 1:</strong>
In @Configuration files, we are accessing keys in application.properties using <strong><em><code>@Value(value = "${KeyName}")</code></em></strong>.
So, we are using almost same syntax for accessing env variables and accessing keys in application.properties.</p>

<p><strong>Case 2:</strong>
When trying to access the keys in application.properties using system.getEnv("keyname"), I got only null.</p>

<p><strong>Case 3:</strong>
Recently worked on <strong><em>configmap in kubernetes with spring boot</em></strong>.</p>

<p>Config file looks like,</p>

<pre><code>spec:
  containers:
  - name: demo-configconsumercontainer
    image: springbootappimage:latest
    ports:
    - containerPort: 8080
    envFrom:
      - configMapRef:
          name: example-configmap
</code></pre>

<p>All the values from configMap is exported as environmental variables and 
I am accessing those values by <code>@Value(value = "${KeyName}")</code> and by <code>system.getEnv(KeyName)</code>.</p>

<ol>
<li>My question is, how case 3 is working when case 2 is not.</li>
<li>Is Spring boot made such a way that, it is allowing to access by <code>${KeyName}</code> and not by <code>system.getEnv(KeyName)</code>? (ie. Case 2)</li>
</ol>

<p>Could some one clarify my questions here.</p>

NANDAKUMAR THANGAVELU
<p>Using @Value annotation, <strong>you can access a property from many property sources</strong> such as in application.properties or an environment variable and few more property sources.</p>

<p>The important point here is <strong>ordering of these property sources</strong>.</p>

<p>Below is the order of looking up the property in various sources.</p>

<ol>
<li>Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).</li>
<li>@TestPropertySource annotations on your tests.</li>
<li>@SpringBootTest#properties annotation attribute on your tests.</li>
<li>Command line arguments.</li>
<li>Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property)</li>
<li>ServletConfig init parameters.</li>
<li>ServletContext init parameters.</li>
<li>JNDI attributes from java:comp/env.</li>
<li>Java System properties (System.getProperties()).</li>
<li><strong>OS environment variables.</strong></li>
<li>A RandomValuePropertySource that only has properties in random.*.</li>
<li>Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)</li>
<li>Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)</li>
<li>Application properties outside of your packaged jar (application.properties and YAML variants).</li>
<li><strong>Application properties packaged inside your jar</strong> (application.properties and YAML variants).</li>
<li>@PropertySource annotations on your @Configuration classes.</li>
<li>Default properties (specified using SpringApplication.setDefaultProperties).</li>
</ol>

<p>In your case, the property is either declared in environment variable or in application.yaml and hence accessible using @Value annotation.</p>
