Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: @Value returns always null

I would like to use a value from application.properties file in order to pass it in the method in another class. The problem is that the value returns always NULL. What could be the problem? Thanks in advance.

application.properties

filesystem.directory=temp

FileSystem.java

@Value("${filesystem.directory}")
private static String directory;
like image 952
Jonathan Williams Avatar asked Sep 04 '17 08:09

Jonathan Williams


People also ask

How does @value work in spring boot?

One of the most important annotations in spring is @Value annotation which is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. It also supports Spring Expression Language (SpEL).

Can I use @value in @service?

@Value annotation can be used within classes annotated with @Configuration , @Component and other stereotype annotations like @Controller , @Service etc. The actual processing of @Value annotation is performed by BeanPostProcessor and so @Value cannot be used within BeanPostProcessor class types.

Can I use @value in interface?

No, this is not (directly) possible. The default value of an annotation property must be a compile-time constant.

What is ConfigurationProperties?

@ConfigurationProperties allows to map the entire Properties and Yaml files into an object easily. It also allows to validate properties with JSR-303 bean validation. By default, the annotation reads from the application. properties file. The source file can be changed with @PropertySource annotation.


2 Answers

You can't use @Value on static variables. You'll have to either mark it as non static or have a look here at a way to inject values into static variables:

https://www.mkyong.com/spring/spring-inject-a-value-into-static-variables/

EDIT: Just in case the link breaks in the future. You can do this by making a non static setter for your static variable:

@Component
public class MyComponent {

    private static String directory;

    @Value("${filesystem.directory}")
    public void setDirectory(String value) {
        this.directory = value;
    }
}

The class needs to be a Spring bean though or else it won't be instantiated and the setter will be not be accessible by Spring.

like image 126
Plog Avatar answered Oct 02 '22 16:10

Plog


For the ones still facing the issue after all the preceding suggestions, make sure you are not accessing that variable before the bean has been constructed.

That is:

Instead of doing this:

@Component
public MyBean {
   @Value("${properties.my-var}")
   private String myVar;

   private String anotherVar = foo(myVar); // <-- myVar here is still null!!!
}

do this:

@Component
public MyBean {
   @Value("${properties.my-var}")
   private String myVar;

   private String anotherVar;

   @PostConstruct  
   public void postConstruct(){

      anotherVar = foo(myVar); // <-- using myVar after the bean construction
   }
}

Hope this will help someone avoid wasting hours.

like image 38
Philippe Simo Avatar answered Oct 02 '22 15:10

Philippe Simo