filter是过滤的意思:在web开发中,是请求会先到过滤器,然后由过滤器再转发到具体的借口上去,此时过滤器就可以对捕捉到的请求作出适当的逻辑了。

一般如果用第三方的filter但是此filter又不是专门服务于spring开发环境,此时就不会被扫描到,因此需要加个@Configuration注解,有了此注解spring会在启动之初就去加载该filter这和以前在spring.xml中配置创建类是一样的效果,urls是指定要过滤那些请求,没有指定的则直接去访问不需要过滤器。

创建一个maven项目,然后此项目继承一个父项目:org.springframework.boot

1.创建一个maven项目:

2.点击next后配置父项目及版本号

3.点击finish后就可查看pom.xml文件中已经有父级项目了。

好了,创建项目演示已经做完,现在粘贴整个各个组件的代码:说明在注释中

1.启动类:

  1. package com.mr.li;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  6. import org.springframework.boot.web.servlet.ServletComponentScan;
  7. import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
  8. /**
  9. * 启动类:
  10. * 1.@springBootApplication:表示此类是spring boot的启动类.
  11. * 2.@ServletComponentScan:此注解的意思是在spring boot启动时,主动去扫描所有的Servlet类,filter类....
  12. * @author Administrator
  13. *
  14. */
  15. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  16. import org.springframework.context.annotation.Bean;
  17.  
  18. import com.mr.li.filter.MyFilter2;
  19. import com.mr.li.listener.MyListener2;
  20. import com.mr.li.servlet.MyServlet;
  21. @SpringBootApplication
  22. @ServletComponentScan
  23. public class Application {
  24.  
  25. public static void main(String[] args) {
  26. //此方法是spring boot启动时必须调用的一个静态方法,第一个参数是启动类的模板,第二个参数就是main方法中的参数。
  27. SpringApplication.run(Application.class, args);
  28. }
  29.  
  30. //方法名叫什么无所谓:在启动类中创建一个新的filter
  31. @Bean
  32. public ServletRegistrationBean getServletRegistrationBean() {
  33. ServletRegistrationBean bean = new ServletRegistrationBean(new MyServlet(), "/hhhh");
  34. return bean;
  35. }
  36.  
  37. //过滤器方式二:在启动类中创建一个新的filter对象,加载到spring容器中,然后但凡以他配置的过滤的后缀名结束的请求都会过滤掉,
  38. @Bean
  39. public FilterRegistrationBean getFilterRegistrationBean() {
  40. FilterRegistrationBean bean = new FilterRegistrationBean(new MyFilter2());
  41. bean.addUrlPatterns("/hhhh","/my");
  42. return bean;
  43. }
  44.  
  45. //注册Listener监听器方式二:启动类中方法注册Listener类。
  46. @Bean
  47. public ServletListenerRegistrationBean<MyListener2> getListenerRegistrationBean(){
  48. ServletListenerRegistrationBean<MyListener2> bean = new ServletListenerRegistrationBean<MyListener2>(new MyListener2());
  49. return bean;
  50. }
  51. }

2.整合servlet

  1. package com.mr.li.servlet;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.ServletException;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. /**
  12. * spring boot整合Servlet:
  13. * 在spring boot中演示:正常的web项目下的Servlet编写方式。现在在spring boot中编写是当前这种方式的,简化了非常多。
  14. * 其中
  15. * 1.@WebServlet注解中my和/my相当于以前web.xml中配置的servlet标签和servlet-mapping标签的所有内容。
  16. 相当于web.xml中的:
  17. <servlet>
  18. <servlet-name>my</servlet-name>
  19. <servlet-class>com.mr.li.servlet.MyServlet</servlet-class>
  20. </servlet>
  21. <servlet-mapping>
  22. <servlet-name>my</servlet-name>
  23. <url-pattern>/my</url-pattern>
  24. </servlet-mapping>
  25. */
  26. @WebServlet(name = "my", urlPatterns = "/my")
  27. public class MyServlet extends HttpServlet{
  28.  
  29. private static final long serialVersionUID = 8908779617494799833L;
  30.  
  31. @Override
  32. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  33. System.out.println("访问到MyServlet");
  34. // resp.setCharacterEncoding("UTF-8");
  35. resp.getWriter().write("你好,世界");
  36. }
  37.  
  38. }

