在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层注值