Spring Boot(三) 将war文件部署到tomcat 、 Thymeleaf示例

Spring Boot(三) 将war文件部署到tomcat 、 Thymeleaf示例

一 、 将war文件部署到tomcat

  1. 对于Spring Boot WAR部署,需要执行三个步骤:
    1. 扩展SpringBootServletInitializer
    2. 根据提供标记嵌入式servlet容器。
    3. 更新包装为 War
  2. 测试工具
    1. Spring Boot 1.4.2.RELEASE
    2. Tomcat 8.5.9
    3. Maven 3
    4. 注:在Spring Boot中,具有嵌入服务器解决方案的最终可执行JAR文件可能不适合所有生产环境,特别是部署团队(具有良好的服务器优化和监控技能知识,但缺乏开发经验的团队),他们希望完全控制服务器,并且不能接触代码。
  3. 扩展SpringBootServletInitializer

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

      1. SpringBootWebApplication.java 文件内容如下:

        import org.springframework.boot.autoconfigure.SpringBootApplication;
        
        @SpringBootApplication
        public class SpringBootWebApplication {
        
            public static void main(String[] args) throws Exception {
                SpringApplication.run(SpringBootWebApplication.class, args);
            }
        
        }
        
    2. SpringBoot war 部署

      1. SpringBootWebApplication.java 文件的内容如下 -

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

        <properties>
              <start-class>com.yiibai.NewSpringBootWebApplicationForWAR</start-class>
        </properties>
        
  4. 提供标记嵌入式Servlet容器

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

      <dependencies>
      
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-thymeleaf</artifactId>
          </dependency>
      
          <!-- marked the embedded servlet container as provided -->
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-tomcat</artifactId>
              <scope>provided</scope>
          </dependency>
      
      </dependencies>
      
  5. 更新包装为War

    1. 在pom.xml中添加

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

  6. 完整的Spring Boot War + Tomcat 部署

    1. 以Spring Boot Thymeleaf为例,更新它并手动部署到Tomcat。
    2. 更新现有的SpringBootWebApplication并让它扩展SpringBootServletInitializer类
    3. SpringBootWebApplication.java文件的完整代码如下所示 -

      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. 更新打包包,并按提供的标记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>
          <url>http://www.yiibai.com</url>
          <version>1.0</version>
      
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>1.4.2.RELEASE</version>
          </parent>
      
          <properties>
              <java.version>1.8</java.version>
          </properties>
      
          <dependencies>
      
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-thymeleaf</artifactId>
              </dependency>
      
              <!-- marked the embedded servlet container as provided -->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-tomcat</artifactId>
                  <scope>provided</scope>
              </dependency>
      
              <!-- hot swapping, disable cache for template, enable live reload -->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-devtools</artifactId>
                  <optional>true</optional>
              </dependency>
      
              <!-- Optional, for bootstrap -->
              <dependency>
                  <groupId>org.webjars</groupId>
                  <artifactId>bootstrap</artifactId>
                  <version>3.3.7</version>
              </dependency>
      
          </dependencies>
          <build>
              <plugins>
                  <!-- Package as an executable jar/war -->
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
          </build>
      </project>
      
    5. 这是可选的,将contextPath更新为/yiibai以方便稍后作为演示。准备好WAR部署文件。application.properties 文件的完整内容如下所示 -

      welcome.message: Hello Yiibai
      
      server.contextPath=/yiibai
      
    6. 使用maven执行打包 ,后在target目录下得到一个war文件 。 放到Tomcat的webapps目录下 开启tomcat即可
    7. 访问:http://localhost:8080/yiibai/ ,程序没有错误问题,应该会看到以下结果 -

