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這個值為空,最后導(dǎo)致空指針異常。發(fā)生的原因是
static模塊會被引入,當(dāng)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中Easypoi實現(xiàn)excel多sheet表導(dǎo)入導(dǎo)出功能
這篇文章主要介紹了Java中Easypoi實現(xiàn)excel多sheet表導(dǎo)入導(dǎo)出功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
SpringSecurity配置HTTPS的實現(xiàn)
本文介紹了SpringBoot項目中配置HTTPS并集成SpringSecurity,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
Springboot內(nèi)嵌tomcat應(yīng)用原理深入分析
懂得SpringBoot的童鞋應(yīng)該很清楚,不管應(yīng)用程序是屬于何種類型,都是一個Main方法走遍天下,對于web應(yīng)用,只需要引入spring-boot-starter-web中這個依賴,應(yīng)用程序就好像直接給我們來了個tomcat一樣,對于嵌入式Tomcat,其實也非常簡單,就是調(diào)用Tomcat提供的外部類2022-09-09
springboot項目中實現(xiàn)訪問druid內(nèi)置監(jiān)控頁面
這篇文章主要介紹了springboot項目中實現(xiàn)訪問druid內(nèi)置監(jiān)控頁面的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Java Web開發(fā)防止多用戶重復(fù)登錄的完美解決方案
在web項目開發(fā)中,很多情況下都可以讓同一個賬號信息在不同的登錄入口登錄很多次,這樣子做的不是很完善。一般解決這種情況有兩種解決方案,小編呢主要以第二種方式給大家介紹具體的實現(xiàn)方法,對java web 防止多用戶重復(fù)登錄的解決方案感興趣的朋友一起看看吧2016-11-11

