文章目录
- 前言
- 一、介绍
- 二、注入Bean
- 2.1.ServletRegistrationBean
- 2.2.FilterRegistrationBean
- 2.3.ServletListenerRegistrationBean
- 三.演示结果
- 总结
前言
本文章将介绍SpringBoot注册Servlet的三大组件
一、介绍
由于SpringBoot默认是以jar包的方式运行嵌入式Servlet容器来启动应用,没有web.xml文件,
所以Spring提供以下Bean来注册三大组件。
- ServletRegistrationBean 注册自定义Servlet
- FilterRegistrationBean 注册自定义Filter
- ServletListenerRegistrationBean 注册自定义Listener
二、注入Bean
2.1.ServletRegistrationBean
首先我们需要一个类继承至HttpServlet
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get请求进入servlet");
resp.getWriter().println("<h1>get:hello world</h1>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post请求进入servlet");
resp.getWriter().println("<h1>post:hello world</h1>");
}
}
然后在配置类中注入ServletRegistrationBean
@Configuration
public class SpringBootConfig {
@Bean
public ServletRegistrationBean doRegiste(){
ServletRegistrationBean bean = new ServletRegistrationBean();
//将自己创建的继承至HttpServlet的类添加进ServletRegistrationBean
bean.setServlet(new MyServlet());
//设置请求路径
bean.setUrlMappings(Arrays.asList("/myServlet"));
//设置优先级
bean.setLoadOnStartup(1);
return bean;
}
}
2.2.FilterRegistrationBean
首先我们需要一个类实现Filter接口,重写它的doFilter方法
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("============请求过滤");
request.setCharacterEncoding("utf-8");
//分水岭
chain.doFilter(request, response);
response.setCharacterEncoding("utf-8");
System.out.println("============响应过滤");
}
}
在配置类中将FilterRegistrationBean注入
@Configuration
public class SpringBootConfig {
//注册Servlet
//替换:@WebServlet(urlPatterns = "/myServlet")
@Bean
public ServletRegistrationBean doRegiste(){
ServletRegistrationBean bean = new ServletRegistrationBean();
bean.setServlet(new MyServlet());
bean.setUrlMappings(Arrays.asList("/myServlet"));
bean.setLoadOnStartup(1);
return bean;
}
//注册Filter
//替换//@WebFilter(urlPatterns = {"/*"})
@Bean
public FilterRegistrationBean doFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new MyFilter());
bean.addUrlPatterns("/*");
return bean;
}
}
2.3.ServletListenerRegistrationBean
首先需要一个类实现ServletContextListener接口,重现它的contextInitialized和contextDestroyed方法监听程序初始化和销毁
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContextListener.super.contextInitialized(sce);
System.out.println("-------------MyListener inited !");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
ServletContextListener.super.contextDestroyed(sce);
System.out.println("----------------MyListener Destroy !");
}
}
在配置类中注入ServletListenerRegistrationBean
@Configuration
public class SpringBootConfig {
//注册Servlet
//替换:@WebServlet(urlPatterns = "/myServlet")
@Bean
public ServletRegistrationBean doRegiste(){
ServletRegistrationBean bean = new ServletRegistrationBean();
bean.setServlet(new MyServlet());
bean.setUrlMappings(Arrays.asList("/myServlet"));
bean.setLoadOnStartup(1);
return bean;
}
//注册Filter
//替换//@WebFilter(urlPatterns = {"/*"})
@Bean
public FilterRegistrationBean doFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new MyFilter());
bean.addUrlPatterns("/*");
return bean;
}
//注册Listener
//替换//@WebListener
@Bean
public ServletListenerRegistrationBean doListener(){
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean();
bean.setListener(new MyListener());
return bean;
}
}
三.演示结果
1.监听器初始化
>2.使用postman响应
3.请求过滤
4.请求servlet
5.响应过滤
6.销毁(不能点右上角红色方块键,应该点左侧的退出按钮)
总结
除了以上的方法也可以使用传统的@WebFilter…实现注册,但是这样只能注册声明自己创建的类,在将来我们会用到很多框架提供的类,就无法使用传统的注解注册,所以推荐使用以上配置类的方式注册Servlet。