1. 程式人生 > >SpringBoot利用註解@Value獲取properties屬性為null

SpringBoot利用註解@Value獲取properties屬性為null

pro 解決 autowired dfs stat info 如何 一個 去掉

參考:https://www.cnblogs.com/zacky31/p/8609990.html

今天在項目中想使用@Value來獲取Springboot中properties中屬性值。

場景:定義了一個工具類,想要獲取一些配置參數,使用了@value來獲取,但是死活也獲取不到。

如何解決:在使用這個工具類的時候是new的,要想使用@value來獲取,必須將這個工具類交由容器來註入,

SeaWeedFs seaWeedFs = new SeaWeedFs();

改為

@Autowired
private SeaWeedFs seaWeedFs;

嘗試了還發現一個問題,一開始工具類中的方法是靜態的,當Springboot啟動時後報一個錯誤

技術分享圖片

把static去掉以後,終於可以用@value來獲取properties中的屬性值

SpringBoot利用註解@Value獲取properties屬性為null