Springboot如何通過自定義工具類獲取bean
Springboot 自定義工具類獲取bean
/**
* Created with IntelliJ IDEA.
*
* @Auther: zp
* @Date: 2021/03/26/13:32
* @Description: 通過beanFactory獲取spring管理的bean對象工具類
*/
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext context;
// springboot加載完成后會把beanfactory作為參數(shù)傳給次方法,然后我們可以把工廠賦值給context。
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
// 通過context獲取bean
public static Object getBean(String beanName) {
return context.getBean(beanName);
}
}
在工具類注入bean的三種方式
1. 需求/目的
比如:在進(jìn)行使用HandlerInterceptorAdapter攔截器時,需要訪問數(shù)據(jù)庫來判斷是否攔截請求,這時就需要在攔截器的判斷類中注入Dao或Service對象來執(zhí)行sql語句。而直接使用@Autowired無法進(jìn)行注入。
2.使用環(huán)境
spring boot 2.0.3
3.方法一:獲取ApplicationContext上下文
在applicationContext對象中可以獲取到所有的bean
第一步:準(zhǔn)備ApplicationContextAware的實現(xiàn)類,用于獲取applicationContext對象
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獲取對應(yīng)的bean
第二步: 對要獲取的bean設(shè)置id
如:
@Component("basicDaoImpl")
public class BasicDaoImpl extends AbstractDao implements BasicDao
第三步: 在要使用的類中寫一個方便調(diào)用的方法
public BasicDaoImpl getBasicDaoImpl (){
return SpringUtils.getBean(BasicDaoImpl .class, "basicDaoImpl");
}
4.方法二:將工具類的對象也添加為bean
第一步:當(dāng)前類添加@Component注解
第二步:對要獲取的對象使用@Autowired 注解
@Autowired private BasicDaoImpl basicDaoImpl;
第三步:在創(chuàng)建該工具類的地方,這樣定義
@Bean
public AuthInterceptor authInterceptor(){
return new AuthInterceptor();
}
5.方法三:在spring Boot 啟動時創(chuàng)建工具類自身的靜態(tài)對象
在本質(zhì)上,同方法二
第一步:當(dāng)前類添加@Component注解
第二步:在工具類創(chuàng)建一個自身的靜態(tài)對象
public static AuthInterceptor authInterceptor;
第三步:使用@PostConstruct注解,在springboot加載時執(zhí)行該方法
@PostConstruct
public void init() {
authInterceptor= this;
AuthInterceptor .authInterceptor= this.authInterceptor;
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳談java線程與線程、進(jìn)程與進(jìn)程間通信
下面小編就為大家?guī)硪黄斦刯ava線程與線程、進(jìn)程與進(jìn)程間通信。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
SpringCloudGateway使用Skywalking時日志打印traceId解析
這篇文章主要為大家介紹了SpringCloudGateway使用Skywalking時日志打印traceId解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Java設(shè)計模式七大原則之單一職責(zé)原則詳解
單一職責(zé)原則(Single Responsibility Principle, SRP),有且僅有一個原因引起類的變更。簡單來說,就是針對一個java類,它應(yīng)該只負(fù)責(zé)一項職責(zé)。本文將詳細(xì)介紹一下Java設(shè)計模式七大原則之一的單一職責(zé)原則,需要的可以參考一下2022-02-02
SpringBoot統(tǒng)一處理功能實現(xiàn)的全過程
最近在做項目時需要對異常進(jìn)行全局統(tǒng)一處理,主要是一些分類入庫以及記錄日志等,下面這篇文章主要給大家介紹了關(guān)于SpringBoot統(tǒng)一功能處理實現(xiàn)的相關(guān)資料,文中通過圖文以及實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
Triple協(xié)議支持Java異?;貍髟O(shè)計實現(xiàn)詳解
這篇文章主要為大家介紹了Triple協(xié)議支持Java異?;貍髟O(shè)計實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Springboot Mybatis-Plus數(shù)據(jù)庫單元測試實戰(zhàn)(三種方式)
這篇文章主要介紹了Springboot Mybatis-Plus數(shù)據(jù)庫單元測試實戰(zhàn)(三種方式),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

