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); } }
通過(guò)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 沒(méi)有使用@Component標(biāo)注,所以在Controller中不能才用注入方式生成CommonClass對(duì)象,而是才用new的方式生成了該對(duì)象。
這樣一來(lái),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ì)象,而是通過(guò)工具類的方式
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)文章
SpringBoot集成Swagger構(gòu)建api文檔的操作
這篇文章主要介紹了SpringBoot集成Swagger構(gòu)建api文檔的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12MyBatis-Plus+Druid配置及應(yīng)用詳解
這篇文章主要介紹了MyBatis-Plus+Druid配置及應(yīng)用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Spring Boot企業(yè)常用的starter示例詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot企業(yè)常用starter的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12springboot整合vue2-uploader實(shí)現(xiàn)文件分片上傳、秒傳、斷點(diǎn)續(xù)傳功能
對(duì)于大文件的處理,無(wú)論是用戶端還是服務(wù)端,如果一次性進(jìn)行讀取發(fā)送、接收都是不可取,很容易導(dǎo)致內(nèi)存問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于springboot整合vue2-uploader實(shí)現(xiàn)文件分片上傳、秒傳、斷點(diǎn)續(xù)傳功能的相關(guān)資料,需要的朋友可以參考下2023-06-06Java Runnable線程傳參,實(shí)現(xiàn)讓run訪問(wèn)參數(shù)
這篇文章主要介紹了Java Runnable線程傳參,實(shí)現(xiàn)讓run訪問(wèn)參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09劍指Offer之Java算法習(xí)題精講二叉樹(shù)專題篇上
跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過(guò)之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03