Reading values from application.properties Spring Boot

Viewed 65269

My Spring boot app has this application structure:

  • src
    • main
      • java
      • resources
        • application.properties

This is my application.properties file:

logging.level.org.springframework=TRACE
logging.level.org.hibernate=ERROR
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
#spring.resources.chain.cache=false
#spring.resources.chain.html-application-cache=false
#spring.headers.cache=false
language=java

I have a class which requires the use of that language=java property. This is how I am trying to use it:

public class EntityManager {

    @Value("${language}")
    private static String newLang;

    public EntityManager(){
        System.out.println("langauge is: " + newLang);
    }
}

That printed value is always "null" for some reason! I have also tried putting this on top of the class declaration:

@PropertySource(value = "classpath:application.properties")
12 Answers

If you are using Spring boot you do not need @PropertySource("classpath:application.properties") if you are using spring boot starter parent , just remove the static keyword and it should start working.

Sometimes, the classpath entry for src/main/resources contains an exclude tag. If application.properties is present in this list of excluded items then @PropertySource("classpath:application.properties") will not be able to find the property file.

Either remove the exclude entry from [.classpath][1] file for src/main/resources manually, or use the Configure build path option in Eclipse and go to Source tab. Then remove the exclude entry from src.

As provided in the 2nd answer above I was indeed using the spring boot, so removed the static keyword and that resolved my issue. Make sure you don't have static for the property defined with @Value.

The value from the properties file is ALWAYS null IF you are trying to read it BEFORE the Spring Bean has been fully initialized, for example, when calling the value from the constructor. This was the case for me.

To get past this, use the @PostConstruct annotation like this:

@Configuration
public class CustomConfiguration {


     Logger logger = Logger.getLogger(CustomConfiguration.class.getSimpleName());

     @Value("${test.value}")
     private String testValue;

      @PostConstruct
      public void initializeApplication() {
        logger.info("============================================>"+testValue);
      }

Just make sure you are not trying to use the value of testValue anywhere else before initializeApplication is called.

in my case, the same problem had a slightly different cause - i had a class with a static instance variable (the old usual way we used to implement a Singleton pattern), into which i wanted to inject an entry from application.properties (probably the "static" thing was why the property could not be found - with no errors no anything :/).

moreover this class was in a totally different package from the class declared as @SpringBootApplication. i understood that package names and locations have to be taken into serious consideration with Spring Boot and automatic component scanning. my 5 cent

You do not need to specify the properties file if you are using the default application.properties file in src/main/resources directory. It will be auto-detected.

You need to add @Value in the constructor as the value injection doesn't happen before constructor is called.

    private static String newLang;

    public EntityManager(@Value("${language}") newLang){
        this.newLang=newLang;
    }
Related