Spring Boot 1.0.0-RC4以多个EmbeddedServletContainerFactory实例启动

我希望覆盖默认的EmbeddedServletContainerFactory,如文档here,以便设置SSL . old docs(来自RC1天)表示要覆盖定制器,并且在我今天升级之前工作得很好,改变了实现以遵循新约定 .

@Configuration
public class ContainerConfig {

@Bean
public EmbeddedServletContainerFactory servletContainer() {
  TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
  factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {

  @Override
  public void customize(Connector connector) {

    connector.setPort(8443);
    connector.setSecure(true);
    connector.setScheme("https");
    connector.setAttribute("keyAlias", "tomcat");
    connector.setAttribute("keystorePass", "changeit");
    try {
      connector.setAttribute("keystoreFile", ResourceUtils.getFile("src/ssl/tomcat.keystore").getAbsolutePath());
    } catch (FileNotFoundException e) {
      throw new IllegalStateException("Cannot load keystore", e);
    }
      connector.setAttribute("clientAuth", "false");
      connector.setAttribute("sslProtocol", "TLS");
      connector.setAttribute("SSLEnabled", true);
    }

  });

  factory.setSessionTimeout(10, TimeUnit.MINUTES);

  return factory;
}

Boot(EmbeddedServletContainerAutoConfiguration)中的源代码表明如果找到我的bean,它将不会注册默认值:

@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)

然而它似乎无论如何都要注册 . 有没有人有这个工作?这是堆栈:

线程“main”中的异常org.springframework.context.ApplicationContextException:无法启动嵌入式容器;嵌套异常是org.springframework.context.ApplicationContextException:由于多个EmbeddedServletContainerFactory bean无法启动EmbeddedWebApplicationContext:位于org.springframework的org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:135)中的servletContainer,tomcatEmbeddedServletContainerFactory . 在org.springframework.boot.SpringApplication.refresh(SpringApplication.java: 619)org.springframework.boot.SpringApplication.run(SpringApplication.java:306)org.springframework.boot.SpringApplication.run(SpringApplication.java:880)org.springframework.boot.SpringApplication.run(SpringApplication.java) :869)at sun.reflect.NativeMeth at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMeth od.ccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:606)at com.intellij.rt.execution .application.AppMain.main(AppMain.java:120)引起:org.springframework.context.ApplicationContextException:由于多个EmbeddedServletContainerFactory bean无法启动EmbeddedWebApplicationContext:servletContainer,org.springframework.boot.context.embedded.EmbeddedWebApplicationContext中的tomcatEmbeddedServletContainerFactory . 位于org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158)的orE.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:132)中的getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:190) ......还有12个

我创建了一个GitHub repo,用于修复Spring Boot项目中的Tomcat和Websocket示例,并应用此配置 .

回答(1)

3 years ago

这是一种(某种)错误(https://github.com/spring-projects/spring-boot/issues/479) . 现在修复了 . 解决方法是使用 EmbeddedServletContainerCustomizer 而不是 EmbeddedServletContainerFactory (就像原始问题中的文档链接一样) .