SpringBoot 获取上下文,获取bean的几种中方式

传统Spring项目

在写传统的spring项目中,一般通过初始化抽象类AbstractXmlApplicationContext 的实现类,并传入spring.xml,来获取应用上下文,最终通过getBean方法获取bean,如下:

ApplicationContext app1 = new FileSystemXmlApplicationContext("applicationContext.xml");
app1.getBean("beanName");
ApplicationContext app2 = new ClassPathXmlApplicationContext("applicationContext.xml");
app2.getBean("beanName");

SpringBoot项目获取bean的几种方式

1. 通过启动类中返回的上下文获取

ConfigurableApplicationContext app = SpringApplication.run(BeanDemoApplication.class, args);
SpringUtil.setAppContext(app);
public class SpringUtil {
private static ApplicationContext appContext;
public static void setAppContext(ApplicationContext appContext) {
SpringUtil.appContext = appContext;
}
public static ApplicationContext getAppContext() {
return appContext;
}
}

在第三方类中使用:

ApplicationContext appContext = SpringUtil.getAppContext();
appContext.getBean("beanName");

2. 通过工具类获取

RequestContextUtils.findWebApplicationContext(HttpServletRequest request),WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)
a. 在controller中传入request,例如:

public String test(HttpServletRequest request,HttpServletRequest response) {
WebApplicationContext wc = RequestContextUtils.findWebApplicationContext(request);
wc.getBean("beanName");
WebApplicationContext wc2 = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
wc2.getBean("beanName");
}

b. 在service中或者其他后端服务中获取:

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
WebApplicationContext wc = RequestContextUtils.findWebApplicationContext(request);
WebApplicationContext wc2 = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
wc.getBean("beanName");
wc2.getBean("beanName");

3. 通过实现接口ApplicationContextAware

@Component
public class TestApplicationContextAware implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
public ApplicationContext getApplicationContext() {
return applicationContext;
}
}

在其他类中调用

@Autowired
private TestApplicationContextAware app;
public void testMethod() {
app.getBean("beanName");
}

4. 通过继承抽象类:ApplicationObjectSupport,WebApplicationObjectSupport

原理参考第3点

5. 其他方式

在网上看,发现也可以直接调用:ContextLoader.getCurrentWebApplicationContext(),或者 ContextLoaderListener.getCurrentWebApplicationContext() 其实都是调用同一段代码,如下:

@Nullable
public static WebApplicationContext getCurrentWebApplicationContext() {
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl != null) {
WebApplicationContext ccpt = currentContextPerThread.get(ccl);
if (ccpt != null) {
return ccpt;
}
}
return currentContext;
}

说明:目前通过这种方式获取上下文为null,从代码可以看出,上下文是通过currentContextPerThread.get(ccl) 来获取的,而currentContextPerThread缓存是通过方法contextInitialized(ServletContextEvent event) 来初始化的,至于为何获取为空

posted @   三号小玩家  阅读(2208)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2021-01-04 基于 Docker 安装 RocketMQ
Title
三号小玩家的 Mail: 17612457115@163.com, 联系QQ: 1359720840 微信: QQ1359720840

喜欢请打赏

扫描二维码打赏

支付宝打赏

点击右上角即可分享
微信分享提示