Spring Boot的listener简单使用
2017-04-24 11:25
567 查看
监听器(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);
}
}
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的listener简单使用
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- springboot 中使用websocket简单例子
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- 使用eclipse工具搭建一个简单的Spring boot。
- springboot+javamail的简单使用
- 使用搬瓦工VPS搭建一个简单的spring-boot应用
- jersey的简单介绍及与spring-boot的集成使用
- spring boot的拦截器简单使用示例代码
- 一、springboot项目的简单使用
- spring boot mybatis 简单整合使用
- Spring Boot中Servlet&Filter&Listener&Interceptor的使用
- 二、springboot项目的简单使用之:定时任务
- 在SpringBoot中使用 Servlet Filter Listener
- Spring Boot 中的Servlet简单使用
- springboot 使用Servlet,Filter,Listener的方法
- 使用RethinkDB与SpringBoot 搭建一个简单的聊天
- 三、springboot项目的简单使用之:JPA使用操作数据库