@Value取不到值的原因

在springboot中想获取配置文件中的值,一般的方法为

@Value("${tag}")
private String tagValue;

但是取值时,有时这个tagvalue为NULL,可能原因有:

1.类没有加上@Component(或者@service等)

@Component //遗漏
class TestValue{
    @Value("${tag}")
    private String tagValue;
}

2.类被new新建了实例,而没有使用@Autowired

@Component 
class TestValue{
    @Value("${tag}")
    private String tagValue;
}

class Test{
    ...
    TestValue testValue = new TestValue()

正确方式:

1.使用@Autowired注入

2.在controller层注值


版权声明:本文为u013008898原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。