Avoid creating a new EmbeddedServletContainerFactory for websockets
User can now also switch off and customize the websockets customizer by adding a bean named "websocketContainerCustomizer". Fixes gh-479
This commit is contained in:
@@ -18,20 +18,22 @@ package org.springframework.boot.autoconfigure.aop;
|
||||
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.reflect.Advice;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
|
||||
/**
|
||||
*
|
||||
* <p> {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration}
|
||||
* for Spring's AOP support. Equivalent to enabling {@link org.springframework.context.annotation.EnableAspectJAutoProxy}
|
||||
* in your configuration. The configuration will not be activated if {@literal spring.aop.auto=false}.
|
||||
* The {@literal proxyTargetClass} attribute will be {@literal false}, by default, but can be overridden by
|
||||
* specifying {@literal spring.aop.proxyTargetClass=true}.
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
|
||||
* Auto-configuration} for Spring's AOP support. Equivalent to enabling
|
||||
* {@link org.springframework.context.annotation.EnableAspectJAutoProxy} in your
|
||||
* configuration. The configuration will not be activated if
|
||||
* {@literal spring.aop.auto=false}. The {@literal proxyTargetClass} attribute will be
|
||||
* {@literal false}, by default, but can be overridden by specifying
|
||||
* {@literal spring.aop.proxyTargetClass=true}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Josh Long
|
||||
* @see EnableAspectJAutoProxy
|
||||
|
||||
@@ -53,8 +53,7 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer {
|
||||
|
||||
private Integer sessionTimeout;
|
||||
|
||||
@NotNull
|
||||
private String contextPath = "";
|
||||
private String contextPath;
|
||||
|
||||
@NotNull
|
||||
private String servletPath = "/";
|
||||
|
||||
@@ -23,7 +23,11 @@ import org.apache.catalina.deploy.ApplicationListener;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer;
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -47,14 +51,31 @@ public class WebSocketAutoConfiguration {
|
||||
"org.apache.tomcat.websocket.server.WsContextListener", false);
|
||||
|
||||
@Bean
|
||||
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
|
||||
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory() {
|
||||
@ConditionalOnMissingBean(name = "websocketContainerCustomizer")
|
||||
public EmbeddedServletContainerCustomizer websocketContainerCustomizer() {
|
||||
|
||||
EmbeddedServletContainerCustomizer customizer = new EmbeddedServletContainerCustomizer() {
|
||||
|
||||
@Override
|
||||
protected void postProcessContext(Context context) {
|
||||
context.addApplicationListener(WS_APPLICATION_LISTENER);
|
||||
public void customize(ConfigurableEmbeddedServletContainer container) {
|
||||
if (!(container instanceof TomcatEmbeddedServletContainerFactory)) {
|
||||
throw new IllegalStateException(
|
||||
"Websockets are currently only supported in Tomcat (found "
|
||||
+ container.getClass() + ")");
|
||||
}
|
||||
((TomcatEmbeddedServletContainerFactory) container)
|
||||
.addContextCustomizers(new TomcatContextCustomizer() {
|
||||
@Override
|
||||
public void customize(Context context) {
|
||||
context.addApplicationListener(WS_APPLICATION_LISTENER);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
return factory;
|
||||
|
||||
return customizer;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletCont
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
@@ -75,7 +76,7 @@ public class ServerPropertiesTests {
|
||||
public void testCustomizeTomcat() throws Exception {
|
||||
ConfigurableEmbeddedServletContainer factory = mock(ConfigurableEmbeddedServletContainer.class);
|
||||
this.properties.customize(factory);
|
||||
verify(factory).setContextPath("");
|
||||
verify(factory, times(0)).setContextPath("");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user