zoukankan      html  css  js  c++  java
  • Spring中的ApplicationListener和ServletContextListener的区别

    ServletContextListener:是对javax.servlet.ServletContext(application)的监听,Tomcat/Jetty容器启动后就执行;

    ApplicationListener:是对Spring的ApplicationContext的监听,Spring应用初始化完成后。

    ServletContextListener:

      1、依赖于sevlet容器,需要配置web.xml(Spring Boot只需要配置@WebListener即可,并且使用@WebListener后,可以注入bean)

      2、需要重写contextInitialized方法,public void contextInitialized(ServletContextEvent event),在ServletContext被创建时调用

      3、public void contextDestroyed(ServletContextEvent event):在ServletContext被销毁时调用

    ApplicationListener:

      1、依赖于Spring框架,在Spring启动时调用。在普通Spring应用中一般监听ContextRefreshedEvent事件。

      2、重写onApplicationEvent方法,public void onApplicationEvent(ContextRefreshedEvent event) ,用于容器初始化完成之后,执行需要处理的一些操作

      3、在普通Spring环境中,基于ApplicationListener的监听器的onApplicationEvent方法可能会被执行多次,所以需要添加以下判断:

      if(event.getApplicationContext().getParent() == null)

  • 相关阅读:
    内存
    TCP/IP
    安装
    linux常用命令
    linux文本处理三剑客之 grep
    tail命令:显示文件结尾的内容
    less命令:查看文件内容
    head命令:显示文件开头内容
    改进Zhang Suen细化算法的C#实现
    【转】在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
  • 原文地址:https://www.cnblogs.com/wangsizheng/p/13690039.html
Copyright © 2011-2022 走看看