英文:
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("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;
}
}
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 '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 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}
专注分享java语言的经验与见解,让所有开发者获益!
评论