按照@philwebb说法,嵌入式环境下,SpringBoot有意忽略javax.servlet.ServletContainerInitializer,原因:
The search algorithm used by the containers was problematic. It also causes problems when you want to develop an executable WAR as you often want a javax.servlet.ServletContainerInitializer for the WAR that is not executed when you run java -jar.
解决方案:
1. 注册org.springframework.boot.context.embedded.ServletContextInitializer类型的Bean代替ServletContainerInitializer。
2. 直接向容器注册Servlet和Filter。
3. 向容器注册ServletRegistrationBean和FilterRegistrationBean。
参考:
[1]https://github.com/spring-projects/spring-boot/issues/321
[2]https://github.com/spring-projects/spring-boot/issues/522