Spring Boot将WAR文件部署到Tomcat(十一)

这篇博文主要用于新手学习Spring Boot,同时也记录自己学习的过程…
文章内容主要来源于易百教程

在本文中,将演示如何将Spring Boot WAR文件部署到Tomcat servlet容器中。
对于Spring Boot WAR部署,需要执行三个步骤:

  • 列表内容扩展SpringBootServletInitializer
  • 根据提供标记嵌入式servlet容器。
  • 更新包装为War

测试工具:

  • Spring Boot2.0.3.RELEASE
  • Tomcat 8.5.32
  • Maven 3

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

1. 推展SpringBootServletInitializer
使现有的@SpringBootApplication类扩SpringBootServletInitializer

1.1 - 经典Spring Boot JAR部署(更新此文件以支持WAR部署)
SpringBootWebApplication.java文件的内容如下-

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootApplication{
    public static void main(String[] args) throws Exception{
        SpringApplication.run(SpringBootWebApplication.class,args);
    }
}

1.2 Spring Boot WAR部署
SpringBootWebApplication.java文件的内容如下-

package com.th;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;


@SpringBootApplication
public class SpringBootApplication extends SpringBootServletInitializer{

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
        return application.sources(SpringBootWebApplication.class);
    }
    public static void main(String[] args)throws Exception{
        SpringApplication.run(SpringBootWebApplication.class,args);
    }

}
/*@SpringBootApplication
public class SpringBootWebApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

}*/

如果要为部署创建了一个额外的SpringBootWebApplication类,请确保告诉Spring Boot要从哪个主类开始启动程序:
pom.xml文件中增加以下内容指定-

<properties>
      <start-class>com.th.NewSpringBootWebApplicationForWAR</start-class>
</properties>

2. 提供标记嵌入式servlet容器
pom.xml文件中增加以下内容指定-

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

<!-- marked the embedded servlet container as provided -->
<!-- 打war包时加入此项, 告诉spring-boot tomcat相关jar包用服务器的,不打成jar -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

Scope选项

范围 描述
compile(编译范围) compile是默认的范围;如果没有提供一个范围,那该范围的范围就是编译范围。编译范围依赖在所有的classpath中可用,同时它们也会被打包。
provided(以提供范围) provided依赖只有在当JDK或者一个容器已提供该依赖之后才使用。例如,如果你开发了一个web应用,你可能在编译classpath中需要可用的Servlet APT来编译一个servlet,但是你不会想要在打包好的WAR中包含这个Servlet API;这个Servlet API JAR由你的应用服务器或者servlet容器提供。已提供范围的依赖在编译classpath(不是运行时)可用。它们不是传递性的,也不会被打包。
runtime(运行时范围) runtime依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如,你可能在编译的时候只需要JDBC API JAR,而只有在运行的时候才需要JDBC驱动实现。
test(测试范围) test范围依赖在编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。
system(系统范围) system范围依赖与provided类似,但是你必须显式的提供一个对于本地系统中JAR文件的路径。这么做是为了允许基于本地对象编译,而这些对象是系统类库的一部分。这样的构件应该是一直可用的,Maven也不会在仓库中去寻找它。 如果你将一个依赖范围设置成系统范围,你必须同时提供一个systemPath元素 。注意该范围是不推荐使用的(应该一直尽量去从公共或定制的Maven仓库中引用依赖)。

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文件的完整代码如下所示-

package com.th;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootWebApplication.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

}

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-boot-web-thymeleaf</artifactId>
    <packaging>war</packaging>
    <name>Spring Boot Web Thymeleaf Example</name>
    <description>Spring Boot Web Thymeleaf Example</description>
    <version>1.0</version>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.2.RELEASE</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!-- This is a web application -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Tomcat embedded container -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

        <!-- Need this to compile JSP, tomcat-embed-jasper version is not working, 
            no idea why -->
        <dependency>
            <groupId>org.eclipse.jdt.core.compiler</groupId>
            <artifactId>ecj</artifactId>
            <version>4.6.1</version>
            <scope>provided</scope>
        </dependency>

        <!-- Optional, test for static content, bootstrap CSS -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.7</version>
        </dependency>

        <!-- jasper是将jsp转化为jvm 能识别的class.java 文件 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>
    <build>
       <!-- 打成war之后的项目名称 -->
        <finalName>springboot</finalName>

        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warSourceExcludes>src/main/resources/**</warSourceExcludes>
                    <warName>springboot</warName>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

welcome.message=Hello Nicole Tang

4.5 - 完整的项目目录结构-
这里写图片描述

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

D:\workspace\spring-boot-web-thymeleaf>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Spring Boot Web Thymeleaf Example 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ spring-boot-web-thymeleaf ---
[INFO] Deleting D:\workspace\spring-boot-web-thymeleaf\target
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ spring-boot-web-th
ymeleaf ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ spring-boot-web-thymele
af ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to D:\workspace\spring-boot-web-thymeleaf\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ spring-boo
t-web-thymeleaf ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\workspace\spring-boot-web-thymeleaf\src\test
\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ spring-boot-web
-thymeleaf ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ spring-boot-web-thymeleaf --
-
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:3.1.0:war (default-war) @ spring-boot-web-thymeleaf ---
[INFO] Packaging webapp
[INFO] Assembling webapp [spring-boot-web-thymeleaf] in [D:\workspace\spring-boot-web-thym
eleaf\target\springboot]
[INFO] Processing war project
[INFO] Copying webapp resources [D:\workspace\spring-boot-web-thymeleaf\src\main\webapp]
[INFO] Webapp assembled in [468 msecs]
[INFO] Building war: D:\workspace\spring-boot-web-thymeleaf\target\springboot.war
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.3.RELEASE:repackage (default) @ spring-boot-web-th
ymeleaf ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.080 s
[INFO] Finished at: 2018-08-07T15:24:15+08:00
[INFO] Final Memory: 33M/276M
[INFO] ------------------------------------------------------------------------

D:\workspace\spring-boot-web-thymeleaf>

将生成的springboot.war拷贝到Tomcat发布目录下-
这里写图片描述
例如,写本文章时,Tomcat 发布目录使用的是 –
D:\apache-tomcat-8.5.32\webapps
这里写图片描述
现在点击startup.bat,启动 Tomcat,访问:http://localhost:8080/springboot/ ,程序没有错误问题,应该会看到以下结果 –
这里写图片描述
这里写图片描述




相关文章:

猜你喜欢

转载自blog.csdn.net/ththcc/article/details/81660865