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
posted @ 2022-08-22 14:28  ppjj  阅读(4866)  评论(0编辑  收藏  举报