最近在用监听器的时候遇到了spring无法注入的问题,代码如下,这个task总是null,包明明已经被扫到了,就是注入不进来。

public class MyListener implements ServletContextListener {

   @Autowired
private TaskThread taskThread; @Override
public void contextInitialized(ServletContextEvent sce) {
task.doThread0();
} @Override
public void contextDestroyed(ServletContextEvent sce) { } }

原因如下:在Listener监听器中无法使用Spring容器的@Resource或者@Autowired 注解的方法注入bean,因为,在web Server容器中,无论是Servlet,Filter,还是Listener都不是Spring容器管理的,因此我们都无法在这些类中直接使用Spring注解的方式来注入我们需要的对象。在这里,Servlet的整个生命周期都是由Servlet容器来处理的。如果把它硬放到Spring容器中去创建,Servlet对象是可被Spring容器建出来,但Servlet容器可能跟本就不知道这个Servlet是否存在,因为不在它自己的容器中。所以,servlet交给web server来管理,不要交给spring管理。

做如下修改:

从spring的上下文中获取,完美解决了问题。

public class MyListener implements ServletContextListener {

    @Override
public void contextInitialized(ServletContextEvent sce) {
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
TaskThread task = context.getBean(TaskThread.class);
task.doThread0();
} @Override
public void contextDestroyed(ServletContextEvent sce) { } }

Listener中@Autowired无法注入的问题的更多相关文章

  1. listener中@Autowired无法注入bean的一种解决方法

    背景:使用监听器处理业务,需要使用自己的service方法: 错误:使用@Autowired注入service对象,最终得到的为null: 原因:listener.fitter都不是Spring容器管 ...

  2. Java Listener中Spring接口注入的使用

    在项目中使用Spring通常使用他的依赖注入可以很好的处理,接口与实现类之间的耦合性,但是通常的应用场景中都是Service层和DAO层,或者web层的话, 也是与Strust2来整合,那么如何在Li ...

  3. struts2 action 中autowired 不能注入

    一.pom.xml <dependency> <groupId>org.apache.struts</groupId> <artifactId>stru ...

  4. 关于工具类中@Autowired注入为NULL的问题记录

      记录:在实体类中加入@Component注解和@Autowired注解时Service不能注入成功. @Component //把普通pojo实例化到spring容器中 0 public clas ...

  5. 关于如何在Listener中注入service和ServletContextListener源码分析

      今天在做项目时突然发现我该如何向listener中注入service对象,因为监听器无法使用注解注入. 此时有人会想用以下代码通过xml的方式注入: ApplicationContext cont ...

  6. spring自定义类中@AutoWired标识的元素注入为null

    最近在做项目的时候,发现程序运行的时候有一个nullpointer exception,一脸懵逼因为感觉程序没什么逻辑.后来发现是因为new出来的component不会自动注入它的元素. 现象:@Co ...

  7. 于工具类中@Autowired注入为NULL的问题记录

      记录:在实体类中加入@Component注解和@Autowired注解时Service不能注入成功. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...

  8. spring boot 中@Autowired注解无法自动注入的错误

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...

  9. 在ContextLoaderListener中使用注解注入的类和job中使用注解注入的类

    场景:在ContextLoaderListener子类中加载job,为JobFactory的实现类声明@Component后,在ContextLoaderListener子类中为scheduler设置 ...

随机推荐

  1. NGINX---一次阿里云宝塔开发flask经历

    1.放行端口问题 不但需要在阿里云官网服务器控制台放行端口,还需要在宝塔里面放行端口 2.nginx 宝塔默认的用户是www 句法: user user [group]; 默认: 用户无人; 语境: ...

  2. javascript常用小案例

    常用javascript小案例 样式调节 //注: 这个可以控制td中的字段成行显示 #modelInfos td,th { white-space: nowrap; } //文本输入框随着内容尺寸往 ...

  3. English Grammar in Use - Part1 Present and past

    Unit 1 Present continuous (I am doing) A) Am/is/are + -ing is the Present continuous. B) I am doing ...

  4. 组件上使用v-model

    组件上使用v-model <input v-model="searchText"> 等价于 <input v-bind:value="searchTex ...

  5. L2范数归一化概念和优势

    1 归一化处理        归一化是一种数理统计中常用的数据预处理手段,在机器学习中归一化通常将数据向量每个维度的数据映射到(0,1)或(-1,1)之间的区间或者将数据向量的某个范数映射为1,归一化 ...

  6. .ajaxStart() / .ajaxStop() —— ajax请求开始时 / 结束时触发

    一..ajaxStart()——ajax请求开始时触发  描述:ajax请求开始时触发 .ajaxStart()的回调函数,全局的,所有的ajax都可以用 写法:元素.ajaxStart(functi ...

  7. DevExtreme学习笔记(一) DataGrid中js分析

    1.overviewjs采用 $(function() { $("#gridContainer").dxDataGrid({ dataSource: { store: { type ...

  8. win10 总是很快自动关机 无人参与系统睡眠超时设置

    解决WIN10隔几分钟就自动黑屏睡眠的方法!_Win10之家原文是卸载了电源驱动,下面是在评论里看到的方法: 这是系统无人值守时睡眠时间的设定,默认是两分钟.解决方法:1.运行注册表管理器,win+r ...

  9. C# 中的匿名函数使用

    需求:在图一的callback函数中,我需要使用4个参数,但是又不想把四个参数都传入到requestImg 里面,可以采用上面的 匿名函数的做法.

  10. ubuntu18.04 下启动Android Studio报错KVM is required to run this AVD. /dev/kvm device: permission denied.

    在ubuntu18.04下安装Android Studio,安装了模拟器后运行报错 KVM is required to run this AVD. /dev/kvm device: permissi ...