listener中@Autowired无法注入bean

文章讲述了在Spring应用中,由于监听器和fitter不是Spring容器管理的,不能使用@Autowired自动注入。解决方案是实现ApplicationContextAware接口,创建一个SpringJobBeanFactory来从WebApplicationContext中获取Bean。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做导入时使用监听器处理业务,需要使用自己的service方法;

错误:使用@Autowired注入service对象,最终得到的为null;

原因:listener、fitter都不是Spring容器管理的,无法在这些类中直接使用Spring注解的方式来注入我们需要的对象。

解决:写一个bean工厂,从spring的上下文WebApplicationContext 中获取。

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
* @ClassName: SpringJobBeanFactory*/
@Component
public class SpringJobBeanFactory implements ApplicationContextAware {

    
    private static ApplicationContext applicationContext;
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringJobBeanFactory.applicationContext=applicationContext;
        
    }
     public static ApplicationContext getApplicationContext() {
            return applicationContext;
    }
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
            if (applicationContext == null){
                return null;
            }
            return (T)applicationContext.getBean(name);
      }
    
    public static <T> T getBean(Class<T>  name) throws BeansException {
            if (applicationContext == null){
                return null;
            }
            return applicationContext.getBean(name);
      }
}

监听器里获取service,使用getBean(XXX.class)

  AccountService accountService = SpringJobBeanFactory.getBean(AccountService.class);

原文链接:https://www.likecs.com/show-305376150.html

### 如何解决 Spring 拦截器中 Bean 注入失败问题 当遇到在拦截器中无法注入 `Bean` 的情况时,主要原因是由于拦截器被手动实例化而不是由 Spring 容器管理。这导致了即使使用了 `@Component` 或者其他注解也无法正常注入所需的 `Bean`。 #### 方法一:让 Spring 管理拦截器的生命周期 为了使拦截器能够成功获取到容器中的 `Bean`,应该避免直接通过 `new` 关键字创建拦截器对象。相反,在定义拦截器配置类时应当利用 `addInterceptors()` 方法来注册拦截器,并确保该方法返回的是由 Spring 创建并托管的对象而非自行构建的新实例[^1]。 ```java @Configuration public class WebConfig implements WebMvcConfigurer { private final MyInterceptor myInterceptor; public WebConfig(MyInterceptor myInterceptor){ this.myInterceptor = myInterceptor; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor); } } ``` 这样做的好处在于可以让 Spring 自动完成依赖关系的装配工作,从而使得所有的 `Bean` 都可以按照预期的方式运作起来。 #### 方法二:调整初始化顺序 另一种解决方案是改变应用程序启动过程中某些组件加载的时间点。具体来说就是在应用上下文刷新之后再向框架内添加自定义逻辑(比如设置拦截器)。可以通过监听事件的方式来实现这一点: ```java @Component public class ApplicationStartupListener implements ApplicationListener<ApplicationReadyEvent> { private final InterceptorRegistry interceptorRegistry; public ApplicationStartupListener(InterceptorRegistry interceptorRegistry){ this.interceptorRegistry = interceptorRegistry; } @Override public void onApplicationEvent(ApplicationReadyEvent event) { // 此处可安全地操作interceptorRegistry,因为此时所有beans都已准备好 interceptorRegistry.addInterceptor(new CustomInterceptor()); } } ``` 不过这种方法相对复杂些,通常推荐采用第一种方式即保证拦截器是由Spring负责管理和实例化的[^2]。 #### 方法三:延迟初始化 对于那些确实需要提前注册但又不想影响整体流程的情况,则可以选择延迟初始化策略。这意味着只有当实际请求到来时才会真正去尝试访问相应的资源和服务。这种方式适用于特定场景下的优化需求[^4]。 ```java @RestControllerAdvice public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { @Autowired(required=false) private transient RedisTemplate<String, Object> redisTemplate; @PostConstruct protected void init() { if (redisTemplate == null) { ApplicationContext context = ... ;// 获取ApplicationContext的方法 redisTemplate = context.getBean(RedisTemplate.class); } } } ``` 以上三种途径都可以有效地应对 Spring Boot 中常见的拦截器 `Bean` 注入失败的问题。选择哪种取决于具体的业务背景和个人偏好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值