SpringBoot整合filter(过滤器)与整合Listener(监听器)
一.SpringBoot整合filter过滤器的两种方法
第一种:
1.以前我们编写一个过滤器需要,编写配置文件和一个过滤器类,而现在在springBoot里不需编写配置类。只需在配置类中添加一个注解即可。
package com.dsd.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter(filterName = "fastFilter",urlPatterns = "/fast") //通过该注解取代了以前的编写配置类
public class fastFilter implements Filter{ //使用Filter接口
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("进入doFilter过滤器》》》》》》》》》》》》》》》》");
chain.doFilter(request, response);
System.out.println("通过dofilter过滤器。。。。。。。。。。。");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}
2.编写一个启动类
package com.dsd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan //通过该注解可以自动扫描
public class HellowServlet011Application {
public static void main(String[] args) {
SpringApplication.run(HellowServlet011Application.class, args);
}
}
二.整合filter的第二种方法
1.编写一个filter类但是不需要添加任何注解
package com.dsd.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class towFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("进入doFilter过滤器》》》》》》》》》》》》》》》》");
chain.doFilter(request, response);
System.out.println("通过dofilter过滤器。。。。。。。。。。。");
}
}
2.编写一个启动器。
package com.dsd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import com.dsd.filter.towFilter;
import com.dsd.servlet.towServlet;
@SpringBootApplication
public class App {
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
@Bean
public ServletRegistrationBean towsservlet() { //整合servlet的方法
ServletRegistrationBean bean = new ServletRegistrationBean(new towServlet());
bean.addUrlMappings("/tows");
return bean;
}
@Bean
public FilterRegistrationBean towffilter() { //整合Filter(过滤器)的方法
FilterRegistrationBean bean = new FilterRegistrationBean(new towFilter());
bean.addUrlPatterns("/tows");
return bean;
}
@Bean
public ServletListenerRegistrationBean towListener() { //整合LIstener(监听器)的方法
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new com.dsd.Listener.towListener());
return bean;
}
}
二.SpringBoot整合Listener(监听器)的两种方法
监听器在服务启动时启动,在服务关闭后结束监听
第一种
1,编写一个带注解的Listener(监听类)
package com.dsd.Listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener // 监听器不需要给它名称
public class fastListener implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent sce) { //服务关闭够执行
System.out.println("监听器关闭中!");
}
@Override
public void contextInitialized(ServletContextEvent sce) { //服务启动时监听执行
System.out.println("监听器正在监听!");
}
}
2.编写一个启动类
package com.dsd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class HellowServlet011Application {
public static void main(String[] args) {
SpringApplication.run(HellowServlet011Application.class, args);
}
}
第二种方法
1.编写一个没有注解的Listener(监听器类)
package com.dsd.Listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class towListener implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("监听退出");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("服务启动,正在监听");
}
}
2.编写启动器
package com.dsd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import com.dsd.filter.towFilter;
import com.dsd.servlet.towServlet;
@SpringBootApplication
public class App {
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
@Bean
public ServletListenerRegistrationBean towListener() { //这是Listener(监听器)的bean方法实现类
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new com.dsd.Listener.towListener());
return bean;
}
}