From 785b04c15668fa49843382ecd0980ae48e43cb9c Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Wed, 31 Oct 2018 22:18:07 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20spring-boot-demo-war=20=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot-demo-war/README.md | 102 ++++++++++++++++++ spring-boot-demo-war/pom.xml | 94 ++++++++-------- .../war/SpringBootDemoWarApplication.java | 12 ++- .../src/main/resources/application.yml | 4 + 4 files changed, 168 insertions(+), 44 deletions(-) create mode 100644 spring-boot-demo-war/README.md diff --git a/spring-boot-demo-war/README.md b/spring-boot-demo-war/README.md new file mode 100644 index 0000000..8976045 --- /dev/null +++ b/spring-boot-demo-war/README.md @@ -0,0 +1,102 @@ +# spring-boot-demo-war + +> 本 demo 主要演示了如何将 Spring Boot 项目打包成传统的 war 包程序。 + +## pom.xml + +```xml + + + 4.0.0 + + spring-boot-demo-war + 0.0.1-SNAPSHOT + + war + + spring-boot-demo-war + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo + 0.0.1-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-tomcat + provided + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + spring-boot-demo-war + + + org.springframework.boot + spring-boot-maven-plugin + + + + + +``` + +## SpringBootDemoWarApplication.java + +```java +/** + *

+ * 启动器 + *

+ * + * @package: com.xkcoding.war + * @description: 启动器 + * @author: shenyangkai + * @date: Created in 2018/10/30 19:37 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: shenyangkai + */ +@SpringBootApplication +public class SpringBootDemoWarApplication extends SpringBootServletInitializer { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoWarApplication.class, args); + } + + /** + * 若需要打成 war 包,则需要写一个类继承 {@link SpringBootServletInitializer} 并重写 {@link SpringBootServletInitializer#configure(SpringApplicationBuilder)} + */ + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(SpringBootDemoWarApplication.class); + } +} +``` + +## 参考 + +https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#howto-create-a-deployable-war-file + diff --git a/spring-boot-demo-war/pom.xml b/spring-boot-demo-war/pom.xml index 6d8f250..38fc08a 100644 --- a/spring-boot-demo-war/pom.xml +++ b/spring-boot-demo-war/pom.xml @@ -1,48 +1,56 @@ - 4.0.0 - - spring-boot-demo-war - 0.0.1-SNAPSHOT - jar - - spring-boot-demo-war - Demo project for Spring Boot - - - com.xkcoding - spring-boot-demo - 0.0.1-SNAPSHOT - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-test - test - - - - - spring-boot-demo-war - - - org.springframework.boot - spring-boot-maven-plugin - - - + 4.0.0 + + spring-boot-demo-war + 0.0.1-SNAPSHOT + + war + + spring-boot-demo-war + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo + 0.0.1-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-tomcat + provided + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + spring-boot-demo-war + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/spring-boot-demo-war/src/main/java/com/xkcoding/war/SpringBootDemoWarApplication.java b/spring-boot-demo-war/src/main/java/com/xkcoding/war/SpringBootDemoWarApplication.java index 016387e..2ef1d1b 100644 --- a/spring-boot-demo-war/src/main/java/com/xkcoding/war/SpringBootDemoWarApplication.java +++ b/spring-boot-demo-war/src/main/java/com/xkcoding/war/SpringBootDemoWarApplication.java @@ -2,6 +2,8 @@ package com.xkcoding.war; 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; /** *

@@ -17,9 +19,17 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * @modified: shenyangkai */ @SpringBootApplication -public class SpringBootDemoWarApplication { +public class SpringBootDemoWarApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(SpringBootDemoWarApplication.class, args); } + + /** + * 若需要打成 war 包,则需要写一个类继承 {@link SpringBootServletInitializer} 并重写 {@link SpringBootServletInitializer#configure(SpringApplicationBuilder)} + */ + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(SpringBootDemoWarApplication.class); + } } diff --git a/spring-boot-demo-war/src/main/resources/application.yml b/spring-boot-demo-war/src/main/resources/application.yml index e69de29..a02fbde 100644 --- a/spring-boot-demo-war/src/main/resources/application.yml +++ b/spring-boot-demo-war/src/main/resources/application.yml @@ -0,0 +1,4 @@ +server: + port: 8080 + servlet: + context-path: /demo \ No newline at end of file