SpringBoot起飞系列-配置嵌入式Servlet容器(八)
一、前言
springboot中默认使用的是tomcat容器,也叫做嵌入式的servlet容器。因为它和我们平常使用的tomcat容器不一样,这个tomcat直接嵌入到的springboot,平常我们使用tomcat容器是一个独立的应用,配置的时候需要在tomcat中的xml中配置,而使用springboot就不一样了,本文我们来学习怎么配置和替换servlet容器、以及注册传统的servlet组件。
二、自定义Servlet容器的相关配置
2.1 修改配置文件的值
我们可以在application.properties中按照传统的配置方式设置,如下:
server.port=8081
server.context-path=/crud server.tomcat.uri-encoding=UTF-8 //通用的Servlet容器设置
server.xxx
//Tomcat的设置
server.tomcat.xxx
其中server开头的是web容器统一的配置,因为我们以后可以使用其他的web容器,带tomcat的是特定为tomcat容器的配置。
2.2 编码方式配置
编写一个EmbeddedServletContainerCustomizer:嵌入式的Servlet容器的定制器,来修改Servlet容器的配置。我们可以添加一个MyServerConfig类,用注解的方式来配置我们的Server配置。
package com.example.demo.config; import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyServerConfig { @Bean
public WebServerFactoryCustomizer embeddedServletContainerCustomizer(){
return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8085);
}
};
}
}
启动之后我们就把端口设置到了8085。
三、注册Servlet三大组件(Servlet、Filter、Listener)
由于springboot默认是以jar包的方式启动嵌入式的web服务器来启动springboot应用,没有web.xml文件。传统情况下我们配置三大组件都是在web.xml中添加配置,现在就不能这么做了。
3.1 注册Servlet
我们使用ServletRegistrationBean类来往容器中添加Servlet组件,在MyServerConfig中添加一个方法:
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
return registrationBean;
}
添加一个MyServlet类来处理/myServlet的请求:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("hello,my servlet");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
启动后浏览器访问http://localhost:8085/myServlet,被我们的MyServlet接管处理,因为配置的时候我们要处理的urlPatterns是/myServlet。
3.2 注册Filter
一样的原理,我们使用FilterRegistrationBean类来往容器中添加Filter组件:
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return registrationBean;
}
添加一个MyFilter的类来处理映射的url:
@WebFilter(filterName = "MyFilter")
public class MyFilter implements Filter {
public void destroy() {
} public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
resp.getWriter().write("hello,my filter");
chain.doFilter(req, resp);
} public void init(FilterConfig config) throws ServletException { } }
3.3 注册Listener
使用ServletListenerRegistrationBean类来往容器中添加Listener组件:
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
return registrationBean;
}
添加一个MyListenner的类来处理逻辑:
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("contextInitialized...web应用启动");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("contextDestroyed...当前web项目销毁");
}
}
这就是我们注册传统三大组件的方法。
有了以上的基础,我们就可以看一下,其实springboot为我们启动springmvc的时候也是通过这种方式注册了DispatcherServlet,这就是我们springmvc中核心的servlet。
@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public ServletRegistrationBean dispatcherServletRegistration(
DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet, this.serverProperties.getServletMapping());
//默认拦截: / 所有请求;包静态资源,但是不拦截jsp请求; /*会拦截jsp
//可以通过server.servletPath来修改SpringMVC前端控制器默认拦截的请求路径 registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
registration.setLoadOnStartup(
this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}
四、总结
这次我们学些了怎样注册传统的三大组件,虽然用到的机会可能不会那么多了,但是springboot的设计理念还是很好的。
SpringBoot起飞系列-配置嵌入式Servlet容器(八)的更多相关文章
- SpringBoot源码学习系列之嵌入式Servlet容器
目录 1.博客前言简单介绍 2.定制servlet容器 3.变换servlet容器 4.servlet容器启动原理 SpringBoot源码学习系列之嵌入式Servlet容器启动原理 @ 1.博客前言 ...
- 17、配置嵌入式servlet容器(1)
SpringBoot默认使用Tomcat作为嵌入式的Servlet容器 1).如何定制和修改Servlet容器的相关配置 1.修改和server有关的配置 (Se ...
- SpringBoot配置嵌入式Servlet容器
1).如何定制和修改Servlet容器的相关配置: 1.修改和server有关的配置(ServerProperties[也是EmbeddedServletContainerCustomizer]): ...
- 配置嵌入式Servlet容器
SpringBoot默认是用的是Tomcat作为嵌入式的Servlet容器:问题?1).如何定制和修改Servlet容器的相关配置:1.修改和server有关的配置(ServerProperties) ...
- 18、配置嵌入式servlet容器(2)
使用其他Servlet容器 -Jetty(长连接) -Undertow(不支持jsp) 替换为其他嵌入式Servlet容器 默认支持: Tomcat(默认使用) Jetty: <depend ...
- 【串线篇】spring boot配置嵌入式servlet容器
SpringBoot默认使用Tomcat作为嵌入式的Servlet容器 问题? 一.如何定制和修改Servlet容器的相关配置 1.方法1修改和server有关的配置(ServerProperties ...
- 17. Spring Boot 配置嵌入式Servlet容器
一.如何定制和修改Servlet容器的相关配置 1.配置文件(ServerProperties): 优先级最高 server.port=8081 server.context‐path=/crud s ...
- 19、配置嵌入式servlet容器(下)
使用外置的Servlet 嵌入式Servlet容器:应用打成可执行的j ar 优点:简单.便携: 缺点:默认不支持JSP.优化定制比较复杂 使用定制器[ServerProperti ...
- springboot(七) 配置嵌入式Servlet容器
github代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service ...
随机推荐
- Python 爬取喜马拉雅音频
一.分析音频下载相关链接地址 1. 分析专辑音频列表页面 在 PC端用 Chrome 浏览器中打开 喜马拉雅 网站,打开 Chrome开发者工具,随意打开一个音频专辑页面,Chrome开发者工具中 ...
- 物联网是前端工程师的新蓝海吗? | Live笔记
物联网是继 Web .无线之后的又一次重大技术变革,在变革的大潮中,程序员的知识体系和思维方式将面临全面更新. 前端开发的历史 在准备这个live的过程中,我回顾了前端开发短暂的历史,有几次我认为非常 ...
- AtCoder AGC001E BBQ Hard (DP、组合计数)
题目链接: https://atcoder.jp/contests/agc001/tasks/agc001_e 题解: 求\(\sum^n_{i=1}\sum^n_{j=i+1} {A_i+A_j+B ...
- 【java设计模式】-07适配器模式
适配器模式 定义: 将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 类型: 结构型模式 应用实例: 1.JAVA JDK 1.1 提供 ...
- 利用ceph-deploy部署ceph存储集群
一.环境准备 创建两台主机,ip地址和主机名如下 192.168.2.100,主机名ceph-1 192.168.2.101,主机名ceph-2 每个主机 新增加一块数据盘,分区根据自己需要分区即可, ...
- elasticsearch-head后台运行
运行插件 # npm run start > elasticsearch-head@0.0.0 start /usr/local/elasticsearch-head-master > g ...
- python3连接redis数据库
1.python想操作redis,需要安装第三方模块(我是在windows下进行操作的) pip install redis 2.连接数据库 #coding:utf-8 import redis r ...
- 发布jar项目到maven仓库
在要发布的项目pom文件中添加配置: <distributionManagement> <repository> <id>releases</id> & ...
- Centos7 yum安装mysql(完整版)
1.下载mysql 地址:https://dev.mysql.com/downloads/repo/yum/.选择对应版本下载.
- map,实现技巧,id
cf #include<iostream> #include<cstdio> #include<algorithm> #include<vector> ...