• java 如何在listener(监听器) 中使用Spring容器管理bean


    问题来源:在Listener监听器中无法使用Spring容器的@Resource或者@Autowired 注解的方法注入bean,因为,在web Server容器中,无论是Servlet,Filter,还是Listener都不是Spring容器管理的,因此我们都无法在这些类中直接使用Spring注解的方式来注入我们需要的对象。

    在这里,Servlet的整个生命周期都是由Servlet容器来处理的。如果把它硬放到Spring容器中去创建,Servlet对象是可被Spring容器建出来,但Servlet容器可能跟本就不知此Servlet存在,因不在它的容器中。所以,servlet交给web server来管理,不要交给spring管理。


    我们在Java web 应用中会使用到监听器来完成一些操作,比如说使用Session监听器来监听Session的变化,通常情况下我们会用javaee规范中的Listener去实现,例如

    1. public class TestListener implements HttpSessionAttributeListener,ServletContextListener
    2. {
    3. @Override
    4. public void attributeAdded(HttpSessionBindingEvent arg0)
    5. {
    6. // TODO Auto-generated method stub
    7. }
    8. @Override
    9. public void attributeRemoved(HttpSessionBindingEvent arg0)
    10. {
    11. // TODO Auto-generated method stub
    12. }
    13. @Override
    14. public void attributeReplaced(HttpSessionBindingEvent arg0)
    15. {
    16. // TODO Auto-generated method stub
    17. }
    18. @Override
    19. public void contextDestroyed(ServletContextEvent arg0)
    20. {
    21. // TODO Auto-generated method stub
    22. }
    23. @Override
    24. public void contextInitialized(ServletContextEvent arg0)
    25. {
    26. // TODO Auto-generated method stub
    27. }
    28. }

    现在,我们想在这个监听器里面注入bean,如果使用Spring注解的方式注入bean如下面这样:

    1. @Resource
    2. private AdvertisementServiceImpl advertisementServiceImpl;

    然而以上代码会在项目启动时抛出空指针异常!AdvertisementServiceImpl的实例并没有成功注入。这是为什么呢?要理解这个问题,首先要区分Listener的生命周期和spring管理的bean的生命周期。

    (1)Listener的生命周期是由servlet容器(例如tomcat)管理的,项目启动时上例中的TestListener是由servlet容器实例化并调用其contextInitialized方法,而servlet容器并不认得@Resource注解,因此导致AdvertisementServiceImpl实例注入失败。

    (2)而spring容器中的bean的生命周期是由spring容器管理的。

    那么该如何在spring容器外面获取到spring容器bean实例的引用呢?这就需要用到spring为我们提供的WebApplicationContextUtils工具类,该工具类的作用是获取到spring容器的引用,进而获取到我们需要的bean实例。代码如下

    1. @Override
    2. public void contextInitialized(ServletContextEvent sce) {
    3. WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
    4. this.advertisementServiceImpl=(AdvertisementServiceImpl)applicationContext.getBean("advertisementServiceImpl");
    5. }
    然后在spring配置:

     <bean id="xxxService">xxx.xxx.xxx.xxxService</bean>





    来源:https://blog.csdn.net/wewewfs/article/details/76075615
  • 相关阅读:
    第九章、硬件抽象层:HAL
    第八章、让开发板发出声音:蜂鸣器驱动
    第七章、LED将为我闪烁:控制发光二极管
    第六章、第一个Linux驱动程序:统计单词个数
    第五章、搭建S3C6410开发板的测试环境
    Android深度探索(卷1)HAL与驱动开发
    第三次月考
    第二次月考
    Android深度探索(卷1)HAL与驱动开发
    第六章 集合运算
  • 原文地址:https://www.cnblogs.com/konglxblog/p/16227103.html
Copyright © 2020-2023  润新知