No qualifying bean of type 'org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory'

huangapple 未分类评论51阅读模式
英文:

No qualifying bean of type 'org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory'

问题

我正在尝试自定义嵌入式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)}

我已经尝试了三天,有人可以指导我在这里漏掉了什么吗?

英文:

I am trying to customize embedded spring-boot tomcat, I created customizer class as shown here

@Configuration
@PropertySource(&quot;classpath:configure_springboot.properties&quot;)
public class EmbeddedTomcatConfiguration {

  private static final String HTTP_PROTOCOL = &quot;HTTP/1.1&quot;;

  private static final String URI_ENCODING = &quot;UTF-8&quot;;

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


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

  @Bean
  public WebServerFactoryCustomizer&lt;TomcatServletWebServerFactory&gt; tomcatCustomizer() {
    return container -&gt; 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;
  }

}

To test my customization i am creating TomcatServletWebServerFactory here

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

  @Autowired
  TomcatServletWebServerFactory tomcatServletWebServerFactory;

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

  }

}

but my test is failing with error

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

I have been trying this from the last three days, can someone guide me what am I missing here?

答案1

得分: 0

在我的情况下,我不小心对控制器进行了两次注解:

@SpringBootTest(classes = {MyController.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

加上

@Autowired
private MyController myController;

通过移除 classes = {MyController.class} 来修复了这个问题。

英文:

In my case I accidentally annotated the controller twice:

@SpringBootTest(classes = {MyController.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

plus

@Autowired
private MyController myController;

Fixed it by removing classes = {MyController.class}

huangapple
  • 本文由 发表于 2020年4月8日 17:08:54
  • 转载请务必保留本文链接:https://java.coder-hub.com/61097110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定