SpringBoot在Tomcat部署war包
启动类配置
继承SpringBootServletInitializer
@SpringBootApplication
public class TestApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(TestApplication.class);
}
}
打包方式配置
war
移除内置Tomcat
org.springframework.boot
spring-boot-starter-tomcat
provided
WebSocket错误
javax.servlet
javax.servlet-api
3.1.0
provided
Bean按需加载
@Configuration
public class WebSocketConfig {
@Bean
@ConditionalOnProperty(name = "system.package", havingValue = "jar", matchIfMissing = true)
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
Tomcat设置
Host节点增加Context可以直接通过ip+端口方式访问,需要将appBase清除,防止启动两次应用
注意事项
对于框架封装引用jar包,需要注意工程项目中只能有一个类继承自SpringBootServletInitializer,否则会导致ApplicationContext初始化两次