Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
428 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...