最近在用监听器的时候遇到了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. Angular 发布订阅模式实现不同组件之间通讯

    在我们项目中要实现不同组件之间通讯,Angular的@Input和@Output只能实现有父子组件的限制,如果是复杂跨组件实现不同组件可以通过共享变量的方式实现,比如这个博客的思路:https://w ...

  2. CentOS 7命令行安装GNOME桌面

    1.切换至root用户,检查一下已经安装的软件以及可以安装的软件 [root@localhost ~]# yum grouplistLoaded plugins: fastestmirrorThere ...

  3. PHP 生成公钥私钥,加密解密,签名验签

    test_encry.php <?php //创建私钥,公钥 //create_key(); //要加密内容 $str = "test_str"; //加密 $encrypt ...

  4. 6.66 分钟,一文Python爬虫解疑大全教入门!

    我收集了大家关注爬虫最关心的  16 个问题,这里我再整理下分享给大家,并一一解答. 1. 现在爬虫好找工作吗? 如果是一年前我可能会说爬虫的工作还是挺好找的,但现在已经不好找了,一市场饱和了,二是爬 ...

  5. C++_自引用指针this

    自引用指针this 例 3.1 this指针的引例 #include<iostream.h> class A{ public: A(int x1){ x=x1; } void disp() ...

  6. 【Linux】Shell批量修改文件名

    修改文件名,替换中间字符: 例如:ABC_define_EFG.jpg,要把中间的define替换成argument: 用如下脚本即可: for var in *; do mv "$var& ...

  7. VMware Workstation Pro 15 序列号

    VMware Workstation Pro 15 序列号: GA70H-8TYE2-H886P-04YZC-YVA84 YG5H2-ANZ0H-M8ERY-TXZZZ-YKRV8 UG5J2-0ME ...

  8. vmware vSphere Data Protection 6.1--------2-初始化

    一.简介 安装完vdp接下来就是部署初始化了 安装篇请参考:vmware vSphere Data Protection 6.1部署 二.开始初始化 登陆https://192.168.216.200 ...

  9. 如何封装属于自己的WPF控件库

    在网上找了一下,发现这方面的资料并不多.做传统桌面的本来就不多了吧,更别说WPF了.我可能也要另寻出路了,不过我还是觉得做桌面挺有意思的. 言归正传  首先,新建一个WPF自定义控件库项目 这里我们封 ...

  10. React 脚手架支持Typescript和Sass

    首先,创建React工程目录,以及选择Typescript版本 进入在my-app目录,安装node-sass 然后再安装webpack的sass-loader  接下来进入node_modules ...