SpringBoot靜態(tài)方法調(diào)用Spring容器bean的三種解決方案
問題分析
今天一個(gè)小伙伴和我說@Autowired 注入的Bean為null值,經(jīng)過一番溝通后,我發(fā)現(xiàn)原來他需要在靜態(tài)方法中調(diào)用容器Bean,大致將代碼貼出來給大家看看:
//模擬錯(cuò)誤的使用方式
@Autowired
private static RedisUtil redisUtilBean;
public static String getMsgByRedis(){
redisUtilBean.get("xxx") //這里redisUtilBean一定會(huì)是NULL值
}
為什么會(huì)出現(xiàn)這種情況?原因是Spring容器的依賴注入是依賴set方法,而set方法是實(shí)例對(duì)象的方法,注入依賴時(shí)是無法注入靜態(tài)成員變量的,在調(diào)用的時(shí)候依賴的Bean才會(huì)為null;
解決方案一
使用@PostConstruct注解,
@Autowired
private RedisUtil redisUtilBean;
//由于靜態(tài)方法無法使用注入的Bean 定義靜態(tài)變量
private static RedisUtil redisUtil;
//當(dāng)容器實(shí)例化當(dāng)前受管Bean時(shí)@PostConstruct注解的方法會(huì)被自動(dòng)觸發(fā),借此來實(shí)現(xiàn)靜態(tài)變量初始化
@PostConstruct
public void init(){
this.redisUtil = redisUtilBean;
}
public static String getMsgByRedis(){
redisUtil.get("xxx") //這里可以正常使用
}
解決方案二
利用springboot的啟動(dòng)類中,SpringApplication.run() 方法返回的是一個(gè)ConfigurableApplicationContext對(duì)象通過定義static變量ConfigurableApplicationContext,利用容器的getBean方法獲得依賴對(duì)象;
@SpringBootApplication
@EnableTransactionManagement
public class Application {
//定義靜態(tài)的ApplicationContext
public static ConfigurableApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = SpringApplication.run(Application.class, args);
}
}
//調(diào)用 注意Application是我們SpringBoot的啟動(dòng)類
public static String getMsgByRedis(){
Application.applicationContext..getBean(RedisUtil .class).get("xxx")
}
解決方案三
在我們以前SpringMVC中常用的工具類,通過實(shí)現(xiàn)ApplicationContextAware接口,網(wǎng)上也很多這里就把工具類貼出來即可;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.toher.common.utils.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* 以靜態(tài)變量保存Spring ApplicationContext, 可在任何代碼任何地方任何時(shí)候中取出ApplicaitonContext.
*
* @author 李懷明
* @version 2017-01-02
*/
@Component
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
//實(shí)現(xiàn)ApplicationContextAware接口的context注入函數(shù), 將其存入靜態(tài)變量.
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext;
}
//取得存儲(chǔ)在靜態(tài)變量中的ApplicationContext.
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}
//從靜態(tài)變量ApplicationContext中取得Bean, 自動(dòng)轉(zhuǎn)型為所賦值對(duì)象的類型.
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
}
//從靜態(tài)變量ApplicationContext中取得Bean, 自動(dòng)轉(zhuǎn)型為所賦值對(duì)象的類型.
//如果有多個(gè)Bean符合Class, 取出第一個(gè).
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
@SuppressWarnings("rawtypes")
Map beanMaps = applicationContext.getBeansOfType(clazz);
if (beanMaps != null && !beanMaps.isEmpty()) {
return (T) beanMaps.values().iterator().next();
} else {
return null;
}
}
private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,請(qǐng)?jiān)赼pplicationContext.xml中定義SpringContextHolder");
}
}
public static HttpServletRequest getRequest() {
try {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
} catch (Exception e) {
return null;
}
}
}
調(diào)用方法:
RedisUtil redisUtil= (RedisUtil) SpringContextHolder.getBean(RedisUtil.class);
結(jié)語
到此這篇關(guān)于SpringBoot靜態(tài)方法調(diào)用Spring容器bean的三種方案的文章就介紹到這了,更多相關(guān)SpringBoot靜態(tài)方法調(diào)用bean內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA爬蟲實(shí)現(xiàn)自動(dòng)登錄淘寶
給大家分享一個(gè)關(guān)于JAVA爬蟲的相關(guān)知識(shí)點(diǎn),通過代碼實(shí)現(xiàn)自動(dòng)登錄淘寶網(wǎng),有興趣的朋友測(cè)試下。2018-04-04
Java后臺(tái)接收數(shù)據(jù)的三種方式(url、form-data與application/json)
本文主要介紹了Java后臺(tái)接收數(shù)據(jù)的三種方式(url、form-data與application/json),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Java并發(fā)編程示例(六):等待線程執(zhí)行終止
這篇文章主要介紹了Java并發(fā)編程示例(六):等待線程執(zhí)行終止,在本節(jié),示例程序演示等待初始化方法完成后,再去執(zhí)行其他任務(wù),需要的朋友可以參考下2014-12-12
JavaIO?BufferedReader和BufferedWriter使用及說明
這篇文章主要介紹了JavaIO?BufferedReader和BufferedWriter使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
淺談SpringBoot @Autowired的兩種注入方式
本文主要介紹了兩種SpringBoot @Autowired注入方式,具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
spring boot 使用 Kafka的場(chǎng)景分析
本文詳細(xì)介紹了Kafka作為消息隊(duì)列在SpringBoot中的使用方法,包括添加依賴、創(chuàng)建生產(chǎn)者和消費(fèi)者,以及與RocketMQ的比較,著重于數(shù)據(jù)可靠性、性能和消息傳遞方式,還探討了Kafka在實(shí)時(shí)數(shù)據(jù)流處理、事件驅(qū)動(dòng)架構(gòu)等場(chǎng)景的應(yīng)用,感興趣的朋友跟隨小編一起看看吧2025-12-12
java實(shí)現(xiàn)TCP socket和UDP socket的實(shí)例
這篇文章主要介紹了本文主要介紹了java實(shí)現(xiàn)TCP socket和UDP socket的實(shí)例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

