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

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- springboot如何獲取applicationContext?servletContext
- SpringBoot?容器刷新前回調(diào)ApplicationContextInitializer
- SpringBoot?ApplicationContext接口深入分析
- SpringBoot如何使用applicationContext.xml配置文件
- Springboot如何獲取上下文ApplicationContext
- springboot ApplicationContextInitializer的三種使用方法小結
- SpringBoot獲取ApplicationContext的3種方式
- SpringBoot ApplicationContextAware拓展接口使用詳解
相關文章
SpringMVC 參數(shù)綁定之視圖傳參到控制器的實現(xiàn)代碼
這篇文章主要介紹了SpringMVC 參數(shù)綁定之視圖傳參到控制器的相關知識,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
java request.getParameter中文亂碼解決方法
今天跟大家分享幾個解決java Web開發(fā)中,request.getParameter()獲取URL中文參數(shù)亂碼的解決辦法,需要的朋友可以參考下2020-02-02
MyBatis-Plus通用枚舉自動關聯(lián)注入的實現(xiàn)
本文主要介紹了MyBatis-Plus通用枚舉自動關聯(lián)注入的實現(xiàn),解決了繁瑣的配置,讓 mybatis 優(yōu)雅的使用枚舉屬性,感興趣的可以一起來了解一下2021-06-06

