5种方式获取ApplicationContext
第一种直接注入
@Resource
private ApplicationContext ctx;
第二种实现ApplicationContextAware接口
创建一个实体类并实现ApplicationContextAware接口,重写接口内的setApplicationContext方法来完成获取ApplicationContext实例的方法,代码如下所示:
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class ApplicationContextProvider implements ApplicationContextAware { /** * 上下文对象实例 */ private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext;
/ //或者这么写
String className = Thread currentThread().getStackTrace()[2].getClassName();
if(className.contains("ApplicationContextAwareProcessor")){
this.applicationContext = applicationContext;
} } /** * 获取applicationContext * @return */ public ApplicationContext getApplicationContext() { return applicationContext; } /** * 通过name获取 Bean. * @param name * @return */ public Object getBean(String name){ return getApplicationContext().getBean(name); } /** * 通过class获取Bean. * @param clazz * @param <T> * @return */ public <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } /** * 通过name,以及Clazz返回指定的Bean * @param name * @param clazz * @param <T> * @return */ public <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); } }
我们拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象,在ApplicationContextProvider类内我简单的实现了几个方法来获取指定的Bean实例,当然你可以添加更多的方法来完成更多的业务逻辑。
如果你是想在非Spring管理的实体内使用ApplicationContext还不想采用注入ApplicationContextProvider来完成实例化,这时我们可以修改ApplicationContext实例对象为静态实例,方法改为静态方法,这样在外部同样是可以获取到指定Bean的实例。
这里要注意ApplicationContextProvider类上的@Component注解是不可以去掉的,去掉后Spring就不会自动调用setApplicationContext方法来为我们设置上下文实例。
第三种在自定义AutoConfiguration中获取
有时候我们需要实现自定义的Spring starter,并在自定义的AutoConfiguration中使用ApplicationContext,Spring在初始化AutoConfiguration时会自动传入ApplicationContext,这时我们就可以使用下面的方式来获取ApplicationContext:
@Configuration @EnableFeignClients("com.yidian.data.interfaces.client") public class FeignAutoConfiguration { FeignAutoConfiguration(ApplicationContext context) { // 在初始化AutoConfiguration时会自动传入ApplicationContext doSomething(context); } }
第四种启动时获取ApplicationContext
在启动Spring Boot项目时,需要调用SpringApplication.run()方法,而run()方法的返回值就是ApplicationContext,我们可以把run()方法返回的ApplicationContext对象保存下来,方便随时使用:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class WebApplication { private static ApplicationContext applicationContext; public static void main(String[] args) { applicationContext = SpringApplication.run(WebApplication.class, args); SpringBeanUtil.setApplicationContext(applicationContext); } }
第五种通过WebApplicationContextUtils获取
Spring提供了一个工具类用于获取ApplicationContext对象:
WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
本文转自:https://www.jianshu.com/p/ef7739a01cb0
分类:
sping
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?