SpringBoot external properties not loaded : I'm starting with SpringBoot, so am first going through the [SpringBoot reference Guide][1]


I'm currently having a look at the [Externalized Configuration][2]

My goal is to have a simple Component to read the value out of `application.properties`, which, if I'm not mistaken, should be loaded automatically, without any further configuration.

The example in the reference is as follows:

import org.springframework.stereotype.*
import org.springframework.beans.factory.annotation.*

    @Component
    public class MyBean {
    
        @Value("${name}")
        private String name;
    
        // ...
    
    }
with the following explanation:

> On your application classpath (e.g. inside your jar) you can have an
> application.properties that provides a sensible default property value
> for name. When running in a new environment, an application.properties
> can be provided outside of your jar that overrides the name; and for
> one-off testing, you can launch with a specific command line switch
> (e.g. java -jar app.jar --name="Spring").

Seems simple enough. So, I have created following Component:

    @Component
    public class Admin {
    
        @Value("${name}")
        private String name;
    
        public String getName(){
            return "ReadName_" + name;
        }
    }

And, in my `recources` folder, the following `application.properties` file (which, I verified, is copied into my .jar)

> name=myName

So, if I follow the logic in the reference (which is also mentioned in this post: [Read application.properties][3], and run my application.

The code compiles and runs without error, but each time I print the value of `getName()` from an instance of `Admin` on my screen, it just gives a "ReadName_null" instead of "ReadName_myName", the value in the properties file.

As the reference states, in 24.2:

> By default SpringApplication will convert any command line option
> arguments (starting with ‘--’, e.g. --server.port=9000) to a property
> and add it to the Spring Environment. As mentioned above, command line
> properties always take precedence over other property sources.

So, just to check, I run my .jar file with both the `application.properties` with the key present, and add `--name=passedName` as a command line argument.

Even though this value should be automatically added to the environment, and overrule anything (or in my case: nothing) that is currently there, and I expect the getName() to return "ReadName_passedName", it still returns "ReadName_null".

EDIT: I print the command line arguments I pass while booting, so I know that the argument is indeed read by the system as "--name=passedName"

Is there anything obvious I'm missing here?



  [1]: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
  [2]: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
  [3]: https://stackoverflow.com/questions/34956752/read-properties-file-in-spring-boot