java - 没有“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)}
我从过去三天一直在尝试这个,有人可以指导我在这里缺少什么吗?
解决方案
推荐阅读
- c# - 温莎城堡不会注入基本抽象类属性
- excel - VBA 从形状运行宏和屏幕提示(或工具提示)。我无法获得我发现工作的代码
- javascript - 将日期格式更改为字符串以仅显示月份和年份
- python - 基于第三个变量着色的散景散点图
- r - xgb.cv 似乎只使用训练数据进行 xfold 验证?
- reactjs - 用全圆角反应本机日历周期标记
- reactjs - 如果可选 prop 是 TypeScript 中的数组,如何在三元运算符中使用布尔赋值
- google-apps-script - 如果 J 列同一行中的单元格不为空,则覆盖 A 列中的单元格中的数据
- javascript - 在 .php 中的 Javascript 中使用 php
- qt - 如何读取 QTableWidget 的信息?