Spring Boot的listener(监听器)简单使用实例详解
2017-04-24 14:21
1156 查看
监听器(Listener)的注册方法和 Servlet 一样,有两种方式:代码注册或者注解注册
1.代码注册方式
通过代码方式注入过滤器
@Bean public ServletListenerRegistrationBean servletListenerRegistrationBean(){ ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(); servletListenerRegistrationBean.setListener(new IndexListener()); return servletListenerRegistrationBean; }
IndexListener.Java类:
package com.example.Listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class IndexListener implements ServletContextListener{ @Override public void contextDestroyed(ServletContextEvent arg0) { System.out.println("IndexListener contextDestroyed method"); } @Override public void contextInitialized(ServletContextEvent arg0) { System.out.println("IndexListener contextInitialized method"); } }
2.注解方式
通过注解方式注入过滤器
IndexListener2.Java类
package com.example.Listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener public class IndexListener2 implements ServletContextListener{ @Override public void contextDestroyed(ServletContextEvent arg0) { System.out.println("IndexListener2 contextDestroyed method"); } @Override public void contextInitialized(ServletContextEvent arg0) { System.out.println("IndexListener2 contextInitialized method"); } }
把注解加到入口处启动即可
@SpringBootApplication @ServletComponentScan public class SpringBootSimpleApplication { public static void main(String[] args) { SpringApplication.run(SpringBootSimpleApplication.class, args); } }
以上所述是小编给大家介绍的Spring Boot的listener(监听器)简单使用实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:
相关文章推荐
- Spring Boot的filter(过滤器)简单使用实例详解
- springboot Mongodb的集成与使用实例详解
- springboot config 拦截器使用方法实例详解
- Spring Boot的listener简单使用
- Spring Boot的listener简单使用
- 使用Spring boot + jQuery上传文件(kotlin)功能实例详解
- SpringBoot使用Thymeleaf3.0自定义标签,简单实例。快速学习!
- JAVAWEB开发之工作流详解(二)——Activiti核心API的使用(流程定义和流程实例的管理、流程变量、监听器...)以及与Spring的集成
- Spring boot 使用mysql实例详解
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- 详解Spring Boot 使用Java代码创建Bean并注册到Spring中
- Spring Boot中使用MyBatis注解配置详解
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- Spring Boot 搭建应用实现登陆实例,页面使用bootstrap
- spring 框架中的依赖注入(IOC--设值注入)---使用xml简单配置文件---的具体实例的简单实现
- SpringBoot 入门教程实例详解(一) 开发第一个SpringBoot应用程序实例
- spring boot介绍及使用详解
- 在listener或者工具中使用spring容器中的bean实例
- java 使用JDBC构建简单的数据访问层实例详解