Spring boot Sample 006之spring-boot-custom-servlet
一、环境
1.1、Idea 2020.1
1.2、JDK 1.8
二、步骤
2.1、点击File -> New Project -> Spring Initializer,点击next
2.3、选择Web依赖,选中Spring Web。可以选择Spring Boot版本,本次默认为2.2.6,点击Next
2.4、项目结构
三、自定义实现
3.1、通过bean实现
package org.ouyushan.springboot.custom.servlet.config.listener; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; /**
* @Description: 自定义监听器
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/4/28 10:45
*/
public class CustomListener implements ServletContextListener { @Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("========contextInitialized========");
} @Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("========contextDestroyed========");
}
}
package org.ouyushan.springboot.custom.servlet.config.filter; import javax.servlet.*;
import java.io.IOException; /**
* @Description: 自定义fileter
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/4/28 10:38
*/
public class CustomFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("========init filter========");
} @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("========do filter========");
filterChain.doFilter(servletRequest, servletResponse);
} @Override
public void destroy() {
System.out.println("========destroy filter========");
}
}
package org.ouyushan.springboot.custom.servlet.config.servlet; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; /**
* @Description: 自定义servlet
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/4/28 10:25
*/
public class CustomServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("========servlet get method is called========");
resp.getWriter().write("hello world by get");
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("========servlet post method is called========");
resp.getWriter().write("hello world by post");
}
}
package org.ouyushan.springboot.custom.servlet.controller; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @Description:
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/4/28 10:55
*/
@RestController
@RequestMapping("/api")
public class ServletController { @RequestMapping("/servlet")
public String servlet() {
return "custom servlet";
} @RequestMapping("/filter")
public String filter() {
return "custom filter";
}
}
package org.ouyushan.springboot.custom.servlet; import org.ouyushan.springboot.custom.servlet.config.filter.CustomFilter;
import org.ouyushan.springboot.custom.servlet.config.listener.CustomListener;
import org.ouyushan.springboot.custom.servlet.config.servlet.CustomServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean; @SpringBootApplication
public class SpringBootCustomServletApplication { @Bean
public ServletRegistrationBean servletRegistrationBean() {
// 只有路径是以/custom/servlet开始的才会触发
return new ServletRegistrationBean(new CustomServlet(), "/custom/servlet");
} @Bean
public FilterRegistrationBean filterRegistrationBean() {
//第二个参数为需要拦截的路径,不传则拦截所有
return new FilterRegistrationBean(new CustomFilter(), servletRegistrationBean());
} @Bean
public ServletListenerRegistrationBean<CustomListener> servletListenerRegistrationBean() {
return new ServletListenerRegistrationBean<CustomListener>(new CustomListener());
} public static void main(String[] args) {
SpringApplication.run(SpringBootCustomServletApplication.class, args);
} }
========contextInitialized========
========init filter========
custom servlet
========do filter========
========servlet get method is called========
========destroy filter========
========contextDestroyed========
3.2、通过实现ServletContextInitializer实现
// 方式二 通过实现ServletContextInitializer
@SpringBootApplication
public class SpringBootCustomServletApplication implements ServletContextInitializer { @Override
public void onStartup(ServletContext servletContext) {
// 创建Servlet,并映射访问路径为/custom/servlet
servletContext.addServlet("customServlet", new CustomServlet()).addMapping("/custom/servlet"); // 创建Filter,拦截的Servlet
servletContext.addFilter("customFilter", new CustomFilter())
.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "customServlet"); // 设置自定义filter
servletContext.addListener(new CustomListener());
} public static void main(String[] args) {
SpringApplication.run(SpringBootCustomServletApplication.class, args);
}
}
========contextInitialized========
========init filter========
custom servlet
========do filter========
========servlet get method is called========
========destroy filter========
========contextDestroyed========
3.3、通过@ServletComponentScan结合注解实现
@ServletComponentScan
@SpringBootApplication
public class SpringBootCustomServletApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootCustomServletApplication.class, args);
}
}
package org.ouyushan.springboot.custom.servlet.config.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; /**
* @Description: 自定义servlet
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/4/28 10:25
*/ @WebServlet(name = "customServlet", urlPatterns = "/custom/servlet")
public class CustomServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
System.out.println("========servlet get method is called========");
resp.getWriter().write("hello world by get");
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
System.out.println("========servlet post method is called========");
resp.getWriter().write("hello world by post");
}
} package org.ouyushan.springboot.custom.servlet.config.filter; import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException; /**
* @Description: 自定义fileter
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/4/28 10:38
*/ @WebFilter(filterName = "customFilter", urlPatterns = "/*")
public class CustomFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) {
System.out.println("========init filter========");
} @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("========do filter========");
filterChain.doFilter(servletRequest, servletResponse);
} @Override
public void destroy() {
System.out.println("========destroy filter========");
}
} package org.ouyushan.springboot.custom.servlet.config.listener; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener; /**
* @Description: 自定义监听器
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/4/28 10:45
*/
@WebListener
public class CustomListener implements ServletContextListener { @Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("========contextInitialized========");
} @Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("========contextDestroyed========");
}
}
========contextInitialized========
========init filter========
custom servlet
========do filter========
========servlet get method is called========
========destroy filter========
========contextDestroyed========
Spring boot Sample 006之spring-boot-custom-servlet的更多相关文章
- Spring boot Sample 012之spring-boot-web-upload
一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 spring boot 整合web实现文件上传下载 三.步骤 3.1.点击File -> New Project -& ...
- Spring boot Sample 0010之spring-boot-web-freemarker
一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 spring boot 整合freemarker模板开发web项目 三.步骤 3.1.点击File -> New Pr ...
- Spring boot Sample 009之spring-boot-web-thymeleaf
一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 spring boot 整合thymeleaf模板开发web项目 三.步骤 3.1.点击File -> New Pro ...
- Spring boot Sample 005之spring-boot-profile
一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 通过yaml文件配置spring boot 属性文件 三.步骤 3.1.点击File -> New Project - ...
- Spring Boot (五)Spring Data JPA 操作 MySQL 8
一.Spring Data JPA 介绍 JPA(Java Persistence API)Java持久化API,是 Java 持久化的标准规范,Hibernate是持久化规范的技术实现,而Sprin ...
- 一起学JAVA之《spring boot》03 - 开始spring boot基本配置及项目结构(转)
<div class="markdown_views"> <h3 id="一导航"><a name="t0"& ...
- Spring 5.x 、Spring Boot 2.x 、Spring Cloud 与常用技术栈整合
项目 GitHub 地址:https://github.com/heibaiying/spring-samples-for-all 版本说明: Spring: 5.1.3.RELEASE Spring ...
- spring cloud教程之使用spring boot创建一个应用
<7天学会spring cloud>第一天,熟悉spring boot,并使用spring boot创建一个应用. Spring Boot是Spring团队推出的新框架,它所使用的核心技术 ...
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...
随机推荐
- K. Road Widening
\(考虑每个区域可行的区间\) \(x[1]=s[1]\ \ y[1]=s[1]+g[1]\) \(x[i]=max(x[i-1]-1,s[i]),y[i]=min(y[i-1]+1,s[i]+g[i ...
- T - zxa and leaf HDU - 5682 二分+dfs
T - zxa and leaf HDU - 5682 题目大意是:给你一颗树,这棵树有些节点已经设置了它的美丽值,然后剩下一些节点需要我们设置美丽值. 一条边的丑陋程度等于被定义为由这个边缘连接的两 ...
- SQL SERVER 函数举例
需求说明 将字符串按照指定的分隔符进行分割,并将结果按照从后往前的顺序倒序排列,拼接后的结果用‘/’符连接.(也可以按照指定符号分割为多个列,修改最后一部分即可) 创建测试表及数据 /* 创建一张测试 ...
- 将csv文件导入sql数据库
有一个csv文件需要导入到Sql数据库中,其格式为 “adb”,"dds","sdf" “adb”,"dds","sdf" ...
- linux centos7搭建redis-5.0.5
1. 下载redis 1.1 下载地址 http://download.redis.io/releases/ 1.2 安装版本 redis-5.0.5.tar.gz 2. 安装redis 2.1 前置 ...
- JUC之CountDownLatch源码分析
CountDownLatch是AbstractQueuedSynchronizer中共享锁模式的一个的实现,是一个同步工具类,用来协调多个线程之间的同步.CountDownLatch能够使一个或多个线 ...
- ASP.NET Core on K8S学习之旅(13)Ocelot API网关接入
本篇已加入<.NET Core on K8S学习实践系列文章索引>,可以点击查看更多容器化技术相关系列文章. 上一篇介绍了Ingress的基本概念和Nginx Ingress的基本配置和使 ...
- 【Android】是时候为你的应用加上WebDav同步了
WebDav是什么? WebDAV (Web-based Distributed Authoring and Versioning) 一种基于 HTTP 1.1协议的通信协议.它扩展了HTTP 1.1 ...
- js倒计时 手机休眠时 时间不进行减少
http://www.111cn.net/wy/js-ajax/94218.htm 手机版网页js倒计时存在的问题与解决的方法 www.111cn.net 更新:2015-09-16 编辑:kp123 ...
- dTree
1.dtree.js源码 /*--------------------------------------------------| | dTree 2.05 | www.destroydrop.co ...