Spring Boot Servlet 过滤 监听

Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、Filter、Listener、Interceptor 等等。

当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSessionListener监听器)。 
Spring boot 的主 Servlet 为 DispatcherServlet,其默认的url-pattern为“/”。也许我们在应用中还需要定义更多的Servlet,该如何使用SpringBoot来完成呢?

在spring boot中添加自己的Servlet有两种方法,代码注册Servlet和注解自动注册(Filter和Listener也是如此)。 
一、代码注册通过ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制。 
也可以通过实现 ServletContextInitializer 接口直接注册。

二、在 SpringBootApplication 上使用@ServletComponentScan 注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。

通过代码注册Servlet示例代码:

SpringBootSampleApplication.Java

 1 package org.springboot.sample;
 2
 3 import org.springboot.sample.servlet.MyServlet;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.boot.context.embedded.ServletRegistrationBean;
 7 import org.springframework.boot.web.servlet.ServletComponentScan;
 8 import org.springframework.context.annotation.Bean;
 9 import org.springframework.web.servlet.DispatcherServlet;
10
11 @SpringBootApplication
12 public class SpringBootSampleApplication {
13
14     /**
15      * 使用代码注册Servlet(不需要@ServletComponentScan注解)
16      *
17      * @return
18      * @author SHANHY
19      * @create  2016年1月6日
20      */
21     @Bean
22     public ServletRegistrationBean servletRegistrationBean() {
23         return new ServletRegistrationBean(new MyServlet(), "/xs/*");// ServletName默认值为首字母小写,即myServlet
24     }
25
26      /**
27      * 使用代码注册Servlet(不需要@ServletComponentScan注解)
28      *
29      * @return
30      * @author SHANHY
31      * @create  2016年1月6日
32      */
33    /* @Bean
34     public FilterRegistrationBean  filterRegistrationBean () {
35         FilterRegistrationBean registrationBean= new FilterRegistrationBean(new GoodsFilter());// ServletName默认值为首字母小写,即goodsFilter
36         registrationBean.addUrlPatterns("/goods/*");
37         return registrationBean;
38     }*/
39
40
41     public static void main(String[] args) {
42         SpringApplication.run(SpringBootSampleApplication.class, args);
43     }
44 }

MyServlet.java

 1 package org.springboot.sample.servlet;
 2
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 /**
13  * Servlet
14  *
15  * @author   单红宇(365384722)
16  * @myblog  http://blog.csdn.net/catoop/
17  * @create    2016年1月6日
18  */
19 //@WebServlet(urlPatterns="/xs/*", description="Servlet的说明")
20 public class MyServlet extends HttpServlet{
21
22     private static final long serialVersionUID = -8685285401859800066L;
23
24     @Override
25     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
26         System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");
27         doPost(req, resp);
28     }
29
30     @Override
31     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
32         System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");
33         resp.setContentType("text/html");
34         PrintWriter out = resp.getWriter();
35         out.println("<html>");
36         out.println("<head>");
37         out.println("<title>Hello World</title>");
38         out.println("</head>");
39         out.println("<body>");
40         out.println("<h1>大家好,我的名字叫Servlet</h1>");
41         out.println("</body>");
42         out.println("</html>");
43     }
44
45 }

使用注解注册Servlet示例代码

SpringBootSampleApplication.java

 1 package org.springboot.sample;
 2
 3 import org.springboot.sample.servlet.MyServlet;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.boot.context.embedded.ServletRegistrationBean;
 7 import org.springframework.boot.web.servlet.ServletComponentScan;
 8 import org.springframework.context.annotation.Bean;
 9 import org.springframework.web.servlet.DispatcherServlet;
10
11 @SpringBootApplication
12 @ServletComponentScan
13 public class SpringBootSampleApplication {
14
15     public static void main(String[] args) {
16         SpringApplication.run(SpringBootSampleApplication.class, args);
17     }
18 }
时间: 2024-08-07 08:39:46

Spring Boot Servlet 过滤 监听的相关文章

Spring Boot实践——事件监听

借鉴:https://blog.csdn.net/Harry_ZH_Wang/article/details/79691994 https://blog.csdn.net/ignorewho/article/details/80702827     https://www.jianshu.com/p/edd4cb960da7 事件监听介绍 Spring提供5种标准的事件监听: 上下文更新事件(ContextRefreshedEvent):该事件会在ApplicationContext被初始化或者

rabbitMq与spring boot搭配实现监听

在我前面有一篇博客说到了rabbitMq实现与zk类似的watch功能,但是那一篇博客没有代码实例,后面自己补了一个demo,便于理解.demo中主要利用spring boot的配置方式, 一.消费者(也就是watcher)配置 配置都采用spring的注解进行配置 1.创建连接 @Bean public ConnectionFactory createConnectionFactory() { CachingConnectionFactory connectionFactory = new C

Spring Boot实现一个监听用户请求的拦截器

项目中需要监听用户具体的请求操作,便通过一个拦截器来监听,并继续相应的日志记录 项目构建与Spring Boot,Spring Boot实现一个拦截器很容易. Spring Boot的核心启动类继承WebMvcConfigurerAdapter // 增加拦截器 @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new RequestLog()); } //这

Spring boot设置启动监听端口

一.直接看代码: 1 @Controller 2 @EnableAutoConfiguration 3 @ComponentScan 4 public class CallMain extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer { 5 private final static Logger logger = LoggerFactory.getLogger(CallMain.cl

【Spring】关于SpringMvc监听的知识点

一,在使用Spring系列框架时,我们需要在Web.xml配置Spring的监听:ContextLoaderListener ContextLoaderListener的作用就是,在Web容器初始化时自动加载所对应的applicationContext.xml等配置文件,以帮助Spring完成其他初始化操作 语法是 这样在默认情况下,当Web容器启动时,系统会自动从web-INF文件夹下寻找一个叫applicationContext.xml的文件,并加载它进行初始化操作! 但是我们同样可以修改a

Servlet之监听事件细究

观察者三个模式,我用草图画一下: ServletContextListener:用于监听WEB 应用启动和销毁的事件,监听器类需要实现javax.servlet.ServletContextListener 接口. ServletContextAttributeListener:用于监听WEB应用属性改变的事件,包括:增加属性.删除属性.修改属性,监听器类需要实现javax.servlet.ServletContextAttributeListener接口. HttpSessionListene

spring boot servlet 注入

spring boot 注入servlet的方法是借助ServletRegistrationBean这个类 例子如下: 先建一个servlet import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.Ht

java spring 文件上传监听 控制条显示

1.        接管CommonsMultipartResolver,重写针对文件上传的请求. package com.sinosoft.amoeba.fileupload; import com.sinosoft.amoeba.fileupload.listener.FileUploadProgressListener;import org.apache.commons.fileupload.*;import org.apache.commons.fileupload.servlet.Se

Servlet常用监听接口

场景 监听接口 事件类型 你想知道一个web应用上下文中是否增加.删除或替换了一个属性 javax.servlet.ServletContextAttributeListener attributeAdded attributeRemoved attributeReplaced ServletContextAttributeEvent 你想知道有多少并发个用户.也就是说,你想跟踪活动的会话 javax.servlet.http.HttpSessionListener sessionCreated