把spring-boot项目按照平常的web项目一样发布到tomcat容器下
修改打包形式:
在pom.xml里设置:<packaging>war</packaging>
-
移除自带tomcat插件(springboot自带tomcat)
第一种方式:在pom.xml里找到节点:spring-boot-starter-web,添加如下代码:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 移除嵌入式tomcat插件 --> <exclusions> <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-web</artifactId> </dependency> <!--打包时排除tomcat--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
-
修改启动类,重写初始化方法
重点关注:①需要继承SpringBootServletInitializer,②重写configure方法@Slf4j @EnableTransactionManagement @SpringBootApplication @MapperScan("com.abc.*.mapper") public class AppApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(AppApplication.class, args); log.info("启动完成"); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){ return builder.sources(AppApplication.class); } }