嵌入式 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 项目呢?

  1. 创建一个war打包形式的web项目,其目录结构如下

  1. 修改 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>
  2. 编写一个类继承 SpringBootServletInitializer
public class ServletInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(SpringBootWarApplication.class);}
}

使用外部 servlet 容器原理

jar包和war的启动过程

  1. jar包: 执行spring boot 主类的main方法,启动ioc容器,然后创建嵌入式的servlet容器
  2. 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启动原理

  • 重写SpringBootServletInitializerconfigure(SpringApplicationBuilder application)方法,调用SpringApplicationBuildersources(Class<?>... sources)方法
  • 启动原理

    • Servlet3.0标准ServletContainerInitializer扫描所有jar包中META- INF/services/javax.servlet.ServletContainerInitializer文件指定的类并加载
    • 加载spring web包下的org.springframework.web.SpringServletContainerInitializer
    • 扫描@HandleType(WebApplicationInitializer)`,即扫描WebApplicationInitializer的所有实现类
    • 加载SpringBootServletInitializer并运行onStartup方法
    • 加载@SpringBootApplication主类,启动容器

【spring boot2】第8篇:spring boot 中的 servlet 容器及如何使用war包部署相关推荐

  1. Spring boot配置嵌入式Servlet容器

    Servlet容器 项目,打成war包,放在Tomcat启动 Tomcat就是一个Servlet容器 Spring boot 默认的嵌入了Servlet容器Tomcat 打开项目pom文件 右键Dia ...

  2. Spring Boot 打成war包部署到tomcat8.5.20报无法访问

    这里记录一下自己的心得 时间:2017.11.05-2017.11.06 问题:Spring Boot 项目经过mvn clean install之后的war包部署到tomcat 8.5.20里,访问 ...

  3. war包部署vue_又一干货实战,spring boot2:以 War 包的形式部署

    1. 前言 Spring Boot 提供了内置的 tomcat.undertow.jetty 三种 Servlet Web 容器.让我们开箱即用,可以迅速以 JAR 启动一个 Web 应用.但是在某些 ...

  4. Linux中把文件夹打成war包,SpringBoot中maven项目打成war包部署在liunx服务器上的方法...

    说明:Spring Boot由于内嵌了如Tomcat,Jetty和Undertow这样的容器,也就是说可以直接跑起来,用不着再像Spring项目还需要外置的Tomcat等容器来进行部署工作了,通过启动 ...

  5. SpringBoot中文件下载、拦截器、war包部署、jar包部署

    3. SpringBoot中文件下载 将可以被下载资源放在磁盘的 D:\springbootcodes\springboot_day6\download 路径 这里我们使用jsp开发 引入依赖使tom ...

  6. spring boot 项目打成war包部署到服务器

    这是spring boot学习的第二篇了,在上一篇已经整合了spring boot项目了,如果还有小伙伴没有看得可以先去看第一篇 基础整合spring boot项目 到这里的小伙伴应该都是会整合基本的 ...

  7. Spring Boot配置嵌入式Servlet容器的两种方法

    一.前言 SpringBoot默认使用Tomcat作为嵌入式的Servlet容器 二.如何定制和修改Servlet容器的相关配置: 1.修改和server有关的配置(ServerProperties[ ...

  8. IDEA中如何将一个JavaWeb项目打包成war包

    一.war包目录结构介绍. 1.Java的打包方式jar.war.ear包的作用.区别: jar:通常是开发时要引用通用(JAVA)类,打成包便于存放管理: war:是做好一个(web)应用后,通常是 ...

  9. spring cloud学习进阶篇:Spring Cloud Sleuth + Zipkin 实现分布式跟踪解决方案

    2019独角兽企业重金招聘Python工程师标准>>> 简述 使用 spring cloud 用到最多的是各种rest服务调用,Twitter的Zipkin 是一种实现分布式跟踪解决 ...

最新文章

  1. fegin通信中速度慢等待解决异常
  2. 统计文件中每个单词出现的次数
  3. eclipse创建spring boot项目加载不到application.properties配置文件
  4. Android蓝牙无法通信,android.bluetooth.BluetoothSocket无法连接
  5. java string转decimal_java中string转bigdecimal的例子
  6. PDF 与 Word互转工具。 在线的 和安装软件
  7. 上海交通大学出版社python教材答案学生信息管理系统_学生信息管理系统任务书...
  8. github注册以及安装教程
  9. c++中 append()函数用法
  10. 【Digger爬虫系列】用Digger抓取taptap游戏排行榜
  11. 决策树CART、ID3、C4.5原理梳理
  12. 批量下载coursera课程
  13. matlab图像导数求积分_matlab微积分问题:导数、偏导数
  14. 软件项目管理学习(三)
  15. 迪文DWIN串口屏的使用经验分享
  16. Ubuntu系统连接Android真机测试
  17. 讲课大师 如何更新讲课接口
  18. 刀具更换策略问题(完工)
  19. mi5s plus android 8.0,小米5s Plus获安卓8.0系统更新:续航能力、速度和安全极大提升...
  20. 首款屏下摄像手机的幕后赢家

热门文章

  1. Nginx源码分析--基本数据类型的别名
  2. 深度神经网络中的Inception模块介绍
  3. 【Qt】通过QtCreator源码学习Qt(十):多国语言支持
  4. 【转载】linux静态链接库与动态链接库的区别及动态库的创建
  5. html在页面上div绝对定位,html – 中心浮动div在绝对定位div内
  6. html5 audio标志改变音量,HTML5之Audio(二)—— processor调节音量
  7. python面向对象的优点_Python面向对象编程——总结面向对象的优点
  8. matlab整型和浮点的区别,技术帖 | 心理学MATLAB初学者教程--简单数据类型介绍(逻辑型数据,整型/浮点型数据,字符型)......
  9. legend位置 pyecharts_可视化入门 | pyecharts全局配置项详解
  10. simple_html_dom meta,HTML DOM Meta content 属性