SpringBoot 整合 filter
一:用idea 创建 springboot 项目:
详情请参考:《使用IDEA创建一个springboot项目》
二:具体代码内容;
1:springboot 整合 filter 方式一
FirstFilter
package com.alancode.springboot.filter; /** * @author Alan_liu * @Create 2021/2/15 20:59 */ import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * @program: SpringBootAddFilter * @description: 第一个控制器 使用配置方式 进行springboot整合 filter * Springboot 整合 filter 方式一: * <p> * <p> * *传统spring 方式配置 filter 内容: * * <filter> * * <filter-name>FirstFilter</filter-name> * * <filter-class>com.alancode.springboot.filter.FirstFilter</filter-class> * * </filter> * * <filter-mapping> * * <filter-name>FirstFilter</filter-name> * * <url-pattern>/frist</url-pattern> * * </filter-mapping> * @author: Alan_Liu * @create: 2021-02-15 20:59 */ //@WebFilter(filterName = "FirstFilter",urlPatterns ={"*.do","*.jsp"}/**数组方式设置**/) @WebFilter(filterName = "FirstFilter", urlPatterns = {"/frist"}) public class FirstFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("__________进入 filter____________________________________-"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("__________离开 filter____________________________________-"); } }
FirstServlet
package com.alancode.springboot.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @program: springBootAddServlet * @description: FristServlet * 一:传统的spring 框架增加 servlet 的配置方式 * <servlet> * <servlet-name>FirstServlet</servlet-name> * <servlet-class>com.alancode.springboot.Servlet.FirstServlet</servlet-class> * </servlet> * <servlet-mapping> * <servlet-name>FirstServlet</servlet-name> * <url-pattern>/frist</url-pattern> * </servlet-mapping> * */ /***springBoot 配置 servlet 的配置代码 * @author Alan_Liu***/ @WebServlet(name="FirstServlet",urlPatterns = "/frist") public class FirstServlet extends HttpServlet { /** * * *@Param: [req, resp] *@return *@throws *@author Alan_Liu *@date 2021/2/14 23:22 * **/ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //super.doGet(req,resp); System.out.println("------------springBoot 整合 servlet 方式一:注解配置方式------------------------------------------ -----------"); System.out.println("-----------项目启动时候加载 @WebServlet 注解下的FirstServlet 类 - ---------------------------------"); System.out.println("-----------当进行http://localhost:8080/frist 的浏览器地址访问后,进输出该打印内容 - ----------------------"); } }
FilterApplication
package com.alancode.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.web.servlet.ServletComponentScan; /*** * * Springboot 整合 filter 方式一: *@Param: *@return *@throws *@author Alan_Liu *@date 2021/2/15 21:21 * **/ @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) @ServletComponentScan public class FilterApplication { public static void main(String[] args) { SpringApplication.run(FilterApplication.class, args); } } /*** * * **注意: **关于编写启动器需要注意的问题。 **启动器存放位置:启动器可以和controller 位于同一个包 package下,或者位于controller的上一级包。 **但是不能把该文件放到controller平行级的包或者其子包下。 * ServletComponentScan 含义: * 在springboot 启动时候进行扫描@webServlet注解的所有servlet、filter类。并将该类实例化 * * ***/
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.AlanCode.springBoot</groupId> <artifactId>springBootAddFilter</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springBootAddFilter</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
启动 springboot 项目
访问控制器
查看打印信息
2:springboot 整合 filter 方式二:
1:SecondFilter
package com.alancode.springboot.filter; /** * @author Alan_liu * @Create 2021/2/15 21:51 */ import javax.servlet.*; import java.io.IOException; /** * @program: SpringBootAddFilter * @description: 第二个拦截器 springboot 整合 filter * @author: Alan_Liu * @create: 2021-02-15 21:51 */ 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("__________进入 Second filter____________________________________-"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("__________离开 Second filter____________________________________-"); } @Override public void destroy() { } }
2:SecondServlet
package com.alancode.springboot.servlet; /** * @author Alan_liu * @Create 2021/2/14 23:46 */ import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** *@program: springBootAddServlet *@description: SecondServlet *@author: Alan_Liu *@create: 2021-02-14 23:46 */ public class SecondServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("------------springBoot 整合 servlet 方式二: ------------------------------------------ -----------"); System.out.println("-----------项目启动SpringBootAddServletApplication2时候加载声明初始化SecondServlet 类 - ---------------------------------"); System.out.println("-----------当进行http://localhost:8080/second 的浏览器地址访问后,进输出该打印内容 - ----------------------"); } }
3:FilterApplication2
package com.alancode.springboot; import com.alancode.springboot.filter.SecondFilter; import com.alancode.springboot.servlet.SecondServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; /*** * * Springboot 整合 filter 方式二: *@Param: *@return *@throws *@author Alan_Liu *@date 2021/2/15 21:21 * **/ @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) public class FilterApplication2 { public static void main(String[] args) { SpringApplication.run(FilterApplication2.class, args); } /** * 注册 Servlet * @return */ @Bean public ServletRegistrationBean getServletRegistrationBean() { ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet()); bean.addUrlMappings("/second"); System.out.println("------------springBoot 整合 servlet 方式二: ------------------------------------------ -----------"); System.out.println("-----------项目启动SpringBootAddServletApplication2时候加载声明初始化SecondServlet 类 - ---------------------------------"); System.out.println("-----------当进行http://localhost:8080/second 的浏览器地址访问后,进输出该打印内容 - ----------------------"); return bean; } /** * 注册 filter */ @Bean public FilterRegistrationBean getFilterRegistrationBean(){ FilterRegistrationBean bean =new FilterRegistrationBean(new SecondFilter()); bean.addUrlPatterns("/second"); /*bean.addUrlPatterns(new String[]{"*.do","*.jsp","*.html"});*/ System.out.println("------------springBoot 整合 filter 方式二: ------------------------------------------ -----------"); return bean; } } /*** * * **注意: **关于编写启动器需要注意的问题。 **启动器存放位置:启动器可以和controller 位于同一个包 package下,或者位于controller的上一级包。 **但是不能把该文件放到controller平行级的包或者其子包下。 * ServletComponentScan 含义: * 在springboot 启动时候进行扫描@webServlet注解的所有servlet、filter类。并将该类实例化 * * ***/
为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/