From be7048b58fecff44dbc3d98b386ddbcaaef736c3 Mon Sep 17 00:00:00 2001 From: Filip Hanik Date: Tue, 14 Jul 2020 08:21:03 -0700 Subject: [PATCH 1/2] Avoid using reflection when configuring Tomcat listener This benefits native image building and AOT compilation, as reflection requires manual configuration. See gh-22329 --- .../TomcatWebSocketReactiveWebServerCustomizer.java | 6 ++++-- .../servlet/TomcatWebSocketServletWebServerCustomizer.java | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/reactive/TomcatWebSocketReactiveWebServerCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/reactive/TomcatWebSocketReactiveWebServerCustomizer.java index da424560d1..7db2190892 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/reactive/TomcatWebSocketReactiveWebServerCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/reactive/TomcatWebSocketReactiveWebServerCustomizer.java @@ -16,8 +16,9 @@ package org.springframework.boot.autoconfigure.websocket.reactive; -import org.apache.tomcat.websocket.server.WsContextListener; +import java.util.Collections; +import org.apache.tomcat.websocket.server.WsSci; import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.core.Ordered; @@ -33,7 +34,8 @@ public class TomcatWebSocketReactiveWebServerCustomizer @Override public void customize(TomcatReactiveWebServerFactory factory) { - factory.addContextCustomizers((context) -> context.addApplicationListener(WsContextListener.class.getName())); + factory.addContextCustomizers((context) -> + context.addServletContainerInitializer(new WsSci(), Collections.emptySet())); } @Override diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/TomcatWebSocketServletWebServerCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/TomcatWebSocketServletWebServerCustomizer.java index 86c33f8158..51e4114d41 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/TomcatWebSocketServletWebServerCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/TomcatWebSocketServletWebServerCustomizer.java @@ -16,8 +16,9 @@ package org.springframework.boot.autoconfigure.websocket.servlet; -import org.apache.tomcat.websocket.server.WsContextListener; +import java.util.Collections; +import org.apache.tomcat.websocket.server.WsSci; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.core.Ordered; @@ -35,7 +36,8 @@ public class TomcatWebSocketServletWebServerCustomizer @Override public void customize(TomcatServletWebServerFactory factory) { - factory.addContextCustomizers((context) -> context.addApplicationListener(WsContextListener.class.getName())); + factory.addContextCustomizers((context) -> + context.addServletContainerInitializer(new WsSci(), Collections.emptySet())); } @Override From 18a3459dc375108a4210381bf4b39075f75b6a2f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 15 Jul 2020 11:19:52 +0100 Subject: [PATCH 2/2] Polish "Avoid using reflection when configuring Tomcat listener" See gh-22329 --- .../TomcatWebSocketReactiveWebServerCustomizer.java | 8 +++----- .../TomcatWebSocketServletWebServerCustomizer.java | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/reactive/TomcatWebSocketReactiveWebServerCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/reactive/TomcatWebSocketReactiveWebServerCustomizer.java index 7db2190892..9bac4168f2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/reactive/TomcatWebSocketReactiveWebServerCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/reactive/TomcatWebSocketReactiveWebServerCustomizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,8 @@ package org.springframework.boot.autoconfigure.websocket.reactive; -import java.util.Collections; - import org.apache.tomcat.websocket.server.WsSci; + import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.core.Ordered; @@ -34,8 +33,7 @@ public class TomcatWebSocketReactiveWebServerCustomizer @Override public void customize(TomcatReactiveWebServerFactory factory) { - factory.addContextCustomizers((context) -> - context.addServletContainerInitializer(new WsSci(), Collections.emptySet())); + factory.addContextCustomizers((context) -> context.addServletContainerInitializer(new WsSci(), null)); } @Override diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/TomcatWebSocketServletWebServerCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/TomcatWebSocketServletWebServerCustomizer.java index 51e4114d41..e390b5bf2d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/TomcatWebSocketServletWebServerCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/TomcatWebSocketServletWebServerCustomizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,8 @@ package org.springframework.boot.autoconfigure.websocket.servlet; -import java.util.Collections; - import org.apache.tomcat.websocket.server.WsSci; + import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.core.Ordered; @@ -36,8 +35,7 @@ public class TomcatWebSocketServletWebServerCustomizer @Override public void customize(TomcatServletWebServerFactory factory) { - factory.addContextCustomizers((context) -> - context.addServletContainerInitializer(new WsSci(), Collections.emptySet())); + factory.addContextCustomizers((context) -> context.addServletContainerInitializer(new WsSci(), null)); } @Override