欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Spring Boot打war包的实例教程

时间:2021-05-28 08:00:57|栏目:JAVA代码|点击:

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
 */
@SpringBootApplication
public 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;
 }

实例源码下载

上一篇:Spring MVC项目开发踩过的一些bug

栏    目:JAVA代码

下一篇:【Redis缓存机制】详解Java连接Redis_Jedis_事务

本文标题:Spring Boot打war包的实例教程

本文地址:http://www.codeinn.net/misctech/130453.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有