您的位置:首页 > 运维架构 > Tomcat

如何让 Spring Boot 项目发布到 Tomcat 服务器

2016-11-28 10:24 381 查看
第 1 步:将这个 Spring Boot 项目的打包方式设置为 war。

<packaging>war</packaging>

这里还要多说一句, SpringBoot 默认有内嵌的 tomcat 模块,因此,我们要把这一部分排除掉。

即:我们在 spring-boot-starter-web 里面排除了 spring-boot-starter-tomcat ,但是我们为了在本机测试方便,我们还要引入它,所以我们这样写:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<exclusions>

<exclusion>

<artifactId>log4j-over-slf4j</artifactId>

<groupId>org.slf4j</groupId>

</exclusion>

<exclusion>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

</exclusion>

</exclusions></dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

<scope>provided</scope></dependency>

第 2 步:提供一个 SpringBootServletInitializer 子类,并覆盖它的 configure 方法。我们可以把应用的主类改为继承 SpringBootServletInitializer。或者另外写一个类。

@SpringBootApplicationpublic class Application extends SpringBootServletInitializer {

@Override

protected SpringApplicationBuilder configure(

SpringApplicationBuilder application) {

return application.sources(Application.class);

}

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

注意:部署到 tomcat 以后,访问这个项目的时候,须要带上这个项目的 war 包名。

例如:
http://localhost:8080/Boot-1.0/user/show/18
其中,Boot-1.0 是我放在 tomcat 上的 war 包名。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息