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作為參數傳給次方法,然后我們可以把工廠賦值給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. 需求/目的
比如:在進行使用HandlerInterceptorAdapter攔截器時,需要訪問數據庫來判斷是否攔截請求,這時就需要在攔截器的判斷類中注入Dao或Service對象來執(zhí)行sql語句。而直接使用@Autowired無法進行注入。
2.使用環(huán)境
spring boot 2.0.3
3.方法一:獲取ApplicationContext上下文
在applicationContext對象中可以獲取到所有的bean
第一步:準備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;
}
}
這是已經寫好的工具類,可以根據bean的id獲取對應的bean
第二步: 對要獲取的bean設置id
如:
@Component("basicDaoImpl")
public class BasicDaoImpl extends AbstractDao implements BasicDao
第三步: 在要使用的類中寫一個方便調用的方法
public BasicDaoImpl getBasicDaoImpl (){
return SpringUtils.getBean(BasicDaoImpl .class, "basicDaoImpl");
}
4.方法二:將工具類的對象也添加為bean
第一步:當前類添加@Component注解
第二步:對要獲取的對象使用@Autowired 注解
@Autowired private BasicDaoImpl basicDaoImpl;
第三步:在創(chuàng)建該工具類的地方,這樣定義
@Bean
public AuthInterceptor authInterceptor(){
return new AuthInterceptor();
}
5.方法三:在spring Boot 啟動時創(chuàng)建工具類自身的靜態(tài)對象
在本質上,同方法二
第一步:當前類添加@Component注解
第二步:在工具類創(chuàng)建一個自身的靜態(tài)對象
public static AuthInterceptor authInterceptor;
第三步:使用@PostConstruct注解,在springboot加載時執(zhí)行該方法
@PostConstruct
public void init() {
authInterceptor= this;
AuthInterceptor .authInterceptor= this.authInterceptor;
}
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java使用CountDownLatch等待多線程全部執(zhí)行完成
這篇文章主要為大家詳細介紹了使用CountDownLatch等待多線程全部執(zhí)行完成,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10
java集合collection接口與子接口及實現(xiàn)類
這篇文章主要介紹了java集合collection接口與子接口及實現(xiàn)類,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07
springboot中使用mybatisplus自帶插件實現(xiàn)分頁的示例代碼
這篇文章主要介紹了springboot中使用mybatisplus自帶插件實現(xiàn)分頁,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下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,本文通過圖文給大家分享解決方案,需要的朋友可以參考下2020-08-08

