springboot?靜態(tài)方法中使用@Autowired注入方式
靜態(tài)方法使用@Autowired注入
@Component public class StructUtil { private static StructService structService; private static List<StructInfo> structInfos; // 通過重寫set注入 @Autowired public void setStructService(StructService structService){ StructUtil.structService = structService; } public static List<StructInfo> getStruct(){ if(null==structInfos){ structInfos = structService.getStruct(); } return structInfos; } }
靜態(tài)方法使用@Autowired注入的類
在寫公眾號開發(fā)的時候,有一個處理get請求,我想使用Spring提供的RestTemplate處理發(fā)送;
原來是這樣的
@Component public ?class WeChatContant { @Autowired ? ? private RestTemplate restTemplate; ?/** ? ? ?* 編寫Get請求的方法。但沒有參數(shù)傳遞的時候,可以使用Get請求 ? ? ?* ? ? ?* @param url 需要請求的URL ? ? ?* @return 將請求URL后返回的數(shù)據(jù),轉(zhuǎn)為JSON格式,并return ? ? ?*/ ? ? public ?JSONObject doGerStr(String url) throws IOException { ? ? ? ? ResponseEntity responseEntity = restTemplate.getForEntity ? ? ? ? ? ? ? ? ( ? ? ? ? ? ? ? ? ? ? ? ? url, ? ? ? ? ? ? ? ? ? ? ? ? String.class ? ? ? ? ? ? ? ? ); ? ? ? ? Object body = responseEntity.getBody(); ? ? ? ? assert body != null; ? ? ? ? JSONObject jsonObject = JSONObject.fromObject(body); ? ? ? ? System.out.println(11); ? ? ? ? return jsonObject; ? ? } }
但是到這里的話restTemplate這個值為空,最后導致空指針異常。發(fā)生的原因是
static模塊會被引入,當class加載后。你的component組件的依賴還沒有初始化。
(你的依賴都是null)
解決方法
可以使用@PostConstruct這個注解解決
1,@PostConstruct 注解的方法在加載類的構(gòu)造函數(shù)之后執(zhí)行,也就是在加載了構(gòu)造函數(shù)之后,為此,可以使用@PostConstruct注解一個方法來完成初始化,@PostConstruct注解的方法將會在依賴注入完成后被自動調(diào)用。
2,執(zhí)行優(yōu)先級高于非靜態(tài)的初始化塊,它會在類初始化(類加載的初始化階段)的時候執(zhí)行一次,執(zhí)行完成便銷毀,它僅能初始化類變量,即static修飾的數(shù)據(jù)成員。
自己理解的意思就是在component組件都加載完之后再加載
修改過后的代碼如下
@Component public ?class WeChatContant { ?? ?@Autowired ? ? private RestTemplate restTemplate; ? ? private static RestTemplate restTemplateemp; ? ? @PostConstruct ? ? public void init(){ ? ? ? ? restTemplateemp ?= restTemplate; ? ? } ? ? /** ? ? ?* 編寫Get請求的方法。但沒有參數(shù)傳遞的時候,可以使用Get請求 ? ? ?* ? ? ?* @param url 需要請求的URL ? ? ?* @return 將請求URL后返回的數(shù)據(jù),轉(zhuǎn)為JSON格式,并return ? ? ?*/ ? ? public static JSONObject doGerStr(String url) throws IOException { ? ? ? ? ResponseEntity responseEntity = restTemplateemp.getForEntity ? ? ? ? ? ? ? ? ( ? ? ? ? ? ? ? ? ? ? ? ? url, ? ? ? ? ? ? ? ? ? ? ? ? String.class ? ? ? ? ? ? ? ? ); ? ? ? ? Object body = responseEntity.getBody(); ? ? ? ? assert body != null; ? ? ? ? JSONObject jsonObject = JSONObject.fromObject(body); ? ? ? ? System.out.println(11); ? ? ? ? return jsonObject; ? ? } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 格式化輸出JSON字符串的2種實現(xiàn)操作
這篇文章主要介紹了Java 格式化輸出JSON字符串的2種實現(xiàn)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Java根據(jù)實體生成SQL數(shù)據(jù)庫表的示例代碼
這篇文章主要來和大家分享一個Java實現(xiàn)根據(jù)實體生成SQL數(shù)據(jù)庫表的代碼,文中的實現(xiàn)代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-07-07Java 中執(zhí)行動態(tài)表達式語句前中后綴Ognl、SpEL、Groovy、Jexl3
這篇文章主要介紹了Java 中執(zhí)行動態(tài)表達式語時的句前中后綴Ognl、SpEL、Groovy、Jexl3的相關(guān)資料,需要的朋友可以參考下面文章的詳細介紹2021-09-09java定義通用返回結(jié)果類ResultVO使用示例詳解
這篇文章主要為大家介紹了java定義通用返回結(jié)果類ResultVO使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09