@Value取值爲NULL

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

但是取值時,有時這個tagvalue爲NULL,可能原因有:

1、使用static或final修飾了tagValue,如下:

  private static String tagValue;  //錯誤
  private final String tagValue;   //錯誤

2、類沒有加上@Component(或者@service等)

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

3、類被new新建了實例,而沒有使用@Autowired

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

class Test{
   TestValue testValue = new TestValue()
}

這個testValue中肯定是取不到值的,必須使用@Autowired:

class Test{
    @AutoWired
    TestValue testValue
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章