当前位置:   article > 正文

Spring Boot将WAR文件部署到Tomcat_spring boot war 部署tomcat

spring boot war 部署tomcat

在本文中,将演示如何将Spring Boot WAR文件部署到Tomcat servlet容器中。

对于Spring Boot WAR部署,需要执行三个步骤:

  1. 扩展SpringBootServletInitializer
  2. 根据提供标记嵌入式servlet容器。
  3. 更新包装为 War

测试工具:

  • Spring Boot 1.4.2.RELEASE
  • Tomcat 8.5.9
  • Maven 3

注意
在Spring Boot中,具有嵌入服务器解决方案的最终可执行JAR文件可能不适合所有生产环境,特别是部署团队(具有良好的服务器优化和监控技能知识,但缺乏开发经验的团队),他们希望完全控制服务器,并且不能接触代码。

1. 推展 SpringBootServletInitializer

使现有的 @SpringBootApplication 类扩展 SpringBootServletInitializer
1.1 - 经典Spring Boot JAR部署(更新此文件以支持WAR部署)。

SpringBootWebApplication.java 文件的内容如下 -

  1. import org.springframework.boot.autoconfigure.SpringBootApplication;
  2. @SpringBootApplication
  3. public class SpringBootWebApplication {
  4. public static void main(String[] args) throws Exception {
  5. SpringApplication.run(SpringBootWebApplication.class, args);
  6. }
  7. }

 

1.2 Spring Boot WAR 部署

SpringBootWebApplication.java 文件的内容如下 -

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.boot.builder.SpringApplicationBuilder;
  4. import org.springframework.boot.web.support.SpringBootServletInitializer;
  5. @SpringBootApplication
  6. public class SpringBootWebApplication extends SpringBootServletInitializer {
  7. @Override
  8. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  9. return application.sources(SpringBootWebApplication.class);
  10. }
  11. public static void main(String[] args) throws Exception {
  12. SpringApplication.run(SpringBootWebApplication.class, args);
  13. }
  14. }
  15. /*@SpringBootApplication
  16. public class SpringBootWebApplication {
  17. public static void main(String[] args) throws Exception {
  18. SpringApplication.run(SpringBootWebApplication.class, args);
  19. }
  20. }*/

 

如果要为部署创建了一个额外的SpringBootWebApplication类,请确保告诉Spring Boot要从哪个主类开始启动程序:

在 pom.xml 文件中增加以下内容指定 -

  1. <properties>
  2. <start-class>com.yiibai.NewSpringBootWebApplicationForWAR</start-class>
  3. </properties>

 

2.提供标记嵌入式servlet容器

在 pom.xml 文件中增加以下内容指定 -

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  5. </dependency>
  6. <!-- marked the embedded servlet container as provided -->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-tomcat</artifactId>
  10. <scope>provided</scope>
  11. </dependency>
  12. </dependencies>

 

3.更新包装为War
在 pom.xml 文件中增加以下内容指定 -

<packaging>war</packaging>

 

完成,mvn打包并将$project/target/xxx.war复制到Tomcat发布目录进行部署。

4. 完整的Spring Boot WAR + Tomcat部署

4.1 - 以Spring Boot Thymeleaf为例,更新它并手动部署到Tomcat。
4.2 - 更新现有的SpringBootWebApplication并让它扩展SpringBootServletInitializer类。

SpringBootWebApplication.java文件的完整代码如下所示 -

  1. package com.yiibai;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.builder.SpringApplicationBuilder;
  5. import org.springframework.boot.web.support.SpringBootServletInitializer;
  6. @SpringBootApplication
  7. public class SpringBootWebApplication extends SpringBootServletInitializer {
  8. @Override
  9. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  10. return application.sources(SpringBootWebApplication.class);
  11. }
  12. public static void main(String[] args) throws Exception {
  13. SpringApplication.run(SpringBootWebApplication.class, args);
  14. }
  15. }

 

4.3 - 更新打包包,并按提供的标记spring-boot-starter-tomcat
pom.xml文件的完整代码如下所示 -

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <artifactId>spring-boot-web-thymeleaf</artifactId>
  6. <packaging>war</packaging>
  7. <name>Spring Boot Web Thymeleaf Example</name>
  8. <description>Spring Boot Web Thymeleaf Example</description>
  9. <url>http://www.yiibai.com</url>
  10. <version>1.0</version>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.4.2.RELEASE</version>
  15. </parent>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  23. </dependency>
  24. <!-- marked the embedded servlet container as provided -->
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-tomcat</artifactId>
  28. <scope>provided</scope>
  29. </dependency>
  30. <!-- hot swapping, disable cache for template, enable live reload -->
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-devtools</artifactId>
  34. <optional>true</optional>
  35. </dependency>
  36. <!-- Optional, for bootstrap -->
  37. <dependency>
  38. <groupId>org.webjars</groupId>
  39. <artifactId>bootstrap</artifactId>
  40. <version>3.3.7</version>
  41. </dependency>
  42. </dependencies>
  43. <build>
  44. <plugins>
  45. <!-- Package as an executable jar/war -->
  46. <plugin>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-maven-plugin</artifactId>
  49. </plugin>
  50. </plugins>
  51. </build>
  52. </project>

 

4.4 - 这是可选的,将contextPath更新为/yiibai以方便稍后作为演示。准备好WAR部署文件。

application.properties 文件的完整内容如下所示 -

  1. welcome.message: Hello Yiibai
  2. server.contextPath=/yiibai

Bash

4.5 - 获取Tomcat并部署WAR文件,执行以下命令 -

  1. F:\worksp\springboot\spring-boot-war-tomcat>mvn clean package
  2. [INFO] Scanning for projects...
  3. [INFO]
  4. [INFO] ------------------------------------------------------------------------
  5. [INFO] Building Spring Boot Web Thymeleaf Example 1.0
  6. [INFO] ------------------------------------------------------------------------
  7. Downloading: http://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-thymeleaf/1.4.2.RELEASE/spring-boot-starter-thymeleaf-1.4.2.RELEASE.pom
  8. Downloaded: http://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-thymeleaf/1.4.2.RELEASE/spring-boot-starter-thymeleaf-1.4.2.RELEASE.pom (2 KB at 0.7 KB/sec)
  9. Downloading: http://repo.maven.apache.org/maven2/org/thymeleaf/thymeleaf-spring4/2.1.5.RELEASE/thymeleaf-spring4-2.1.5.RELEASE.pom
  10. ....
  11. .... ...

 

将生成的 spring-boot-web-thymeleaf-1.0.war 拷贝到 Tomcat 发布目录下 -

例如,写本文章时,Tomcat 发布目录使用的是 -F:\worksp\springmvc\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps

4.6 - 现在启动 Tomcat,访问:http://localhost:8080/yiibai/ ,程序没有错误问题,应该会看到以下结果 -

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/146898
推荐阅读
相关标签
  

闽ICP备14008679号