Spring Boot(二):war包形式发布

在上一篇入门文章中,也提及了Spring Boot可以发布成war包运行在外包的tomcat中.具体做法如下.

  1. 在POM.XML文件中将jar改为war
  1. 添加依赖.
    1
    2
    3
    4
    5
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
    </dependency>

将tomcat的范围改为provided,编译时需要.

3.接下来添加ServletInitializer类,内容如下:

1
2
3
4
5
6
7
8
9
10
11
package com.kiritor.springboot02;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}

其中Application为标注有@SpringBootApplication注解的主启动类.

  1. 需要注意的问题是,如果配置了server.context-path=/spring-boot配置项,则该配置项的值应该和war包名字一致.

本文链接: http://kiritor.github.io/2017/04/05/springboot-war/

-- EOF --

如果文章对您有用请随意打赏,谢谢支持!