You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # spring-boot-demo-war
  2. > 本 demo 主要演示了如何将 Spring Boot 项目打包成传统的 war 包程序。
  3. ## pom.xml
  4. ```xml
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>spring-boot-demo-war</artifactId>
  10. <version>1.0.0-SNAPSHOT</version>
  11. <!-- 若需要打成 war 包,则需要将打包方式改成 war -->
  12. <packaging>war</packaging>
  13. <name>spring-boot-demo-war</name>
  14. <description>Demo project for Spring Boot</description>
  15. <parent>
  16. <groupId>com.xkcoding</groupId>
  17. <artifactId>spring-boot-demo</artifactId>
  18. <version>1.0.0-SNAPSHOT</version>
  19. </parent>
  20. <properties>
  21. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  22. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  23. <java.version>1.8</java.version>
  24. </properties>
  25. <dependencies>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30. <!-- 若需要打成 war 包,则需要将 tomcat 引入,scope 设置为 provided -->
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-tomcat</artifactId>
  34. <scope>provided</scope>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. </dependency>
  41. </dependencies>
  42. <build>
  43. <finalName>spring-boot-demo-war</finalName>
  44. <plugins>
  45. <plugin>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-maven-plugin</artifactId>
  48. </plugin>
  49. </plugins>
  50. </build>
  51. </project>
  52. ```
  53. ## SpringBootDemoWarApplication.java
  54. ```java
  55. /**
  56. * <p>
  57. * 启动器
  58. * </p>
  59. *
  60. * @package: com.xkcoding.war
  61. * @description: 启动器
  62. * @author: shenyangkai
  63. * @date: Created in 2018/10/30 19:37
  64. * @copyright: Copyright (c) 2018
  65. * @version: V1.0
  66. * @modified: shenyangkai
  67. */
  68. @SpringBootApplication
  69. public class SpringBootDemoWarApplication extends SpringBootServletInitializer {
  70. public static void main(String[] args) {
  71. SpringApplication.run(SpringBootDemoWarApplication.class, args);
  72. }
  73. /**
  74. * 若需要打成 war 包,则需要写一个类继承 {@link SpringBootServletInitializer} 并重写 {@link SpringBootServletInitializer#configure(SpringApplicationBuilder)}
  75. */
  76. @Override
  77. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  78. return application.sources(SpringBootDemoWarApplication.class);
  79. }
  80. }
  81. ```
  82. ## 参考
  83. https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#howto-create-a-deployable-war-file

一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 55 个。