Java Spring Boot:@Value 始终返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46032748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:15:08  来源:igfitidea点击:

Spring Boot: @Value returns always null

javaspring-bootapplication.properties

提问by Jonathan Williams

I would like to use a value from application.propertiesfile 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文件中的值,以便在另一个类的方法中传递它。问题是该值总是返回NULL。可能是什么问题呢?提前致谢。

application.properties

application.properties

filesystem.directory=temp

FileSystem.java

FileSystem.java

@Value("${filesystem.directory}")
private static String directory;

采纳答案by Plog

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:

您不能在静态变量上使用 @Value。您必须将其标记为非静态或在此处查看将值注入静态变量的方法:

https://www.mkyong.com/spring/spring-inject-a-value-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:

编辑:以防万一链接在未来中断。你可以通过为你的静态变量创建一个非静态的 setter 来做到这一点:

@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.

该类需要是一个 Spring bean,否则它不会被实例化,并且 Spring 将无法访问 setter。

回答by Karthik R

Few things for you to cross check apart from @Plog's answer.

除了@Plog 的回答之外,您几乎没有什么需要交叉检查的。

staticvariables can't be injected with value. Check @Plog's answer.

static变量不能注入值。检查@Plog 的答案。

  • Make sure the class is annotated with @Componentor @Service
  • The component scan should scan the enclosing package for registering the beans. Check your XML if xml enabled configuration.
  • Check if the property file's path is correct or in classpath.
  • 确保类用@Component或注释@Service
  • 组件扫描应扫描封闭包以注册 bean。如果启用了 xml 配置,请检查您的 XML。
  • 检查属性文件的路径是否正确或在类路径中。

回答by scottysseus

The other answers are probably correct for the OP.

对于 OP,其他答案可能是正确的。

However, I ran into the same symptoms (@Value-annotated fields being null) but with a different underlying issue:

但是,我遇到了相同的症状( - 带@Value注释的字段是null),但有一个不同的潜在问题:

import com.google.api.client.util.Value;

import com.google.api.client.util.Value;

Ensure that you are importing the correct @Valueannotation class! Especially with the convenience of IDEs nowadays, this is a VERY easy mistake to make (I am using IntelliJ, and if you auto-import too quickly without reading WHAT you are auto-importing, you might waste a few hours like I did).

确保您正在导入正确的@Value注释类!尤其是在当今 IDE 的便利下,这是一个非常容易犯的错误(我使用的是 IntelliJ,如果您在没有阅读自动导入的内容的情况下过快地自动导入,您可能会像我一样浪费几个小时)。

Of course, the correct class to import is:

当然,要导入的正确类是:

import org.springframework.beans.factory.annotation.Value;

import org.springframework.beans.factory.annotation.Value;

回答by Vitalii Shramko

Spring uses dependency injection to populate the specific value when it finds the @Value annotation. However, instead of handing the value to the instance variable, it's handed to the implicit setter instead. This setter then handles the population of our NAME_STATIC value.

Spring 使用依赖注入在找到 @Value 注释时填充特定值。但是,它不是将值传递给实例变量,而是传递给隐式 setter。然后这个 setter 处理我们的 NAME_STATIC 值的填充。

    @RestController 
//or if you want to declare some specific use of the properties file then use
//@Configuration
//@PropertySource({"classpath:application-${youeEnvironment}.properties"})
public class PropertyController {

    @Value("${name}")//not necessary
    private String name;//not necessary

    private static String NAME_STATIC;

    @Value("${name}")
    public void setNameStatic(String name){
        PropertyController.NAME_STATIC = name;
    }
}