3.整合filter方式一:

  1. package com.mr.li.filter;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.Filter;
  6. import javax.servlet.FilterChain;
  7. import javax.servlet.FilterConfig;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.ServletRequest;
  10. import javax.servlet.ServletResponse;
  11. import javax.servlet.annotation.WebFilter;
  12. /**
  13. * spring boot整合filter方式一:
  14. * 编写Filter,是Servlet的拦截器,指定某个请求到达此请求(一般是servlet)之前就将一些条件过滤掉
  15. * 相当于web.xml中的:
  16. * <filter>
  17. * <filter-name>MyFilter</filter-name>
  18. * <filter-class>com.mr.li.filter.MyFilter</filter-class>
  19. * </filter>
  20. * <filter-mapping>
  21. * <filter-name>MyFilter</filter-name>
  22. * <url-parrern>/my</url-parrern>
  23. * </filter-mapping>
  24. * 本filter的名字叫:MyFilter
  25. * 拦截:/my 结尾的请求
  26. * @author Administrator
  27. *
  28. */
  29. @WebFilter(filterName = "MyFilter", urlPatterns = {"/my"})
  30. public class MyFilter implements Filter {
  31.  
  32. @Override
  33. public void init(FilterConfig filterConfig) throws ServletException {
  34. System.out.println("filter初始化了");
  35. }
  36.  
  37. @Override
  38. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  39. throws IOException, ServletException {
  40. System.out.println("进入:MyFilter");
  41. chain.doFilter(request, response);//放行
  42. System.out.println("离开:MyFilter");
  43. }
  44.  
  45. @Override
  46. public void destroy() {
  47. System.out.println("filter销毁了");
  48. }
  49.  
  50. }

4.整合filter方式2:方法注册,注册方法在启动类中,这里只提供对象

  1. package com.mr.li.filter;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.Filter;
  6. import javax.servlet.FilterChain;
  7. import javax.servlet.FilterConfig;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.ServletRequest;
  10. import javax.servlet.ServletResponse;
  11. /**
  12. * spring boot整合filter方式二:在启动类中调用
  13. */
  14. public class MyFilter2 implements Filter {
  15.  
  16. @Override
  17. public void init(FilterConfig filterConfig) throws ServletException {
  18. System.out.println("filter222初始化了");
  19. }
  20.  
  21. @Override
  22. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  23. throws IOException, ServletException {
  24. System.out.println("进入:MyFilter222");
  25. chain.doFilter(request, response);//放行
  26. System.out.println("离开:MyFilter222");
  27. }
  28.  
  29. @Override
  30. public void destroy() {
  31. System.out.println("filter销毁了");
  32. }
  33.  
  34. }

5.整合Listener方式一:

  1. package com.mr.li.listener;
  2.  
  3. import javax.servlet.ServletContextEvent;
  4. import javax.servlet.ServletContextListener;
  5. import javax.servlet.annotation.WebListener;
  6.  
  7. /**
  8. * spring boot整合Listener监听器方式一:配置监听器,主要看针对哪个去配置监听器,这里配置的监听器主要是针对Servlet配置的监听器。
  9. * 此配置相当于web.xml中:
  10. *<listener-calss>com.mr.li.listener.MyListener</listener-calss>
  11. */
  12. @WebListener
  13. public class MyListener implements ServletContextListener {
  14.  
  15. @Override
  16. public void contextInitialized(ServletContextEvent sce) {
  17. System.out.println("Listener监听器开始工作了。。。init。。。。。");
  18. }
  19.  
  20. @Override
  21. public void contextDestroyed(ServletContextEvent sce) {
  22.  
  23. }
  24.  
  25. }

6.整合Listener方式二:整合方法在启动类中,这里只提供对象

  1. package com.mr.li.listener;
  2.  
  3. import javax.servlet.ServletContextEvent;
  4. import javax.servlet.ServletContextListener;
  5.  
  6. /**
  7. * spring boot整合Listener监听器方式二:通过在启动类中的方法注册Listener监听器。
  8. * 此配置相当于web.xml中:
  9. *<listener-calss>com.mr.li.listener.MyListener</listener-calss>
  10. */
  11. public class MyListener2 implements ServletContextListener {
  12.  
  13. @Override
  14. public void contextInitialized(ServletContextEvent sce) {
  15. System.out.println("Listener2222监听器开始工作了。。。init。。。。。");
  16. }
  17.  
  18. @Override
  19. public void contextDestroyed(ServletContextEvent sce) {
  20.  
  21. }
  22.  
  23. }

7.controller

  1. package com.mr.li.controller;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. /**
  10. * 单独演示Controller
  11. *
  12. */
  13. @Controller
  14. public class HelloWorld {
  15.  
  16. @RequestMapping("/show")
  17. @ResponseBody
  18. public Map<String, Object> show(){
  19. Map<String, Object> map = new HashMap<String, Object>();
  20. map.put("hello", "world");
  21. return map;
  22. }
  23. }

8.pom.xml

  1. <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <!-- spring boot项目需要继承父项目,现为2.1.4版本的父项目 -->
  4. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>1.5.10.RELEASE</version>
  8. </parent>
  9. <groupId>com.mr.li</groupId>
  10. <artifactId>springboot_001</artifactId>
  11. <version>0.0.1-SNAPSHOT</version>
  12.  
  13. <!-- 修改jdk版本 -->
  14. <properties>
  15. <java.version>1.7</java.version>
  16. </properties>
  17. <!-- 添加启动器:spring boot启动器中包含各个项目所需要的jar包,总共有44个启动器,例如web,jdbc,redis...想要使用哪个技术
  18. 就要添加哪个启动器。启动器是各个技术的组件,想用哪个技术就将哪个技术的启动器加入进来,这样此服务器就会拥有哪个技术的组件 -->
  19. <dependencies>
  20. <dependency>
  21. <!-- web启动器:此启动器中包含:tomcat + spring mvc的所有jar包,所以如果做web项目就必须有此启动器 -->
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. </dependencies>
  26. </project>

