Spring Boot打war包的執行個體教程,springwar

來源:互聯網
上載者:User

Spring Boot打war包的執行個體教程,springwar

Spring Boot除了可以打可執行jar包外,也支援傳統的war包。本文介紹如何使用Spring Boot構建傳統war包。

Spring Boot打war包步驟如下:

1、在pom.xml裡定義打包類型

<packaging>war</packaging>

2、添加Spring Boot啟動器(也可通過parent)

  <dependencyManagement> <dependencies>  <dependency>  <!-- Import dependency management from Spring Boot -->  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-dependencies</artifactId>  <version>1.5.6.RELEASE</version>  <type>pom</type>  <scope>import</scope>  </dependency> </dependencies> </dependencyManagement>

3、添加spring-boot-starter-web依賴

    <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-web</artifactId>  <exclusions>  <exclusion>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-tomcat</artifactId>  </exclusion>  </exclusions> </dependency>

4、添加打包外掛程式

  <build> <plugins>  <plugin>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-maven-plugin</artifactId>  </plugin> </plugins> </build>

5、主類繼承SpringBootServletInitializer

/** * WAR application */@SpringBootApplicationpublic class WarApplication extends SpringBootServletInitializer {  public static void main(String[] args) { SpringApplication.run(WarApplication.class, args); }}

6、執行mvn clean package打包

$mvn clean package

7、將打好的war包拷貝到容器(如tomcat)運行即可。

這裡需要簡單說明下:

主應用可以重寫SpringBootServletInitializer裡面有configure方法,自訂配置Spring Boot。

  /** * Configure the application. Normally all you would need to do is to add sources * (e.g. config classes) because other settings have sensible defaults. You might * choose (for instance) to add default command line arguments, or set an active * Spring profile. * @param builder a builder for the application context * @return the application builder * @see SpringApplicationBuilder */ protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder; }

執行個體源碼下載

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.