我正在尝试创建一个库,该库可以使SpringBoot应用程序暴露在第二个端口(8090)上。
为此,我有一个自动配置,如下所示:
@Configuration
@ConditionalOnWebApplication(
type = Type.SERVLET
)
public class HttpConfig {
public HttpConfig() {
}
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addAdditionalTomcatConnectors(new Connector[]{this.createStanderConnecter()});
return factory;
}
private Connector createStanderConnecter() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(8090);
return connector;
}
}
如果我在同一项目中定义此配置,则可以正常工作。 但是,如果我将此配置作为模块导入,则我的应用程序无法启动并出现以下错误:
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to multiple ServletWebServerFactory beans : tomcatServletWebServerFactory,servletContainer
我想念什么?
答案 0 :(得分:0)
我想。如果其他人想完成我想做的事情。除了配置之外,我还必须实现WebServerFactoryCustomizer
,如下所示:
@Component
@ConditionalOnWebApplication(
type = Type.SERVLET
)
public class HttpConfig implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Autowired
ApplicationContext context;
public HttpConfig() {
}
public void customize(TomcatServletWebServerFactory factory) {
factory.addAdditionalTomcatConnectors(new Connector[]{this.createStanderConnecter()});
}
private Connector createStanderConnecter() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(8090);
return connector;
}
}