spring boot默认创建出来的应用程序是内嵌web容器的,直接运行jar文件就可以的,但通常我们也需要将程序部署到tomcat中,这需要做如下改进:
1.pom.xml修改
打包方式需要修改成war:<packaging>war</packaging>
spring-boot-starter-web的配置中需要排除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>
同时为了应用能够正常编译,还需要加上tomcat的引用:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
2.重写SpringBootServletInitializer类中的configure方法
- @Configuration
- @ComponentScan
- @EnableAutoConfiguration
- 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);
- }
- }
最后将应用打包成war包,并将war包部署到tomcat上就可以了.