Springboot如何通過(guò)自定義工具類獲取bean
Springboot 自定義工具類獲取bean
/**
* Created with IntelliJ IDEA.
*
* @Auther: zp
* @Date: 2021/03/26/13:32
* @Description: 通過(guò)beanFactory獲取spring管理的bean對(duì)象工具類
*/
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext context;
// springboot加載完成后會(huì)把beanfactory作為參數(shù)傳給次方法,然后我們可以把工廠賦值給context。
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
// 通過(guò)context獲取bean
public static Object getBean(String beanName) {
return context.getBean(beanName);
}
}
在工具類注入bean的三種方式
1. 需求/目的
比如:在進(jìn)行使用HandlerInterceptorAdapter攔截器時(shí),需要訪問(wèn)數(shù)據(jù)庫(kù)來(lái)判斷是否攔截請(qǐng)求,這時(shí)就需要在攔截器的判斷類中注入Dao或Service對(duì)象來(lái)執(zhí)行sql語(yǔ)句。而直接使用@Autowired無(wú)法進(jìn)行注入。
2.使用環(huán)境
spring boot 2.0.3
3.方法一:獲取ApplicationContext上下文
在applicationContext對(duì)象中可以獲取到所有的bean
第一步:準(zhǔn)備ApplicationContextAware的實(shí)現(xiàn)類,用于獲取applicationContext對(duì)象
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import com.authstr.ff.utils.exception.Assert;
@Component
public class SpringUtils implements ApplicationContextAware {
private static Log log = LogFactory.getLog(SpringUtils.class);
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) {
SpringUtils.applicationContext = applicationContext;
}
private static ApplicationContext getContext() {
return applicationContext;
}
public static Object getBean(String beanId) {
return SpringUtils.getBean(Object.class, beanId);
}
public static <T> T getBean(Class<T> clazz, String beanId) throws ClassCastException {
ApplicationContext context = SpringUtils.getContext();
Assert.isTrue(StringUtils.hasText(beanId), "beanId must not null!",true);
boolean a=context.containsBean(beanId);
Assert.isTrue(context.containsBean(beanId), "beanId :[" + beanId + "] is not exist!",true);
Object bean = null;
bean = context.getBean(beanId);
return (T)bean;
}
}
這是已經(jīng)寫好的工具類,可以根據(jù)bean的id獲取對(duì)應(yīng)的bean
第二步: 對(duì)要獲取的bean設(shè)置id
如:
@Component("basicDaoImpl")
public class BasicDaoImpl extends AbstractDao implements BasicDao
第三步: 在要使用的類中寫一個(gè)方便調(diào)用的方法
public BasicDaoImpl getBasicDaoImpl (){
return SpringUtils.getBean(BasicDaoImpl .class, "basicDaoImpl");
}
4.方法二:將工具類的對(duì)象也添加為bean
第一步:當(dāng)前類添加@Component注解
第二步:對(duì)要獲取的對(duì)象使用@Autowired 注解
@Autowired private BasicDaoImpl basicDaoImpl;
第三步:在創(chuàng)建該工具類的地方,這樣定義
@Bean
public AuthInterceptor authInterceptor(){
return new AuthInterceptor();
}
5.方法三:在spring Boot 啟動(dòng)時(shí)創(chuàng)建工具類自身的靜態(tài)對(duì)象
在本質(zhì)上,同方法二
第一步:當(dāng)前類添加@Component注解
第二步:在工具類創(chuàng)建一個(gè)自身的靜態(tài)對(duì)象
public static AuthInterceptor authInterceptor;
第三步:使用@PostConstruct注解,在springboot加載時(shí)執(zhí)行該方法
@PostConstruct
public void init() {
authInterceptor= this;
AuthInterceptor .authInterceptor= this.authInterceptor;
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis-plus插入數(shù)據(jù)遇到主鍵沒(méi)有默認(rèn)值的情況
這篇文章主要介紹了Mybatis-plus插入數(shù)據(jù)遇到主鍵沒(méi)有默認(rèn)值的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
scala中的隱式類型轉(zhuǎn)換的實(shí)現(xiàn)
這篇文章主要介紹了scala中的隱式類型轉(zhuǎn)換的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
java使用CountDownLatch等待多線程全部執(zhí)行完成
這篇文章主要為大家詳細(xì)介紹了使用CountDownLatch等待多線程全部執(zhí)行完成,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
java 算法之希爾排序詳解及實(shí)現(xiàn)代碼
這篇文章主要介紹了java 算法之希爾排序詳解及實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
Java多線程并發(fā)執(zhí)行demo代碼實(shí)例
這篇文章主要介紹了Java多線程并發(fā)執(zhí)行demo代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Java圖片與二進(jìn)制相互轉(zhuǎn)換實(shí)現(xiàn)示例講解
這篇文章主要介紹了Java圖片與二進(jìn)制相互轉(zhuǎn)換實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-03-03
java集合collection接口與子接口及實(shí)現(xiàn)類
這篇文章主要介紹了java集合collection接口與子接口及實(shí)現(xiàn)類,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
springboot中使用mybatisplus自帶插件實(shí)現(xiàn)分頁(yè)的示例代碼
這篇文章主要介紹了springboot中使用mybatisplus自帶插件實(shí)現(xiàn)分頁(yè),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
Intellij IDEA 與maven 版本不符 Unable to import maven project See
這篇文章主要介紹了Intellij IDEA 與maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound,本文通過(guò)圖文給大家分享解決方案,需要的朋友可以參考下2020-08-08