具体的项目结构如下:

*如果jdk不对就项目右键 -> maven -> updata Project... 一下

*访问的url:http: //localhost:8080/my  这里是访问的servlet , controller一样可以访问,将my换为show即可

spring boot整合servlet、filter、Listener等组件方式的更多相关文章

  1. Spring Boot整合Servlet,Filter,Listener,访问静态资源

    目录 Spring Boot整合Servlet(两种方式) 第一种方式(通过注解扫描方式完成Servlet组件的注册): 第二种方式(通过方法完成Servlet组件的注册) Springboot整合F ...

  2. spring boot 2.x 系列 —— spring boot 整合 servlet 3.0

    文章目录 一.说明 1.1 项目结构说明 1.2 项目依赖 二.采用spring 注册方式整合 servlet 2.1 新建过滤器.监听器和servlet 2.2 注册过滤器.监听器和servlet ...

  3. Spring Boot 整合Servlet

    冷知识,几乎用不到 在spring boot中使用Servlet有两种实现方法: 方法一: 正常创建servlet,然后只用注解@ServletComponentScan package clc.us ...

  4. Spring Boot整合Servlet、Filter、Listener

    整合 Servlet   方式一:   编写 servlet package com.bjsxt.controller; import javax.servlet.ServletException; ...

  5. SpringBoot整合WEB开发--(九)整合Servlet,Filter,Listener

    简介: 如果需要整合第三方框架时,可能还是不得不使用Servlet,Filter,Listener,Springboot中也有提供支持. @WebServlet("/my") pu ...

  6. 【Spring Boot学习之三】Spring Boot整合数据源

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 一.Spring Boot整合Spring JDBC 1.pom.xml <project xmlns=&quo ...

  7. JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下)

    JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下) Reference

  8. Spring Boot 整合 Web 开发

    这一节我们主要学习如何整合 Web 相关技术: Servlet Filter Listener 访问静态资源 文件上传 文件下载 Web三大基本组件分别是:Servlet,Listener,Filte ...

  9. Spring Boot 注册 Servlet 的三种方法,真是太有用了!

    本文栈长教你如何在 Spring Boot 注册 Servlet.Filter.Listener. 你所需具备的基础 什么是 Spring Boot? Spring Boot 核心配置文件详解 Spr ...

随机推荐

  1. IOS 命令行工具开发

    例子  我们需要查看手机APP里面的某个应用的架构 新建一个Single View App 的ios项目 ToolCL 然后在 main函数中加入以下代码 // // main.m // ToolCL ...

  2. bat如何实现多台android设备同时安装多个apk

    背景:在做预置资源(安装apk)时,有多台android设备需要做相同的资源(如:10台,安装10个apk).一台一台去预置的话(当然也可以每人一台去预置),耗时较长有重复性. 问题:如何去实现多台同 ...

  3. jquery 中多选和全选

  4. day12 函数的嵌套调用 闭包函数,函数对象

    函数嵌套: 函数嵌套: 嵌套指的是,一个物体包含另一个物体,函数嵌套就是一个函数包含另一个函数 按照函数的两个阶段 嵌套调用 指的是在函数的执行过程中调用了另一个函数,其好处可以简化外层大函数的代码, ...

  5. vue 的router的简易运用

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Nginx详解十三:Nginx场景实践篇之防盗链

    防盗链: 目的:防止资源被盗用 防盗链设置思路 首要方式:区别哪些请求是非正常的用户请求 基于http_refer防盗链配置模块(判断refer(上一步的链接)信息是否为允许访问的网站) 配置语法:v ...

  7. win+python+selenium实现窗口和tab切换

    这篇总结主要是关于两方面的需求:其一,在浏览器不同tab标签页之间按时间切换(同事用来不停刷新grid crontol 监控页面):其二,实现开启多个窗口,并将窗口缩放到一定范围,并齐占满整个桌面,按 ...

  8. Jmeter 谷歌插件工具blazemeter录制脚本

    1.下载谷歌浏览器插件工具:blazemeter. 2.在谷歌浏览器中拖放安装扩展工具:blazemeter. 粘贴的图像828x219 13.5 KB 3.测试网站利用这个工具录制jmter脚本. ...

  9. 论文阅读笔记三十九:Accurate Single Stage Detector Using Recurrent Rolling Convolution(RRC CVPR2017)

    论文源址:https://arxiv.org/abs/1704.05776 开源代码:https://github.com/xiaohaoChen/rrc_detection 摘要 大多数目标检测及定 ...

  10. svn_linux + apache 实现网页访问svn

    CentOS7:搭建SVN + Apache 服务器实现网页访问 1. 安装httpd 安装httpd服务: $ sudo yum install httpd 检查httpd是否安装成功: $ htt ...