经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
Spring Boot 2构建可部署的war包
来源:cnblogs  作者:gdjlc  时间:2019/9/25 9:32:45  对本文有异议

默认情况下Spring Boot使用了内嵌的Tomcat服务器,项目最终被打成jar包运行,每个jar包可以被看作一个独立的Web服务器。
传统的Web开发,一般会将Web应用打成一个war包,然后将其部署到Web服务器中运行。
Spring Boot也支持传统的部署模式。

开发环境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8

1、新建一个名称为demo的Spring Boot项目。

2、修改pom.xml文件

下面粗体部分为所加代码,注释掉原来的build节点,该项目最终会打包成一个war-demo的war包。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.8.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <packaging>war</packaging>
  17. <build>
  18. <finalName>war-demo</finalName>
  19. </build>
  20.  
  21. <properties>
  22. <java.version>1.8</java.version>
  23. </properties>
  24.  
  25. <dependencies>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30.  
  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.  
  42. </dependencies>
  43.  
  44. <!-- <build>
  45. <plugins>
  46. <plugin>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-maven-plugin</artifactId>
  49. </plugin>
  50. </plugins>
  51. </build>-->
  52.  
  53. </project>

3、修改启动类方法 DemoApplication.cs

继承SpringBootServletInitializer,重写父类configure方法。增加测试用的控制器。

  1. package com.example.demo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.builder.SpringApplicationBuilder;
  5. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @SpringBootApplication
  9. @RestController
  10. public class DemoApplication extends SpringBootServletInitializer {
  11. protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
  12. return application.sources(DemoApplication.class);
  13. }
  14. public static void main(String[] args) {
  15. SpringApplication.run(DemoApplication.class, args);
  16. }
  17. @RequestMapping("/")
  18. public String test(){
  19. return "test";
  20. }
  21. }

4、先后点击IDEA的Maven窗口的clean和package

 

 到项目的target目录下,可看到生成了一个war-demo.war,把它拷贝到Tomcat的webapps目录下,启动Tomcat,

访问http://localhost:8080/war-demo/,可看到页面输出:test

附,项目结构:

 

原文链接:http://www.cnblogs.com/gdjlc/p/11581901.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号