Spring Boot WAR包运行原理分析

冷不防 2022-07-11 11:15 309阅读 0赞

Spring Boot应用支持用jar方式独立运行(官方推荐)。当然了,也支持打包成war放到web容器中运行

下面,讲简单的演示一下打包成war包运行的步骤

1:新建maven项目

Center

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.pp</groupId>
  5. <artifactId>spring-boot-mall</artifactId>
  6. <version>1.0.0</version>
  7. <!-- 注意这里要是war -->
  8. <packaging>war</packaging>
  9. <name>spring-boot-mall</name>
  10. <url>http://maven.apache.org</url>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.source>1.8</maven.compiler.source>
  14. <maven.compiler.target>1.8</maven.compiler.target>
  15. </properties>
  16. <parent>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-parent</artifactId>
  19. <version>1.4.3.RELEASE</version>
  20. </parent>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-tomcat</artifactId>
  29. <!-- 注意这里是 provided -->
  30. <scope>provided</scope>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <!-- 配置这个插件之后,就支持直接用命令的方式启动(maven clean jetty:run)应用而无需单独准备一个web容器 -->
  36. <plugin>
  37. <groupId>org.eclipse.jetty</groupId>
  38. <artifactId>jetty-maven-plugin</artifactId>
  39. <version>9.3.3.v20150827</version>
  40. <configuration>
  41. <stopKey>foo</stopKey>
  42. <stopPort>9999</stopPort>
  43. <httpConnector>
  44. <port>8080</port>
  45. </httpConnector>
  46. <webApp>
  47. <contextPath>/</contextPath>
  48. </webApp>
  49. </configuration>
  50. </plugin>
  51. <plugin>
  52. <artifactId>maven-war-plugin</artifactId>
  53. <configuration>
  54. <!-- 本例子由于项目里面没有web.xml,所以要配置这个 -->
  55. <failOnMissingWebXml>false</failOnMissingWebXml>
  56. </configuration>
  57. </plugin>
  58. </plugins>
  59. </build>
  60. </project>
  61. package com.pp.mall;
  62. import org.springframework.web.bind.annotation.GetMapping;
  63. import org.springframework.web.bind.annotation.RestController;
  64. @RestController
  65. public class MyController {
  66. @GetMapping("/hello")
  67. public String hello() {
  68. return "Hello World!";
  69. }
  70. }
  71. package com.pp.mall;
  72. import org.springframework.boot.autoconfigure.SpringBootApplication;
  73. import org.springframework.boot.web.support.SpringBootServletInitializer;
  74. @SpringBootApplication
  75. public class SampleWarApplication extends SpringBootServletInitializer {
  76. }

最后,执行maven clean package打包,把打好的war包仍到tomcat等web容器中后,启动容器。

当然了,也可以直接在项目的根目录执行 mvn clean jetty:run 运行项目

之后,就可以访问 /${contextPath}/hello

那么现在问题来了,这个web项目里面没有web.xml,也就是说,没有配置任何初始化、过滤器或者其他Servlet,为什么能启动spring容器呢?

首先要看看Servlet3.0中的一个新接口javax.servlet.ServletContainerInitializer
ServletContainerInitializer 是 Servlet 3.0 新增的一个接口,容器在启动时使用 JAR 服务 API(JAR Service API) 来发现 ServletContainerInitializer 的实现类,并且容器将 WEB-INF/lib 目录下 JAR 包中的类都交给该类的 onStartup() 方法处理,我们通常需要在该实现类上使用 @HandlesTypes 注解来指定希望被处理的类,过滤掉不希望给 onStartup() 处理的类。

也就是说,这里用到了Java的SPI机制。
SPI的全名为Service Provider Interface。这个是针对厂商或者插件的。在java.util.ServiceLoader的文档里有比较详细的介绍。简单的总结下java spi机制的思想。
我们系统里抽象的各个模块,往往有很多不同的实现方案,比如日志模块的方案,xml解析模块、jdbc模块的方案等。
面向的对象的设计里,我们一般推荐模块之间基于接口编程,模块之间不对实现类进行硬编码。一旦代码里涉及具体的实现类,就违反了可拔插的原则,如果需要替换一种实现,就需要修改代码。
为了实现在模块装配的时候能不在程序里动态指明,这就需要一种服务发现机制。 java spi就是提供这样的一个机制:为某个接口寻找服务实现的机制。
有点类似IOC的思想,就是将装配的控制权移到程序之外,在模块化设计中这个机制尤其重要。
java spi的具体约定为:当服务的提供者,提供了服务接口的一种实现之后,在jar包的META-INF/services/目录里同时创建一个以服务接口命名的文件。该文件里就是实现该服务接口的具体实现类。
而当外部程序装配这个模块的时候,就能通过该jar包META-INF/services/里的配置文件找到具体的实现类名,并装载实例化,完成模块的注入。
基于这样一个约定就能很好的找到服务接口的实现类,而不需要再代码里制定。jdk提供服务实现查找的一个工具类:java.util.ServiceLoader

在spring-web-${version}.jar文件里面,可以看到

Center 1

META-INF/services/javax.servlet.ServletContainerInitializer

文件里面的内容是:
org.springframework.web.SpringServletContainerInitializer

意思就是web容器在启动的时候,会去加载所有的javax.servlet.ServletContainerInitializer接口的实现类,然后调用onStartup方法做初始化工作。

所以,在没有web.xml的情况下,也能进行spring容器的启动

发表评论

表情:
评论列表 (有 0 条评论,309人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Boot 运行原理

           Spring Boot的优势在于内置大量习惯性的配置,便于与第三方集成,即“习惯优于配置”,让项目能够快速运行起来,下面我们就探究下Spring Boot自动集成

    相关 Spring Boot运行原理

    Spring Boot方式的项目开发已经逐步成为Java应用开发领域的主流框架,它不仅可以方便地创建生产级的Spring应用程序,还能轻松地通过一些注解配置与目前比较流行的微服