【问题标题】:How to create a custom embedded servlet container for SpringBoot 2如何为 Spring Boot 2 创建自定义嵌入式 servlet 容器
【发布时间】:2020-01-12 20:42:00
【问题描述】:

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

当我调用SpringApplicationrun() 方法时,我可以看到Spring 正在加载我的工厂并正确抓取我的WebServer 对象。但是,启动失败并出现以下错误:

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

我错过了什么?我应该将ServletContext 传递到另一个地方吗?

【问题讨论】:

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


    【解决方案1】:

    我设法通过调用ServletContextInitializers 的onStartup(ServletContext) 方法来解决问题,当它们传递给我的ServletWebServerFactorygetWebServer() 方法时,传入我的自定义初始化ServletContext - 无需等待要在 WebServer 实现上调用的 start() 方法。虽然这解决了我的问题,但我不知道这样做是否正确,但我仍然想听听 SpringBoot 专家的意见。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-20
      • 1970-01-01
      • 2014-11-13
      • 2020-07-10
      • 2017-11-17
      • 1970-01-01
      • 2020-10-02
      • 2013-11-27
      相关资源
      最近更新 更多