如何为SpringBoot 2创建自定义嵌入式servlet容器

时间:2019-09-11 03:43:09

标签: java spring spring-boot spring-mvc servlets

我正在尝试为SpringBoot 2创建一个自定义的嵌入式servlet容器。我已经创建了ServletWebServerFactoryWebServer接口的实现。在我的应用程序中,我从spring-boot-starter-web中明确排除了嵌入式Tomcat,然后将我的自定义工厂类手动提供给SpringApplication构造函数。调用我的start()实现的WebServer方法时,我将ServletContext传递给传递给该方法的ServletContextInitializer方法的所有getWebServer工厂。

当我调用WebServer的{​​{1}}方法时,我可以看到Spring正在加载我的工厂并正确地抓住了run()对象。但是,启动失败并显示以下错误:

SpringApplication

我想念什么?我应该将org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set 传递给另一个地方吗?

1 个答案:

答案 0 :(得分:0)

我设法通过将onStartup(ServletContext)的{​​{1}}方法传递给我的ServletContextInitializer的{​​{1}}方法的getWebServer()方法来解决此问题,已初始化的ServletWebServerFactory-无需等待ServletContext实现上调用start()方法。尽管这解决了我的问题,但我不知道是否做正确的事,我仍然想听听SpringBoot专家的意见。