如何手动获取Spring容器中的bean(ApplicationContextAware 接口)

本文介绍如何通过实现ApplicationContextAware接口来获取Spring容器中的Bean。具体步骤包括定义工具类、在Spring配置文件中注册该工具类及实际使用方法。

ApplicationContextAware 接口的作用

这个接口有什么用?

当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。


如何使用 ApplicationContextAware 接口

如何使用该接口?很简单。

1、定义一个工具类,实现 ApplicationContextAware,实现 setApplicationContext方法

public class SystemContext implements ApplicationContextAware {
	
    private static ApplicationContext applicationContext;

    @SuppressWarnings("rawtypes")
    public static Object getBean(Object beanName) {
        if (beanName instanceof Class) {
            return applicationContext.getBean(((Class) beanName).getName());
        } else {
            return applicationContext.getBean(beanName.toString());
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SystemContext.applicationContext = applicationContext;
    }
    
}


如此一来,我们就可以通过该工具类,来获得 ApplicationContext,进而使用其getBean方法来获取我们需要的bean。

2、在Spring配置文件中注册该工具类

一、因为spring要建立属于自己的容器,就必须要加载自己的配置文件。

这个时候,需要注册ContextLoaderListener或者这个类的子类。

在web.xml加上以下的信息:

1
2
3
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

二、之所以我们能如此方便地使用该工具类来获取,正是因为Spring能够为我们自动地执行 setApplicationContext 方法,显然,这也是因为IOC的缘故,所以必然这个工具类也是需要在Spring的配置文件中进行配置的。
  1. <!-- 初始化SystemContext,注入applicationContext -->     <bean id="SystemContext" class="com.framework.system.engine.SystemContext" lazy-init="false"></bean>

3、编写方法进行使用

一切就绪,我们就可以在需要使用的地方调用该方法来获取bean了。
private static ShineApprovalService getShineApprovalService() {
     return (ShineApprovalService) SystemContext.getBean(ShineApprovalServiceImpl.BEAN_NAME);
}


<think>好的,我现在需要帮助用户解决在Spring拦截器中如何获取容器中的Bean的问题。用户之前已经询问过过滤器和拦截器的区别,现在根据提供的引用内容,用户可能遇到了在拦截器中自动注入Bean为null的情况,想知道如何正确获取Bean。 首先,我要回顾用户提供的引用内容。引用[2]提到在拦截器中@Autowired注入的Bean为null,这可能是因为拦截器的实例化方式导致的。拦截器通常由Spring MVC配置,但如果没有正确注册Spring容器中,拦截器本身不会被Spring管理,因此自动注入会失败。 接下来,我需要考虑解决方案。常见的解决方法包括: 1. **使用WebMvcConfigurer注册拦截器并手动注入Bean**:通过配置类将拦截器实例添加到Spring容器,并确保在注册拦截器时使用已经由Spring管理的Bean。例如,在配置类中使用@Bean创建拦截器实例,这样Spring会自动处理依赖注入。 2. **让拦截器实现BeanFactoryAware接口**:通过实现BeanFactoryAware接口,拦截器可以获取BeanFactory,从而通过它来获取需要的Bean。这种方法允许在拦截器内部手动获取Bean实例。 3. **使用ApplicationContextAware工具类**:创建一个工具类实现ApplicationContextAware,保存ApplicationContext的静态引用。然后在拦截器中通过这个工具类获取Bean。这种方法适用于需要全局访问ApplicationContext的情况。 此外,需要解释为什么直接使用@Autowired会失败,主要是因为拦截器可能没有被Spring容器管理,导致依赖注入无法进行。需要强调拦截器必须由Spring创建实例,才能使用自动注入。 最后,要确保回答结构清晰,分步骤说明每种方法的实现,并给出代码示例。同时,提醒用户注意拦截器的注册方式,确保它们被Spring正确管理。</think>### Spring 拦截器中获取容器 Bean 的 3 种方法 在拦截器中直接使用 `@Autowired` 注解注入 Bean 会失败(如引用[2]所述),因为 **拦截器未被 Spring 容器管理**。以下是可用的解决方案: --- #### 方法 1:通过 `WebMvcConfigurer` 手动注入(推荐) 通过 Spring 配置类显式注册拦截器并注入依赖,确保拦截器实例由 Spring 管理。 ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Autowired // 注入 Spring 容器中的 Bean private ToolsService toolsService; @Override public void addInterceptors(InterceptorRegistry registry) { // 手动创建拦截器实例并注入依赖 registry.addInterceptor(new MyInterceptor(toolsService)) .addPathPatterns("/api/**"); } } // 拦截器改造为通过构造函数接收依赖 public class MyInterceptor implements HandlerInterceptor { private final ToolsService toolsService; public MyInterceptor(ToolsService toolsService) { this.toolsService = toolsService; } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { System.out.println(toolsService); // 正常输出 Bean 对象 return true; } } ``` **原理**:通过构造函数显式传递依赖,确保拦截器实例持有 Spring 管理的 Bean[^2]。 --- #### 方法 2:实现 `BeanFactoryAware` 接口 通过 Spring 的 `BeanFactoryAware` 接口直接获取容器对象。 ```java public class MyInterceptor implements HandlerInterceptor, BeanFactoryAware { private BeanFactory beanFactory; private ToolsService toolsService; @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; // 延迟加载 Bean(避免初始化顺序问题) this.toolsService = beanFactory.getBean(ToolsService.class); } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { System.out.println(toolsService); // 正常输出 return true; } } ``` **注意**:需在 Spring 配置类中注册该拦截器,确保 Spring 调用 `setBeanFactory` 方法[^2]。 --- #### 方法 3:使用 `ApplicationContextAware` 工具类 创建全局工具类获取 Spring 上下文,通过上下文获取 Bean。 ```java @Component public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil.applicationContext = applicationContext; } public static <T> T getBean(Class<T> clazz) { return applicationContext.getBean(clazz); } } // 拦截器中调用工具类 public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { ToolsService toolsService = SpringContextUtil.getBean(ToolsService.class); System.out.println(toolsService); // 正常输出 return true; } } ``` **适用场景**:需要跨多个非 Spring 管理类获取 Bean 的情况。 --- ### 核心原理 - **拦截器未被 Spring 管理**:默认情况下,拦截器实例由 MVC 框架直接创建,而非通过 Spring 容器,因此无法自动注入。 - **依赖传递**:通过构造函数或工具类显式传递 Spring 容器中的 Bean,绕过自动注入的限制。 --- ### 扩展问题 1. 为什么 `@Component` 注解无法让拦截器自动注入 Bean? 2. 如何避免 `ApplicationContextAware` 工具类引起的循环依赖? 3. 拦截器中使用 `@Autowired` 注解的 Bean 何时会初始化?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值