问题分析:
springboot 内置了Tomcat插件,Application作为启动入口,初始化时会去找内置的Tomcat
/*
* Copyright (c) 2016, inter3i.com. All rights reserved.
*
* Author: jiangxingqi
* Created: 11/18/16 6:01 PM
* Description:
*
*/
package com.inter3i.sun.api.ota.v1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@ComponentScan
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@EnableConfigurationProperties
@SpringBootApplication
@ImportResource("classpath:dispatcher-servlet.xml")
public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {
private static final String CONTEXT_PATH = "/otaapi";
/* @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}*/
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(7080);
container.setContextPath(CONTEXT_PATH);
}
}
如果在pom文件中移除了Tomcat插件,使用自定义的tomcat来启动时,程序就会报这样的bug,解决方法就是使用Spring内置的Tomcat插件
<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.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.0.36</version>
</dependency>-->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
-->