【spring boot2】第8篇:spring boot 中的 servlet 容器及如何使用war包部署
嵌入式 servlet 容器
在 spring boot 之前的web开发,我们都是把我们的应用部署到 Tomcat 等servelt容器,这些容器一般都会在我们的应用服务器上安装好环境,但是 spring boot 中并不需要外部应用服务器安装这些servlet容器,spring boot自带了嵌入式的servlet容器。
如何修改和定制嵌入式servlet容器
- 在application.yaml文件中配置修改
#修改服务端口号
server.port=8081
#配置统一请求路径
server.context‐path=/crud#配置tomcat相关
server.tomcat.uri‐encoding=UTF‐8
这些配置相关的属性都定义在org.springframework.boot.autoconfigure.web.ServerProperties
类中
- 编写一个嵌入式的servlet容器的定制器来修改相关的配置
@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {@Overridepublic void customize(ConfigurableServletWebServerFactory server) {server.setPort(9000);}
}
- 直接定制具体的servlet容器配置,比如 tomcat 容器
@Configuration
public class ApplicationConfig implements WebMvcConfigurer {@Beanpublic ConfigurableServletWebServerFactory configurableServletWebServerFactory() {TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();factory.setPort(8585);return factory;}
}
注册 Servlet、Filter、Listener
spring boot默认是以 jar 包的方式启动嵌入式的 servlet 容器来启动web应用,应用中并没有 web.xml 文件,我们注册 servlet, filter, 和 listener
三大组件可以使用以下方式
利用
ServletRegistrationBean
注册servlet
- 定义自己的
servlet
public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.getWriter().print("servlet doGet");} }
- 给容器中注册自己的
servlet
@Configuration public class ApplicationConfig implements WebMvcConfigurer {@Beanpublic ServletRegistrationBean myServlet() {ServletRegistrationBean servletRegistrationBean =new ServletRegistrationBean(new MyServlet(), "/hello/servlet");return servletRegistrationBean;} }
- 定义自己的
利用
FilterRegistrationBean
注册filter
- 定义
filter
`
public class MyFilter implements Filter {@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {System.out.println("do myFilter");chain.doFilter(request, response);} }
- 容器中注册
filter
@Configuration public class ApplicationConfig implements WebMvcConfigurer {@Beanpublic FilterRegistrationBean myFilter() {FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();//注册自己的 filterfilterRegistrationBean.setFilter(new MyFilter());//注册拦截路径filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello", "/hello/servlet"));return filterRegistrationBean;} }
访问
"/hello", "/hello/servlet"
请求时拦截器就会起作用。- 定义
利用
ServletListenerRegistrationBean
注册listener
- 定义一个
listener
public class MyListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("contextInitialized ... web应用启动");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {System.out.println("contextDestroyed ... web应用销毁");} }
- 容器中注册
listener
@Configuration public class ApplicationConfig implements WebMvcConfigurer {@Beanpublic ServletListenerRegistrationBean myListener() {ServletListenerRegistrationBean<MyListener> listenerRegistrationBean =new ServletListenerRegistrationBean(new MyListener());return listenerRegistrationBean;} }
- 定义一个
切换其他 servlet 容器
spring boot 中默认使用的是 tomcat 容器,那么如何切换使用其他容器呢,比如 jetty。那么如何切换成其他的呢?只需要修改 pom.xml 文件
<!‐‐ 引入web模块 ‐‐>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring‐boot‐starter‐web</artifactId><exclusions><exclusion><artifactId>spring‐boot‐starter‐tomcat</artifactId><groupId>org.springframework.boot</groupId></exclusion></exclusions></dependency><!‐‐引入其他的servlet容器‐‐>
<dependency><artifactId>spring‐boot‐starter‐jetty</artifactId><groupId>org.springframework.boot</groupId></dependency>
使用外部 servlet 容器
部署项目的服务器安装 servlet 容器,比如 tomcat、jetty 容器,然后项目打成 war 包的形式进行部署
如何创建一个 war 包启动的 spring boot 项目呢?
- 创建一个war打包形式的web项目,其目录结构如下
修改 pom.xml 文件
修改打包方式
pom.xml 文件中的打包方式设置成 war 即可, ```<packaging>war</packaging>```
修改嵌入的 servlet 容器
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope> </dependency>
- 编写一个类继承
SpringBootServletInitializer
类
public class ServletInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(SpringBootWarApplication.class);}
}
使用外部 servlet 容器原理
jar包和war的启动过程
- jar包: 执行spring boot 主类的main方法,启动ioc容器,然后创建嵌入式的servlet容器
- war包: 启动tomcat服务器,服务器启动spring boot应用,通过
SpringBootServletInitializer
实现,启动ioc容器
spring boot使用外部servlet过程
servlet3.0标准中规定
- web应用启动会创建当前web应用里面每一个jar包里面
ServletContainerInitializer
类型的实例 ServletContainerInitializer
的实现放在jar包的META-INF/services文件夹下,有一个名为javax.servlet.ServletContainerInitializer
的文件,文件内容就是ServletContainerInitializer
实现类的全类名- 使用@HandlesTypes注解在应用启动的时候加载我们需要的类
spring boot使用war启动原理
- 重写
SpringBootServletInitializer
的configure(SpringApplicationBuilder application)
方法,调用SpringApplicationBuilder
的sources(Class<?>... sources)
方法 启动原理
- Servlet3.0标准ServletContainerInitializer扫描所有jar包中
META- INF/services/javax.servlet.ServletContainerInitializer
文件指定的类并加载 - 加载spring web包下的
org.springframework.web.SpringServletContainerInitializer
- 扫描
@HandleType(WebApplicationInitializer)
`,即扫描WebApplicationInitializer
的所有实现类 - 加载
SpringBootServletInitializer
并运行onStartup
方法 - 加载
@SpringBootApplication
主类,启动容器
- Servlet3.0标准ServletContainerInitializer扫描所有jar包中
【spring boot2】第8篇:spring boot 中的 servlet 容器及如何使用war包部署相关推荐
- Spring boot配置嵌入式Servlet容器
Servlet容器 项目,打成war包,放在Tomcat启动 Tomcat就是一个Servlet容器 Spring boot 默认的嵌入了Servlet容器Tomcat 打开项目pom文件 右键Dia ...
- Spring Boot 打成war包部署到tomcat8.5.20报无法访问
这里记录一下自己的心得 时间:2017.11.05-2017.11.06 问题:Spring Boot 项目经过mvn clean install之后的war包部署到tomcat 8.5.20里,访问 ...
- war包部署vue_又一干货实战,spring boot2:以 War 包的形式部署
1. 前言 Spring Boot 提供了内置的 tomcat.undertow.jetty 三种 Servlet Web 容器.让我们开箱即用,可以迅速以 JAR 启动一个 Web 应用.但是在某些 ...
- Linux中把文件夹打成war包,SpringBoot中maven项目打成war包部署在liunx服务器上的方法...
说明:Spring Boot由于内嵌了如Tomcat,Jetty和Undertow这样的容器,也就是说可以直接跑起来,用不着再像Spring项目还需要外置的Tomcat等容器来进行部署工作了,通过启动 ...
- SpringBoot中文件下载、拦截器、war包部署、jar包部署
3. SpringBoot中文件下载 将可以被下载资源放在磁盘的 D:\springbootcodes\springboot_day6\download 路径 这里我们使用jsp开发 引入依赖使tom ...
- spring boot 项目打成war包部署到服务器
这是spring boot学习的第二篇了,在上一篇已经整合了spring boot项目了,如果还有小伙伴没有看得可以先去看第一篇 基础整合spring boot项目 到这里的小伙伴应该都是会整合基本的 ...
- Spring Boot配置嵌入式Servlet容器的两种方法
一.前言 SpringBoot默认使用Tomcat作为嵌入式的Servlet容器 二.如何定制和修改Servlet容器的相关配置: 1.修改和server有关的配置(ServerProperties[ ...
- IDEA中如何将一个JavaWeb项目打包成war包
一.war包目录结构介绍. 1.Java的打包方式jar.war.ear包的作用.区别: jar:通常是开发时要引用通用(JAVA)类,打成包便于存放管理: war:是做好一个(web)应用后,通常是 ...
- spring cloud学习进阶篇:Spring Cloud Sleuth + Zipkin 实现分布式跟踪解决方案
2019独角兽企业重金招聘Python工程师标准>>> 简述 使用 spring cloud 用到最多的是各种rest服务调用,Twitter的Zipkin 是一种实现分布式跟踪解决 ...
最新文章
- fegin通信中速度慢等待解决异常
- 统计文件中每个单词出现的次数
- eclipse创建spring boot项目加载不到application.properties配置文件
- Android蓝牙无法通信,android.bluetooth.BluetoothSocket无法连接
- java string转decimal_java中string转bigdecimal的例子
- PDF 与 Word互转工具。 在线的 和安装软件
- 上海交通大学出版社python教材答案学生信息管理系统_学生信息管理系统任务书...
- github注册以及安装教程
- c++中 append()函数用法
- 【Digger爬虫系列】用Digger抓取taptap游戏排行榜
- 决策树CART、ID3、C4.5原理梳理
- 批量下载coursera课程
- matlab图像导数求积分_matlab微积分问题:导数、偏导数
- 软件项目管理学习(三)
- 迪文DWIN串口屏的使用经验分享
- Ubuntu系统连接Android真机测试
- 讲课大师 如何更新讲课接口
- 刀具更换策略问题(完工)
- mi5s plus android 8.0,小米5s Plus获安卓8.0系统更新:续航能力、速度和安全极大提升...
- 首款屏下摄像手机的幕后赢家
热门文章
- Nginx源码分析--基本数据类型的别名
- 深度神经网络中的Inception模块介绍
- 【Qt】通过QtCreator源码学习Qt(十):多国语言支持
- 【转载】linux静态链接库与动态链接库的区别及动态库的创建
- html在页面上div绝对定位,html – 中心浮动div在绝对定位div内
- html5 audio标志改变音量,HTML5之Audio(二)—— processor调节音量
- python面向对象的优点_Python面向对象编程——总结面向对象的优点
- matlab整型和浮点的区别,技术帖 | 心理学MATLAB初学者教程--简单数据类型介绍(逻辑型数据,整型/浮点型数据,字符型)......
- legend位置 pyecharts_可视化入门 | pyecharts全局配置项详解
- simple_html_dom meta,HTML DOM Meta content 属性