spring boot使用外部tomcat部署
1:pom里面的packaging修改为war(<packaging>war</packaging>)
2:在pom依赖spring-boot-starter-web排除内置的tomcat,具体如下:
<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>
<version>RELEASE</version>
</dependency>
3.pom中添加servlet-api依赖:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
4.修改原来spring-boot的启动类,注释掉main方法,继承SpringBootServletInitializer,重写configure方法,例如下:
@SpringBootApplication
@ServletComponentScan
public class TransitApplication extends SpringBootServletInitializer {
/*public static void main(String[] args) {
SpringApplication.run(TransitApplication.class, args);
}*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// 注意这里要指向原先用main方法执行的Application启动类
return builder.sources(TransitApplication.class);
}
}