Servlet27 - Listener 监听器

Listener

监听 xxx 对象的 xxx 过程

  • ServletContextListener

    • 监听ServletContext对象的创建与销毁

    • 方法名 作用
      contextInitialized(ServletContextEvent sce) ServletContext创建时调用
      contextDestroyed(ServletContextEvent sce) ServletContext销毁时调用
  • HttpSessionListener

    • 监听HttpSession对象的创建与销毁

    • 方法名 作用
      sessionCreated(HttpSessionEvent hse) HttpSession对象创建时调用
      sessionDestroyed(HttpSessionEvent hse) HttpSession对象销毁时调用
  • ServletRequestListener

    • 监听ServletRequest对象的创建与销毁

    • 方法名 作用
      requestInitialized(ServletRequestEvent sre) ServletRequest对象创建时调用
      requestDestroyed(ServletRequestEvent sre) ServletRequest对象销毁时调用
  • ServletContextAttributeListener

    • 监听ServletContext中属性的创建、修改和销毁

    • 方法名 作用
      attributeAdded(ServletContextAttributeEvent scab) 向ServletContext中添加属性时调用
      attributeRemoved(ServletContextAttributeEvent scab) 从ServletContext中移除属性时调用
      attributeReplaced(ServletContextAttributeEvent scab) 当ServletContext中的属性被修改时调用
  • HttpSessionAttributeListener

    • 监听HttpSession中属性的创建、修改和销毁

    • 方法名 作用
      attributeAdded(HttpSessionBindingEvent se) 向HttpSession中添加属性时调用
      attributeRemoved(HttpSessionBindingEvent se) 从HttpSession中移除属性时调用
      attributeReplaced(HttpSessionBindingEvent se) 当HttpSession中的属性被修改时调用
  • ServletRequestAttributeListener

    • 监听ServletRequest中属性的创建、修改和销毁

    • 方法名 作用
      attributeAdded(ServletRequestAttributeEvent srae) 向ServletRequest中添加属性时调用
      attributeRemoved(ServletRequestAttributeEvent srae) 从ServletRequest中移除属性时调用
      attributeReplaced(ServletRequestAttributeEvent srae) 当ServletRequest中的属性被修改时调用
  • HttpSessionBindingListener

    • 监听某个对象在Session域中的创建与移除

    • 方法名 作用
      valueBound(HttpSessionBindingEvent event) 该类的实例被放到Session域中时调用
      valueUnbound(HttpSessionBindingEvent event) 该类的实例从Session中移除时调用
  • HttpSessionActivationListener

    • 监听某个对象在Session中的序列化与反序列化

    • 方法名 作用
      sessionWillPassivate(HttpSessionEvent se) 该类实例和Session一起钝化到硬盘时调用
      sessionDidActivate(HttpSessionEvent se) 该类实例和Session一起活化到内存时调用

1. 配置 Listener

  • web.xml 文件中配置

    • <!-- 每一个listener标签对应一个监听器配置,若有多个监听器,则配置多个listener标签即可 -->
      <listener>
          <!-- 配置监听器指定全类名即可 -->
          <listener-class>com.atguigu.listener.AtguiguListener</listener-class>
      </listener>
      
  • 注解配置 @WebListener

    • @WebListener
      public class AtguiguListener implements ServletContextListener {
       ...
      }
      

2. 重写 Listener 方法

public class AtguiguListener implements ServletContextListener {
	@Override
    public void contextInitialized(
            // Event对象代表本次事件,通过这个对象可以获取ServletContext对象本身
            ServletContextEvent sce) {
        System.out.println("Hello,我是ServletContext,我出生了!");

        ServletContext servletContext = sce.getServletContext();
        System.out.println("servletContext = " + servletContext);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Hello,我是ServletContext,我打算去休息一会儿!");
    }
}

3. Listener 使用实例

没有监听时,IOC 容器的创建在 DispatcherServlet 初始化方法中,会导致初始化速度下降

  • public class DispatcherServlet extends ViewBaseServlet{
        ...
        public void init() throws ServletException {
            super.init();
            beanFactory = new ClassPathXmlApplicationContext();
        }
        ...
    }
    

通过监听上下文启动,在上下文启动时创建 IOC 容器

  • //监听上下文启动,在上下文启动的时候去创建IOC容器,然后将其保存到application作用域
    //后面中央控制器再从application作用域中去获取IOC容器
    @WebListener
    public class ContextLoaderListener implements ServletContextListener {
        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            //1.获取ServletContext对象
            ServletContext application = servletContextEvent.getServletContext();
            //2.获取上下文的初始化参数
            String path = application.getInitParameter("contextConfigLocation");
            //3.创建IOC容器
            BeanFactory beanFactory = new ClassPathXmlApplicationContext(path);
            //4.将IOC容器保存到application作用域
            application.setAttribute("beanFactory",beanFactory);
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
        }
    }
    
  • DispatcherServlet 初始化方法中,只需要从 application 作用域中获取 IOC 容器,提高了初始化速度

  • public class DispatcherServlet extends ViewBaseServlet{
        ...
    	public void init() throws ServletException {
            super.init();
            //之前是在此处主动创建IOC容器的
            //现在优化为从application作用域去获取
            //beanFactory = new ClassPathXmlApplicationContext();
            ServletContext application = getServletContext();
            Object beanFactoryObj = application.getAttribute("beanFactory");
            if(beanFactoryObj!=null){
                beanFactory = (BeanFactory)beanFactoryObj ;
            }else{
                throw new RuntimeException("IOC容器获取失败!");
            }
        }
        ...
    }
    
posted @   LaViez  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
点击右上角即可分享
微信分享提示