SpringBoot获取上下文,获取bean的几种中方式
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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;
}。