【发布时间】:2014-04-15 17:30:06
【问题描述】:
我希望覆盖 here 中记录的默认 EmbeddedServletContainerFactory 以设置 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:无法 由于多个原因启动 EmbeddedWebApplicationContext EmbeddedServletContainerFactory bean: servletContainer,tomcatEmbeddedServletContainerFactory 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:135) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476) 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120) 在 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) 在 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:606) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) 引起:org.springframework.context.ApplicationContextException: 由于多个原因,无法启动 EmbeddedWebApplicationContext EmbeddedServletContainerFactory bean: servletContainer,tomcatEmbeddedServletContainerFactory 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:190) 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:132) ... 12 更多
我创建了一个GitHub repo,它融合了 Spring Boot 项目中的 Tomcat 和 Websocket 示例,并应用了此配置。
【问题讨论】:
-
应该可以工作(并且文档的链接是最新的,尽管它与您实现的模式不同,因为它实际上并没有创建 @987654327 类型的
@Bean@)。可以分享一个项目吗? -
感谢 Dave 的回复,我可以通过将 tomcat 和 websockets 样本放在一起来重现 - 只要 spring-boot-starter-websocket 是依赖项,就会发生这种情况,可能是因为一些与Web 套接字和容器。如果有帮助,我可以发布这两个示例的混搭。
-
确实是
WebSocketAutoConfiguration是罪魁祸首,看起来在创建此类之前检测现有的TomcatEmbeddedServletContainerFactorybean 可能是诀窍,我将进行试验并提出拉取请求如果有效。
标签: spring spring-boot embedded-tomcat-7