SpringBoot整合filter与Listener

本文详细介绍SpringBoot中整合Filter过滤器和Listener监听器的两种方法。包括使用注解简化配置,以及通过注册Bean的方式进行整合,适用于服务的请求过滤和生命周期事件监听。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
		
	}
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值