• spring项目中监听器作用-ContextLoaderListener


    spring项目中监听器作用-ContextLo

    spring框架的启动入口 ContextLoaderListener

     

    1. <context-param>  
    2. <param-name>contextConfigLocation</param-name>  
    3. <param-value>contextConfigLocationValue></param-value>  
    4. </context-param>  

    作用:该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数。

    param-name 设定上下文的参数名称。必须是唯一名称

    param-value 设定的参数名称的值

    • 初始化过程:
      1. 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>。
      2. 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。
      3. 接着容器会将读取到<context-param>转化为键值对,并交给ServletContext。
      4. 容器创建<listener></listener>中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。
      5. 在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。
      6. 得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早。

    2 作用:在启动Web 容器时,自动装配Spring applicationContext.xml 的配置信息。

     

    因为它实现了ServletContextListener 这个接口,在web.xml 配置这个监听器,启动容器时,就会默认执行它实现的方法。在ContextLoaderListener 中关联了ContextLoader 这个类,所以整个加载配置过程由ContextLoader 来完成

     

     

     

    pring 在 web 下的入口在配置文件 web.xml 的监听器中

     

    < listener >

     

            < listener-class >

     

               org.springframework.web.context.ContextLoaderListener

     

            </ listener-class >

     

    </ listener >

     

    <context-param>

     

         <param-name>contextConfigLocation</param-name>

     

          <param-value>classpath:conf/spring/applicationContext.xml</param-value>

     

    </context-param>

     

    上述是在web.xml 中的配置信息。

     

     

     

    // 实现了接口 ServletContextListener, 也就是说他必须实现 contextDestroyed, contextInitialized 这两个方法

     

    public class ContextLoaderListener implements ServletContextListener {

     

           private ContextLoader contextLoader;

     

           /**

     

           * Initialize the root web application context.

     

           */

     

    //Spring 框架由此启动 , contextInitialized 也就是监听器类的 main 入口函数

     

           public void contextInitialized (ServletContextEvent event) {

     

                  this.contextLoader = createContextLoader();

     

                  this.contextLoader.initWebApplicationContext(event.getServletContext());

     

           }

     

           /**

     

           * Create the ContextLoader to use. Can be overridden in subclasses.

     

           * @return the new ContextLoader

     

           */                                            

     

           protected ContextLoader createContextLoader() {

     

                  return new ContextLoader();

     

           }

     

           /**

     

           * Return the ContextLoader used by this listener.

     

           * @return the current ContextLoader

     

           */

     

           public ContextLoader getContextLoader() {

     

                  return this.contextLoader;

     

           }

     

           /**

     

           * Close the root web application context.

     

           */

     

           public void contextDestroyed (ServletContextEvent event) {

     

                  if (this.contextLoader != null) {

     

                         this.contextLoader.closeWebApplicationContext(event.getServletContext());

     

                  }

     

           }

     

    }

     

     

    总的来说这个入口非常简单 , 所有实现都隐藏在 ContextLoader 类里 , 我们在下一篇的内容中讨论ContextLoader, 如果你不知道为什么这里是程序的入口 , 那么复习一下 ServletContextListener 接口和监听器的相关知识吧

     

       ServletContext 被 Servlet 程序用来与 Web 容器通信。例如写日志,转发请求。每一个 Web 应用程序含有一个Context ,被Web 应用内的各个程序共享。因为Context 可以用来保存资源并且共享,所以我所知道的ServletContext 的最大应用是Web 缓存---- 把不经常更改的内容读入内存,所以服务器响应请求的时候就不需要进行慢速的磁盘I/O 了。

     

       ServletContextListener 是 ServletContext 的监听者,如果 ServletContext 发生变化,如服务器启动时ServletContext 被创建,服务器关闭时 ServletContext 将要被销毁。

     

           在JSP 文件中,application 是 ServletContext 的实例,由JSP 容器默认创建。Servlet 中调用getServletContext() 方法得到 ServletContext 的实例。

     

    我们使用缓存的思路大概是:

     

           1. 服务器启动时,ServletContextListener 的 contextInitialized() 方法被调用,所以在里面创建好缓存。可以从文件中或者从数据库中读取取缓存内容生成类,用 ervletContext.setAttribute() 方法将缓存类保存在ServletContext 的实例中。

     

           2. 程序使用 ServletContext.getAttribute() 读取缓存。如果是 JSP ,使用a pplication.getAttribute() 。如果是Servlet ,使用 getServletContext().getAttribute() 。如果缓存发生变化( 如访问计数) ,你可以同时更改缓存和文件/ 数据库。或者你等 变化积累到一定程序再保存,也可以在下一步保存。

     

           3. 服务器将要关闭时,ServletContextListener 的 contextDestroyed() 方法被调用,所以在里面保存缓存的更改。将更改后的缓存保存回文件或者数据库,更新原来的内容。

     

     

    Java 代码  

     

    1. import User; //my own   
    2. classimport DatabaseManager; // my own class  
    3. import javax.servlet.ServletContext;  
    4. import javax.servlet.ServletContextListener;  
    5. public class MyContextListener  implements ServletContextListener {      
    6.       private ServletContext context = null;      
    7.       public void contextInitialized(ServletContextEvent event) {     
    8.                 context = event.getServletContext();          
    9.                 User user = DatabaseManager.getUserById(1);       
    10.                 context.setAttribute("user1", user);      
    11.       }   
    12.       public void contextDestroyed(ServletContextEvent event) {   
    13.                  User user = (User)context.getAttribute("user1");         
    14.                  DatabaseManager.updateUserData(user);        
    15.                  this.context = null;     
    16.       }  
    17. }  

     

    import User; //my own

     

    classimport DatabaseManager; // my own class

     

    import javax.servlet.ServletContext;

     

    import javax.servlet.ServletContextListener;

     

    public class MyContextListener  implements ServletContextListener {   

     

          private ServletContext context = null;

     

          public void contextInitialized(ServletContextEvent event) {

     

                    context = event.getServletContext(); 

     

                    User user = DatabaseManager.getUserById(1); 

     

                    context.setAttribute("user1", user);

     

          }

     

          public void contextDestroyed(ServletContextEvent event) {

     

                     User user = (User)context.getAttribute("user1"); 

     

                     DatabaseManager.updateUserData(user); 

     

                     this.context = null;

     

          }

     

    }

     

     

    布署 ServletContextListener

     

          你实现(implements) 了 ServletContextListener 编译后,把它放在正确的WEB-INF/classes 目录下,更改WEB-INF 目录下的 web.xml 文件,在web-app 节点里添加

     

    <listener>

     

            <listener-class>MyServletContextListener</listener-class>

     

    </listener>

    aderListener

  • 相关阅读:
    KVM虚拟机高级设置——09 设置KVM虚拟机开机自动启动
    KVM虚拟机高级设置——08 管理远程虚拟机
    搭建KVM环境——07 带GUI的Linux上安装KVM图形界面管理工具
    搭建KVM环境——06 创建虚拟机
    TightVNC安装
    数据库设计
    Free lunch is over
    【问题】使用XShell连接Debian,没有语法高亮
    【Intel 汇编】ELF文件
    【问题】No manual entry for pthread_create in section 3
  • 原文地址:https://www.cnblogs.com/handsome1013/p/5209109.html
Copyright © 2020-2023  润新知