【问题标题】:Spring boot Test fails saying, Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory beanSpring Boot Test 失败说,由于缺少 ServletWebServerFactory bean,无法启动 ServletWebServerApplicationContext
【发布时间】:2018-03-09 21:18:59
【问题描述】:

测试类:-

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { WebsocketSourceConfiguration.class,
        WebSocketSourceIntegrationTests.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
                "websocket.path=/some_websocket_path", "websocket.allowedOrigins=*",
                "spring.cloud.stream.default-binder=kafka" })
public class WebSocketSourceIntegrationTests {

    private String port = "8080";

    @Test
    public void testWebSocketStreamSource() throws IOException, InterruptedException {
        StandardWebSocketClient webSocketClient = new StandardWebSocketClient();
        ClientWebSocketContainer clientWebSocketContainer = new ClientWebSocketContainer(webSocketClient,
                "ws://localhost:" + port + "/some_websocket_path");
        clientWebSocketContainer.start();
        WebSocketSession session = clientWebSocketContainer.getSession(null);
        session.sendMessage(new TextMessage("foo"));
        System.out.println("Done****************************************************");
    }

}

我看到了同样的问题here,但没有任何帮助。我可以知道我错过了什么吗?

我在依赖层次结构中有 spring-boot-starter-tomcat 作为编译时依赖。

【问题讨论】:

    标签: java spring spring-boot spring-cloud-stream


    【解决方案1】:

    此消息说: 您需要在 ApplicationContext 中配置至少 1 个 ServletWebServerFactory bean,因此如果您已经拥有 spring-boot-starter-tomcat y您需要自动配置该 bean 或手动配置强>。

    所以,在测试中只有2个配置类来加载applicationContext,它们是= { WebsocketSourceConfiguration.class, WebSocketSourceIntegrationTests.class },那么至少在这些类中应该有一个@Bean方法返回一个实例所需的 ServletWebServerFactory。

    * 解决方案 *

    确保加载配置类中的所有 bean

    WebsocketSourceConfiguration {
      @Bean 
      ServletWebServerFactory servletWebServerFactory(){
      return new TomcatServletWebServerFactory();
      }
    }
    

    或者还启用 AutoConfiguration 来对这些 bean 进行类路径扫描和自动配置。

    @EnableAutoConfiguration
    WebsocketSourceConfiguration
    

    也可以在集成测试课上完成。

    @EnableAutoConfiguration
    WebSocketSourceIntegrationTests
    

    有关更多信息,请查看 SpringBootTest 注释文档 https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html

    【讨论】:

    • 还有:import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; @SpringBootTest(classes = {ServletWebServerFactoryAutoConfiguration.class, ... })
    • 这里的 WebSocketSourceIntegrationTests 到底是什么?
    • @bharal WebSocketSourceIntegrationTests 是描述/问题中发布的类的名称。
    • 对我来说,这发生在我的测试配置类中有@Configuration 而不是@TestConfiguration (在@SpringBootTest 注释中的“类”下列出)
    • 只需添加 @TestConfiguration 带注释的配置类(仅此而已)为我解决了这个问题,谢谢!
    【解决方案2】:

    2.0.5.RELEASE 中,当我遇到以下问题时,我遇到了类似的问题。

    package radon;
    ..
    @SpringBootApplication
    public class Initializer {
        public static void main(String[] args) {
            SpringApplication.run(Config.class, args);
        }
    }
    
    package radon.app.config;
    @Configuration
    @ComponentScan({ "radon.app" })
    public class Config {
        ..
    }
    

    将 Initializer 的包从 radon 更改为 radon.app 解决了这个问题。

    【讨论】:

      【解决方案3】:

      这是因为spring 无法在runtime 加载属性文件,我使用spring 配置文件并且没有在runtime( java -jar application.jar) 提供(程序或虚拟机)参数,添加 vm 参数profile 为我解决了这个问题。

      java -jar -Dspring.profiles.active=dev application.jar
      

      或使用程序参数

      java -jar application.jar --spring.profiles.active=prod --spring.config.location=c:\config
      

      【讨论】:

      • 尽量使用正确的拼写、标点和语法。
      【解决方案4】:

      对于 Web 应用程序,在主类中扩展 *SpringBootServletInitializer*

      @SpringBootApplication
      public class YourAppliationName extends SpringBootServletInitializer{
          public static void main(String[] args) {
              SpringApplication.run(YourAppliationName.class, args);
          }
      }
      

      【讨论】:

      • 即使在尝试后也出现同样的问题
      猜你喜欢
      • 2019-08-07
      • 2018-10-18
      • 2019-04-13
      • 2021-06-19
      • 2019-12-03
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多