首页 > 解决方案 > 没有“org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory”类型的合格bean

问题描述

我正在尝试自定义嵌入式 spring-boot tomcat,我创建了自定义程序类,如下所示

@Configuration
@PropertySource("classpath:configure_springboot.properties")
public class EmbeddedTomcatConfiguration {

  private static final String HTTP_PROTOCOL = "HTTP/1.1";

  private static final String URI_ENCODING = "UTF-8";

  @Value("${server.connection-timeout}")
  int serverConnectionTimeout;


  @Value("${tomcat.http.port}")
  int httpPort;

  @Bean
  public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
    return container -> container.addAdditionalTomcatConnectors(getHTTPConnector());
  }


  private Connector getHTTPConnector() {
    Connector httpConnector = new Connector(HTTP_PROTOCOL);
    httpConnector.setPort(httpPort);
    ((AbstractProtocol) httpConnector.getProtocolHandler()).setConnectionTimeout(serverConnectionTimeout);
    httpConnector.setURIEncoding(URI_ENCODING);
    return httpConnector;
  }

}

为了测试我的自定义,我在这里创建 TomcatServletWebServerFactory

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {EmbeddedTomcatConfiguration.class, ServletWebServerFactoryAutoConfiguration.class })
public class EmbeddedTomcatConfigurationTest {

  @Autowired
  TomcatServletWebServerFactory tomcatServletWebServerFactory;

  @Test
  public void tomcatHttpConnector() {
    assertNotNull(tomcatServletWebServerFactory);

  }

}

但我的测试因错误而失败

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.springboot.configure.EmbeddedTomcatConfigurationTest': Unsatisfied dependency expressed through field 'tomcatServletWebServerFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我从过去三天一直在尝试这个,有人可以指导我在这里缺少什么吗?

标签: javaspringspring-boot

解决方案


推荐阅读