SpringBoot如何使用ApplicationContext獲取bean對(duì)象
使用ApplicationContext獲取bean對(duì)象
編寫一個(gè)ApplicationContextFactory工廠類
public class ApplicationContextFactory{
private static ApplicationContext applicationContext = null;
public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
}
在SpringBoot的啟動(dòng)類中設(shè)置ApplicationContext
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext app = SpringApplication.run(Application.class, args);
ApplicationContextFactory.setApplicationContext(app);
}
}
通過ApplicationContextFactory獲取SpringApplication從而獲取bean對(duì)象
ApplicationContext applicationContext=ApplicationContextFactory.getApplicationContext(); Clazz clazz = applicationContext.getBean(Clazz.class);
SpringBoot Bean注入的深入研究
下面代碼可正常運(yùn)行
DemoService
@Service
public class DemoService {
public void save(){
System.out.println("DemoService save");
}
}
CommonClass
@Component
public class CommonClass {
@Resource
private DemoService demoService;
public void fun(){
System.out.println("fun");
demoService.save();
}
}
Controller
@Resource
private CommonClass commonClass;
@ResponseBody
@GetMapping("/fun")
public void fun(){
commonClass.fun();
}
下面代碼不能正常運(yùn)行
DemoService
@Service
public class DemoService {
public void save(){
System.out.println("DemoService save");
}
}
CommonClass
public class CommonClass {
@Resource
private DemoService demoService;
public void fun(){
System.out.println("fun");
demoService.save();
}
}
Controller
@ResponseBody
@GetMapping("/fun")
public void fun(){
CommonClass commonClass = new CommonClass();
commonClass.fun();
}
比較
比較兩個(gè)代碼發(fā)現(xiàn)后者與前者的區(qū)別:因后者的CommonClass 沒有使用@Component標(biāo)注,所以在Controller中不能才用注入方式生成CommonClass對(duì)象,而是才用new的方式生成了該對(duì)象。
這樣一來,CommonClass 對(duì)象是手工創(chuàng)建,所以在它內(nèi)部注入DemoService 對(duì)象的代碼就錯(cuò)誤了。
解決方案
新建工具類
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext act;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
act = applicationContext;
}
/**
* 根據(jù)bean的名字獲取工廠中對(duì)應(yīng)的bean對(duì)象
* @param beanName
* @return
*/
public static Object getBean(String beanName){
return act.getBean(beanName);
}
}
注:實(shí)際測(cè)試發(fā)現(xiàn)上面代碼中的static不能省略
DemoService
@Service
public class DemoService {
public void save(){
System.out.println("DemoService save");
}
}
CommonClass
public class CommonClass {
@Resource
private DemoService demoService;
public void fun(){
DemoService demoService = (DemoService) ApplicationContextUtil.getBean("demoService");
System.out.println("fun");
demoService.save();
}
}
此處不再采用注入的方式獲取DemoService對(duì)象,而是通過工具類的方式
Controller
@ResponseBody
@GetMapping("/fun")
public void fun(){
CommonClass commonClass = new CommonClass();
commonClass.fun();
}
再次運(yùn)行程序,一切正常
應(yīng)用
在SpringBoot整合Shiro的案例中,自定義Realm時(shí),需要使用Service的對(duì)象。因?yàn)樽远x的Realm類不能使用@Component之類的注解注釋,所以使用本案例介紹的方法是正確的解決方案。盡管在1.6.0的shiro-all中下面代碼可以正確運(yùn)行:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- springboot如何獲取applicationContext?servletContext
- SpringBoot?容器刷新前回調(diào)ApplicationContextInitializer
- SpringBoot?ApplicationContext接口深入分析
- SpringBoot如何使用applicationContext.xml配置文件
- Springboot如何獲取上下文ApplicationContext
- springboot ApplicationContextInitializer的三種使用方法小結(jié)
- SpringBoot獲取ApplicationContext的3種方式
- SpringBoot ApplicationContextAware拓展接口使用詳解
相關(guān)文章
SpringMVC 參數(shù)綁定之視圖傳參到控制器的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringMVC 參數(shù)綁定之視圖傳參到控制器的相關(guān)知識(shí),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
java request.getParameter中文亂碼解決方法
今天跟大家分享幾個(gè)解決java Web開發(fā)中,request.getParameter()獲取URL中文參數(shù)亂碼的解決辦法,需要的朋友可以參考下2020-02-02
MyBatis-Plus通用枚舉自動(dòng)關(guān)聯(lián)注入的實(shí)現(xiàn)
本文主要介紹了MyBatis-Plus通用枚舉自動(dòng)關(guān)聯(lián)注入的實(shí)現(xiàn),解決了繁瑣的配置,讓 mybatis 優(yōu)雅的使用枚舉屬性,感興趣的可以一起來了解一下2021-06-06
Java實(shí)現(xiàn)讀取Jar文件屬性的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)讀取Jar文件屬性的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-08-08
Idea運(yùn)行單個(gè)main方法,不編譯整個(gè)工程的問題
這篇文章主要介紹了Idea運(yùn)行單個(gè)main方法,不編譯整個(gè)工程的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
mybatis xml如何使用not in 某個(gè)集合的格式
這篇文章主要介紹了mybatis xml如何使用not in 某個(gè)集合的格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
JFreeChart簡(jiǎn)單實(shí)現(xiàn)光滑曲線繪制
這篇文章主要為大家詳細(xì)介紹了JFreeChart簡(jiǎn)單實(shí)現(xiàn)光滑曲線的繪制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06

