从零开始的Spring Boot(2在Spring Boot中整合ServletFilterListener的方式)

Posted gaolight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从零开始的Spring Boot(2在Spring Boot中整合ServletFilterListener的方式)相关的知识,希望对你有一定的参考价值。

Spring Boot中整合ServletFilterListener的方式

写在前面

从零开始的Spring Boot(1、搭建一个Spring Boot项目Hello World):https://www.cnblogs.com/gaolight/p/13097818.html

一、Spring Boot中整合Servlet的方式

(一)整合Servlet方式一

1.如图创建Servlet包,FirstServlet类;

技术图片

2. 修改FirstServlet内的代码,如下;

package com.demo.springbootweb.Servlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="FirstServlet",urlPatterns = "/first")
public class FirstServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)  {
        System.out.println("FirstServlet...");
    }
}

3.为启动类xxxxApplication.java增加注解@ServletComponentScan

package com.demo.springbootweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan//spring boot启动时扫描注解@WebServlet,并将该类实例化
public class SpringbootwebApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootwebApplication.class, args);
    }
}

4.运行启动类main方法,在浏览器中输入地址:http://localhost:8080/first

可以看到控制台输出

技术图片

 

(二)整合Servlet方式二

1.创建SecondServlet并修改代码如下:

 技术图片

package com.demo.springbootweb.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
public class SecondServlet extends HttpServlet{
    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("SecondServlet...");
    }
}

3. 创建config包和ServletConfig类,并修改代码;

该代码也可以写在启动类下,完成Servlet组件的注册

技术图片

package com.demo.springbootweb.config;
import com.demo.springbootweb.Servlet.SecondServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Servlet;
@Configuration
public class ServletConfig {
    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        ServletRegistrationBean bean=new ServletRegistrationBean((Servlet) new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }
}

4. 运行启动类main方法,在浏览器输入:http://localhost:8080/second

可以看到控制台输出

技术图片

 

 

 

二、Spring Boot中整合Filter的方式

(一)整合Filter方式一

1. filter包下创建FirstFilter类,并修改代码;

技术图片

 

package com.demo.springbootweb.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName="FirstFilter",urlPatterns = "/first")
//@WebFilter(filterName="FirstFilter",urlPatterns = {"*.do","*.jsp"})
public class FirstFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入FirstFilter");
        filterChain.doFilter(servletRequest,servletResponse );
        System.out.println("离开FirstFilter");
    }
    @Override
    public void destroy() {
    }
}

2. 之前在启动类中添加的@ServletComponentScan同样可以扫描注解@WebFilter

package com.demo.springbootweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan//spring boot启动时扫描注解@WebServlet,@WebFilter,并将该类实例化
public class SpringbootwebApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootwebApplication.class, args);
    }
}

 

3. 运行启动类main方法,在浏览器中输入http://localhost:8080/first控制台输出

技术图片

 

(二)整合Filter方式二

1.如图创建SecondFilter类,并修改代码;

技术图片

 

package com.demo.springbootweb.filter;
import javax.servlet.*;
import java.io.IOException;
public class SecondFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入SecondFilter");
        filterChain.doFilter(servletRequest,servletResponse );
        System.out.println("离开SecondFilter");
    }
    @Override
    public void destroy() {
    }
}

2. 为启动类添加注解;

package com.demo.springbootweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan//spring boot启动时扫描注解@WebServlet,@WebFilter,并将该类实例化
public class SpringbootwebApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootwebApplication.class, args);
    }
}

3. 创建FilterConfig类,修改代码;

技术图片

package com.demo.springbootweb.config;
import com.demo.springbootweb.filter.SecondFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean getFilterRegistrationBean(){
        FilterRegistrationBean bean=new FilterRegistrationBean(new SecondFilter());
        //bean.addUrlPatterns(new  String[]{"*.do","*.jsp"});
        bean.addUrlPatterns("/second");
        return bean;
    }
}

4. 运行启动类main方法,在浏览器中输入:http://localhost:8080/second

技术图片

 

 

 

 

三、Spring Boot中整合Listener的方式

(一)整合Listener方式一

1.创建FirstListener类,并修改代码

技术图片

package com.demo.springbootweb.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class FirstListener implements ServletContextListener {
    @Override
    public void contextDestroyed(ServletContextEvent event) {
    }
    @Override
    public void contextInitialized(ServletContextEvent event) {
        System.out.println("FirstListener....init...");
    }
}

2. 为启动类添加注解

package com.demo.springbootweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan//spring boot启动时扫描注解@WebServlet,@WebFilter,@WebListener并将该类实例化
public class SpringbootwebApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootwebApplication.class, args);
    }
}

3. 运行启动类main()方法,控制台输出

技术图片

 

(二)整合Listener方式二

1.创建SecondListener类,修改代码:

技术图片

package com.demo.springbootweb.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SecondListener implements ServletContextListener {
    @Override
    public void contextDestroyed(ServletContextEvent event) {
    }
    @Override
    public void contextInitialized(ServletContextEvent event) {
        System.out.println("SecondListener....init...");
    }
}

2.创建ListenerConfig类,修改代码:

技术图片

package com.demo.springbootweb.config;
import com.demo.springbootweb.listener.SecondListener;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ListenerConfig {
    @Bean
    public ServletListenerRegistrationBean getServletListenerRegistrationBean(){
        ServletListenerRegistrationBean bean=new ServletListenerRegistrationBean(new SecondListener());
        return bean;
    }
}

4. 运行启动类main()方法,控制台输出:

技术图片

 

 

 

 

 

 

 

 

 

以上是关于从零开始的Spring Boot(2在Spring Boot中整合ServletFilterListener的方式)的主要内容,如果未能解决你的问题,请参考以下文章

从零开始学 Spring Boot

从零开始学Spring Boot

从零开始的Spring Boot(4Spring Boot整合JSP和Freemarker)

25. Spring Boot使用自定义的properties从零开始学Spring Boot

22. Spring Boot 拦截器HandlerInterceptor从零开始学Spring Boot

Spring Boot使用模板freemarker从零开始学Spring Boot(转)