首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >3. 整合 Listener

3. 整合 Listener

作者头像
Yuyy
发布2022-06-28 19:23:34
发布2022-06-28 19:23:34
3020
举报

本文最后更新于 915 天前,其中的信息可能已经有所发展或是发生改变。

通过注解扫描完成 Listener 组件的注册

  • 1.1 编写 Listener
代码语言:javascript
复制

/*** springBoot 整合 Listener 
<listener> 
<listener-class>com.bjsxt.listener.FirstListener</listener-class> 
</listener> 
代码语言:javascript
复制
@WebListener 
public class FirstListener implements ServletContextListener { 
    @Override 
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub 
        }
        @Override 
        public void contextInitialized(ServletContextEvent arg0) { 
            System.out.println("Listener...init......"); 
        } 
    }
  • 1.2 编写启动类
代码语言:javascript
复制
/*** springBoot 整合 Listener 方式一 ***/ 
@SpringBootApplication @ServletComponentScan 
public class App { 
    public static void main(String[] args) { 
        SpringApplication.run(App.class, args); 
    } 
}

通过方法完成 Listener 组件注册

  • 2.1 编写 Listener
代码语言:javascript
复制

/*** springBoot 整合 Listener 方式二。 ***/ 
public class SecondListener implements ServletContextListener { 
    @Override 
    public void contextDestroyed(ServletContextEvent arg0) { 
        // TODO Auto-generated method stub 
    }
    @Override 
    public void contextInitialized(ServletContextEvent arg0) { 
        System.out.println("SecondListener..init....."); 
    } 
}
  • 2.2 编写启动类
代码语言:javascript
复制
/*** SpringBoot 整合 Listener 方式二***/ 
@SpringBootApplication 
public class App2 { 
    public static void main(String[] args) { 
        SpringApplication.run(App2.class, args); 
        }
    /*** 注册 listener */ 
    @Bean 
    public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){ 
        ServletListenerRegistrationBean<SecondListener> bean= new ServletListenerRegistrationBean<SecondListener>(new SecondListener()); 
        return bean; 
    } 
}

Post Views: 384

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-12-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档