有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

当我调用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送到吗


共 (1) 个答案

  1. # 1 楼答案

    我设法解决了这个问题,当它们被传递到我的ServletWebServerFactorygetWebServer()方法时,我调用了ServletContextInitializeronStartup(ServletContext)方法来传递我的自定义、初始化的ServletContext,而不用等待在WebServer实现上调用start()方法。虽然这解决了我的问题,但我不知道这是不是正确的做法,我还是想听听SpringBoot专家的意见