二 、Thymeleaf示例

  1. 这是一个Spring Boot Web应用示例,使用嵌入式Tomcat + Thymeleaf模板引擎,并将其作为可执行JAR文件。
  2. 使用相关技术
    1. Spring Boot 1.4.2.RELEASE
    2. Spring 4.3.4.RELEASE
    3. Thymeleaf 2.1.5.RELEASE
    4. Tomcat Embed 8.5.6
    5. Maven 3
    6. Java 8
  3. 项目目录
  4. 项目依赖

    1. 声明 spring-boot-starter-thymeleaf以获得开发Spring + Thymeleaf Web应用程序所需的任何程序类库。
    2. 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>jar</packaging>
          <name>Spring Boot Web Thymeleaf 示例</name>
          <description>Spring Boot Web Thymeleaf 示例描述</description>
          <url>http://www.yiibai.com</url>
          <version>1.0</version>
      
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>1.4.2.RELEASE</version>
          </parent>
      
          <properties>
              <java.version>1.8</java.version>
          </properties>
      
          <dependencies>
      
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-thymeleaf</artifactId>
              </dependency>
      
              <!-- hot swapping, disable cache for template, enable live reload -->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-devtools</artifactId>
                  <optional>true</optional>
              </dependency>
      
              <!-- Optional, for bootstrap -->
              <dependency>
                  <groupId>org.webjars</groupId>
                  <artifactId>bootstrap</artifactId>
                  <version>3.3.7</version>
              </dependency>
      
          </dependencies>
      
          <build>
              <plugins>
                  <!-- Package as an executable jar/war -->
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
          </build>
      </project>
      
    3. 显示项目依赖关系:
      1. 提示: spring-boot-devtools这个spring-boot-devtools有助于禁用缓存并启用热插拔,以便开发人员总是看到最后的更改。 有利于发展。 阅读这篇 - Spring Boot - 开发工具。尝试修改Thymeleaf模板或属性文件,刷新浏览器以查看更改立即生效。
  5. Spring Boot

    1. 使用@SpringBootApplication进行注释。 运行此类来启动Spring Boot Web应用程序。

      1. SpringBootWebApplication.java

        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        
        @SpringBootApplication
        public class SpringBootWebApplication {
        
            public static void main(String[] args) throws Exception {
                SpringApplication.run(SpringBootWebApplication.class, args);
            }
        
        }
        
      2. 一个简单的Spring控制器类: WelcomeController.java 代码如下 -

        import java.util.Map;
        
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.RequestMapping;
        
        @Controller
        public class WelcomeController {
        
            // inject via application.properties
            @Value("${welcome.message:test}")
            private String message = "Hello World";
        
            @RequestMapping("/")
            public String welcome(Map<String, Object> model) {
                model.put("message", this.message);
                return "welcome";
            }
        
        }
        
  6. Thymeleaf+资源+静态文件

    1. 对于Thymeleaf模板文件,放入 src/main/resources/templates/ 目录下, src/main/resources/templates/welcome.html 文件代码如下 -

      <!DOCTYPE HTML>
      <html xmlns:th="http://www.thymeleaf.org">
      <head>
      <title>Spring Boot Thymeleaf Hello World示例</title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      
      <link rel="stylesheet" type="text/css"
          href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />
      
      <link rel="stylesheet" th:href="@{/css/main.css}"
          href="../../css/main.css" />
      
      </head>
      <body>
      
          <nav class="navbar navbar-inverse">
              <div class="container">
                  <div class="navbar-header">
                      <a class="navbar-brand" href="#">Spring Boot</a>
                  </div>
                  <div id="navbar" class="collapse navbar-collapse">
                      <ul class="nav navbar-nav">
                          <li class="active"><a href="#">首页</a></li>
                          <li><a href="#about">关于</a></li>
                      </ul>
                  </div>
              </div>
          </nav>
      
          <div class="container">
      
              <div class="starter-template">
                  <h1>Spring Boot Web Thymeleaf示例</h1>
                  <h2>
                      <span th:text="'Message: ' + ${message}"></span>
                  </h2>
              </div>
      
          </div>
          <!-- /.container -->
      
          <script type="text/javascript"
              src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      
      </body>
      </html>
      
    2. 对于静态文件,如CSS或Javascript,将它们放入 /src/main/resources/static/ 目录, /src/main/resources/static/css/main.css文件代码如下 -

      h1{
          font-size: 20pt;
      }
      h2{
          font-size: 16pt;
      }
      
    3. 对于属性文件,放入 /src/main/resources/ 目录中, /src/main/resources/application.properties 文件中的代码内容如下 -

      welcome.message: Hello, Spring Boot
      
  7. 运行
    1. 启动Spring Boot Web应用程序,使用Maven命令:mvn spring-boot:run,运行结果输出结果如下 - 可以再浏览器中访问localhost:8080测试
  8. 创建可执行jar
    1. 打包项目以创建可执行的JAR文件。使用Maven命令:mvn clean package,
    2. 执行可执行jar:F:\worksp\springboot\spring-boot-web-thymeleaf> java -jar target/spring-boot-web-thymeleaf-1.0.jar
    3. 在浏览器中访问localhost:8080测试

猜你喜欢

转载自blog.csdn.net/chou_out_man/article/details/80005774