转载请注明出处:http://blog.youkuaiyun.com/l1028386804/article/details/69940851
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
提供一个 SpringBootServletInitializer 子类,并覆盖它的 configure 方法。我们可以把应用的主类改为继承 SpringBootServletInitializer。或者另外写一个类。
@SpringBootApplication
public 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 包名。
另外,我们还可以使用 war 插件来定义打包以后的 war 包名称,以免 maven 为我们默认地起了一个带版本号的 war 包名称。例如:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>lyz</warName>
</configuration>
</plugin>
例如:
http://localhost:8080/lyz/test
其中,lyz 是我放在 tomcat 上的 war 包名。