zoukankan      html  css  js  c++  java
  • SpringBoot 项目 部署 war方式

    SpringBoot部署 –war方式

    步骤1部署方式

    Springboot 和我们之前学习的web 应用程序不一样,其本质上是一个 Java 应用程序,那么又如何部署呢?
    通常来说,Springboot 部署会采用两种方式:全部打包成一个jar,或者打包成一个war。
    本知识点讲解 war 的方式。

    步骤2可运行项目

    开发过程在前面的知识点讲解过了,这里就不表了,首先在右上角下载可运行项目。
    下载后解压,比如解压到如图所示目录

    步骤3application

    Application 修改为如下代码
    新加@ServletComponentScan注解,并且继承SpringBootServletInitializer 。
    为什么要这么改? 这是规定。。。。 要搞成war ,反正就得这么改~

    package com.coding4funjava.springboot;

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import org.springframework.boot.builder.SpringApplicationBuilder;

    import org.springframework.boot.web.servlet.ServletComponentScan;

    import org.springframework.boot.web.support.SpringBootServletInitializer;

    @SpringBootApplication

    @ServletComponentScan

    public class Application extends SpringBootServletInitializer {

             @Override

             protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

                      return application.sources(Application.class);

             }

             public static void main(String[] args) {

                      SpringApplication.run(Application.class, args);

             }

    }

    步骤4pom.xml

    pom.xml修改为如下代码,主要两个改动
    新加打包成war的声明:

    <packaging>war</packaging>

    spring-boot-starter-tomcat修改为 provided方式,以避免和独立 tomcat 容器的冲突. 
    表示provided 只在编译和测试的时候使用,打包的时候就没它了。

    <?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>

      <groupId>com.coding4funjava</groupId>

      <artifactId>springboot</artifactId>

      <version>0.0.1-SNAPSHOT</version>

      <name>springboot</name>

      <description>springboot</description>

      <packaging>war</packaging>

     

        <parent>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-parent</artifactId>

            <version>1.5.9.RELEASE</version>

        </parent>

        <dependencies>

            <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-web</artifactId>

            </dependency>

            <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-tomcat</artifactId>

                 <scope>provided</scope>           

            </dependency>

                 <dependency>

                            <groupId>junit</groupId>

                            <artifactId>junit</artifactId>

                            <version>3.8.1</version>

                            <scope>test</scope>

                 </dependency>

        </dependencies>

        <properties>

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

        </properties>

        <build>

            <plugins>

                <plugin>

                    <groupId>org.springframework.boot</groupId>

                    <artifactId>spring-boot-maven-plugin</artifactId>

                </plugin>

            </plugins>

        </build>

    </project>

    步骤5创建war

    cd C:UsersX7TIDownloadsspringboot

    mvn clean package

    这样就在 target 目录下 生成了一个 springboot-0.0.1-SNAPSHOT.war 文件

     

    步骤6  重命名war包  然后部署

    如果用 springboot-0.0.1-SNAPSHOT.war 这个文件名部署,那么访问的时候就要在路径上加上springboot-0.0.1-SNAPSHOT。 所以把这个文件重命名为 ROOT.war
    然后把它放进tomcat 的webapps目录下。

    注:没有tomcat的同学可以在右上角下载。
    注: ROOT.war 并不是指访问的时候要使用 /ROOT/hello ,而是直接使用/hello 进行访问,ROOT表示根路径。

     

    步骤7启动并测试

    运行tomcat下的 bin目录里的startup.bat, 然后就可以启动了. 启动后访问如下地址测试:

     

    http://127.0.0.1:8080/hello

  • 相关阅读:
    LC.225. Implement Stack using Queues(using two queues)
    LC.232. Implement Queue using Stacks(use two stacks)
    sort numbers with two stacks(many duplicates)
    LC.154. Find Minimum in Rotated Sorted Array II
    LC.81. Search in Rotated Sorted Array II
    LC.35.Search Insert Position
    前后端分离:(一)
    Redis基本使用(一)
    GIT篇章(二)
    GIT篇章(一)
  • 原文地址:https://www.cnblogs.com/mzdljgz/p/11198087.html
Copyright © 2011-2022 走看看