Skip to content

Commit 0172424

Browse files
committed
Avoid CGLIB proxies on websocket/messaging configurations
This commit updates websocket and messaging configurations in order to not use CGLIB proxies anymore. The goal here is to allow support in native executables and to increase the consistency across the portfolio. Closes gh-26227
1 parent 994ec70 commit 0172424

File tree

8 files changed

+144
-103
lines changed

8 files changed

+144
-103
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java

+85-60
Large diffs are not rendered by default.

spring-messaging/src/test/java/org/springframework/messaging/simp/config/MessageBrokerConfigurationTests.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.springframework.context.annotation.Configuration;
3131
import org.springframework.context.support.StaticApplicationContext;
3232
import org.springframework.core.Ordered;
33+
import org.springframework.core.task.TaskExecutor;
3334
import org.springframework.lang.Nullable;
3435
import org.springframework.messaging.Message;
3536
import org.springframework.messaging.MessageChannel;
@@ -594,19 +595,20 @@ public TestController subscriptionController() {
594595

595596
@Override
596597
@Bean
597-
public AbstractSubscribableChannel clientInboundChannel() {
598+
public AbstractSubscribableChannel clientInboundChannel(TaskExecutor clientInboundChannelExecutor) {
598599
return new TestChannel();
599600
}
600601

601602
@Override
602603
@Bean
603-
public AbstractSubscribableChannel clientOutboundChannel() {
604+
public AbstractSubscribableChannel clientOutboundChannel(TaskExecutor clientOutboundChannelExecutor) {
604605
return new TestChannel();
605606
}
606607

607608
@Override
608609
@Bean
609-
public AbstractSubscribableChannel brokerChannel() {
610+
public AbstractSubscribableChannel brokerChannel(AbstractSubscribableChannel clientInboundChannel,
611+
AbstractSubscribableChannel clientOutboundChannel, TaskExecutor brokerChannelExecutor) {
610612
return new TestChannel();
611613
}
612614
}
@@ -680,20 +682,21 @@ protected void configureMessageBroker(MessageBrokerRegistry registry) {
680682

681683
@Override
682684
@Bean
683-
public AbstractSubscribableChannel clientInboundChannel() {
685+
public AbstractSubscribableChannel clientInboundChannel(TaskExecutor clientInboundChannelExecutor) {
684686
// synchronous
685687
return new ExecutorSubscribableChannel(null);
686688
}
687689

688690
@Override
689691
@Bean
690-
public AbstractSubscribableChannel clientOutboundChannel() {
692+
public AbstractSubscribableChannel clientOutboundChannel(TaskExecutor clientOutboundChannelExecutor) {
691693
return new TestChannel();
692694
}
693695

694696
@Override
695697
@Bean
696-
public AbstractSubscribableChannel brokerChannel() {
698+
public AbstractSubscribableChannel brokerChannel(AbstractSubscribableChannel clientInboundChannel,
699+
AbstractSubscribableChannel clientOutboundChannel, TaskExecutor brokerChannelExecutor) {
697700
// synchronous
698701
return new ExecutorSubscribableChannel(null);
699702
}

spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/DelegatingWebSocketConfiguration.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,9 +29,10 @@
2929
* configure WebSocket request handling.
3030
*
3131
* @author Rossen Stoyanchev
32+
* @author Sebastien Deleuze
3233
* @since 4.0
3334
*/
34-
@Configuration
35+
@Configuration(proxyBeanMethods = false)
3536
public class DelegatingWebSocketConfiguration extends WebSocketConfigurationSupport {
3637

3738
private final List<WebSocketConfigurer> configurers = new ArrayList<>();

spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/DelegatingWebSocketMessageBrokerConfiguration.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,9 +37,10 @@
3737
* <p>This class is typically imported via {@link EnableWebSocketMessageBroker}.
3838
*
3939
* @author Rossen Stoyanchev
40+
* @author Sebastien Deleuze
4041
* @since 4.0
4142
*/
42-
@Configuration
43+
@Configuration(proxyBeanMethods = false)
4344
public class DelegatingWebSocketMessageBrokerConfiguration extends WebSocketMessageBrokerConfigurationSupport {
4445

4546
private final List<WebSocketMessageBrokerConfigurer> configurers = new ArrayList<>();

spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationSupport.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
* Configuration support for WebSocket request handling.
2828
*
2929
* @author Rossen Stoyanchev
30+
* @author Sebastien Deleuze
3031
* @since 4.0
3132
*/
3233
public class WebSocketConfigurationSupport {
@@ -39,10 +40,10 @@ public class WebSocketConfigurationSupport {
3940

4041

4142
@Bean
42-
public HandlerMapping webSocketHandlerMapping() {
43+
public HandlerMapping webSocketHandlerMapping(@Nullable TaskScheduler defaultSockJsTaskScheduler) {
4344
ServletWebSocketHandlerRegistry registry = initHandlerRegistry();
4445
if (registry.requiresTaskScheduler()) {
45-
TaskScheduler scheduler = defaultSockJsTaskScheduler();
46+
TaskScheduler scheduler = defaultSockJsTaskScheduler;
4647
Assert.notNull(scheduler, "Expected default TaskScheduler bean");
4748
registry.setTaskScheduler(scheduler);
4849
}

spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupport.java

+26-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,15 +19,19 @@
1919
import org.springframework.beans.factory.config.CustomScopeConfigurer;
2020
import org.springframework.context.ApplicationContext;
2121
import org.springframework.context.annotation.Bean;
22+
import org.springframework.core.task.TaskExecutor;
2223
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
2324
import org.springframework.lang.Nullable;
2425
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
26+
import org.springframework.messaging.simp.SimpMessagingTemplate;
2527
import org.springframework.messaging.simp.SimpSessionScope;
2628
import org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler;
2729
import org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler;
2830
import org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration;
2931
import org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler;
3032
import org.springframework.messaging.simp.user.SimpUserRegistry;
33+
import org.springframework.messaging.support.AbstractSubscribableChannel;
34+
import org.springframework.scheduling.TaskScheduler;
3135
import org.springframework.web.servlet.HandlerMapping;
3236
import org.springframework.web.socket.WebSocketHandler;
3337
import org.springframework.web.socket.config.WebSocketMessageBrokerStats;
@@ -46,6 +50,7 @@
4650
*
4751
* @author Rossen Stoyanchev
4852
* @author Artem Bilan
53+
* @author Sebastien Deleuze
4954
* @since 4.0
5055
*/
5156
public abstract class WebSocketMessageBrokerConfigurationSupport extends AbstractMessageBrokerConfiguration {
@@ -55,9 +60,10 @@ public abstract class WebSocketMessageBrokerConfigurationSupport extends Abstrac
5560

5661

5762
@Override
58-
protected SimpAnnotationMethodMessageHandler createAnnotationMethodMessageHandler() {
59-
return new WebSocketAnnotationMethodMessageHandler(
60-
clientInboundChannel(), clientOutboundChannel(), brokerMessagingTemplate());
63+
protected SimpAnnotationMethodMessageHandler createAnnotationMethodMessageHandler(
64+
AbstractSubscribableChannel clientInboundChannel, AbstractSubscribableChannel clientOutboundChannel,
65+
SimpMessagingTemplate brokerMessagingTemplate) {
66+
return new WebSocketAnnotationMethodMessageHandler(clientInboundChannel, clientOutboundChannel, brokerMessagingTemplate);
6167
}
6268

6369
@Override
@@ -70,10 +76,11 @@ protected SimpUserRegistry createLocalUserRegistry(@Nullable Integer order) {
7076
}
7177

7278
@Bean
73-
public HandlerMapping stompWebSocketHandlerMapping() {
74-
WebSocketHandler handler = decorateWebSocketHandler(subProtocolWebSocketHandler());
79+
public HandlerMapping stompWebSocketHandlerMapping(WebSocketHandler subProtocolWebSocketHandler,
80+
TaskScheduler messageBrokerTaskScheduler) {
81+
WebSocketHandler handler = decorateWebSocketHandler(subProtocolWebSocketHandler);
7582
WebMvcStompEndpointRegistry registry = new WebMvcStompEndpointRegistry(
76-
handler, getTransportRegistration(), messageBrokerTaskScheduler());
83+
handler, getTransportRegistration(), messageBrokerTaskScheduler);
7784
ApplicationContext applicationContext = getApplicationContext();
7885
if (applicationContext != null) {
7986
registry.setApplicationContext(applicationContext);
@@ -83,8 +90,9 @@ public HandlerMapping stompWebSocketHandlerMapping() {
8390
}
8491

8592
@Bean
86-
public WebSocketHandler subProtocolWebSocketHandler() {
87-
return new SubProtocolWebSocketHandler(clientInboundChannel(), clientOutboundChannel());
93+
public WebSocketHandler subProtocolWebSocketHandler(AbstractSubscribableChannel clientInboundChannel,
94+
AbstractSubscribableChannel clientOutboundChannel) {
95+
return new SubProtocolWebSocketHandler(clientInboundChannel, clientOutboundChannel);
8896
}
8997

9098
protected WebSocketHandler decorateWebSocketHandler(WebSocketHandler handler) {
@@ -115,20 +123,17 @@ public static CustomScopeConfigurer webSocketScopeConfigurer() {
115123
}
116124

117125
@Bean
118-
public WebSocketMessageBrokerStats webSocketMessageBrokerStats() {
119-
AbstractBrokerMessageHandler relayBean = stompBrokerRelayMessageHandler();
120-
121-
// Ensure STOMP endpoints are registered
122-
stompWebSocketHandlerMapping();
123-
126+
public WebSocketMessageBrokerStats webSocketMessageBrokerStats(@Nullable AbstractBrokerMessageHandler stompBrokerRelayMessageHandler,
127+
WebSocketHandler subProtocolWebSocketHandler, TaskExecutor clientInboundChannelExecutor, TaskExecutor clientOutboundChannelExecutor,
128+
TaskScheduler messageBrokerTaskScheduler) {
124129
WebSocketMessageBrokerStats stats = new WebSocketMessageBrokerStats();
125-
stats.setSubProtocolWebSocketHandler((SubProtocolWebSocketHandler) subProtocolWebSocketHandler());
126-
if (relayBean instanceof StompBrokerRelayMessageHandler) {
127-
stats.setStompBrokerRelay((StompBrokerRelayMessageHandler) relayBean);
130+
stats.setSubProtocolWebSocketHandler((SubProtocolWebSocketHandler) subProtocolWebSocketHandler);
131+
if (stompBrokerRelayMessageHandler instanceof StompBrokerRelayMessageHandler) {
132+
stats.setStompBrokerRelay((StompBrokerRelayMessageHandler) stompBrokerRelayMessageHandler);
128133
}
129-
stats.setInboundChannelExecutor(clientInboundChannelExecutor());
130-
stats.setOutboundChannelExecutor(clientOutboundChannelExecutor());
131-
stats.setSockJsTaskScheduler(messageBrokerTaskScheduler());
134+
stats.setInboundChannelExecutor(clientInboundChannelExecutor);
135+
stats.setOutboundChannelExecutor(clientOutboundChannelExecutor);
136+
stats.setSockJsTaskScheduler(messageBrokerTaskScheduler);
132137
return stats;
133138
}
134139

spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupportTests.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2929
import org.springframework.context.annotation.Bean;
3030
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.core.task.TaskExecutor;
3132
import org.springframework.messaging.Message;
3233
import org.springframework.messaging.MessageHandler;
3334
import org.springframework.messaging.handler.annotation.MessageMapping;
@@ -67,6 +68,7 @@
6768
* Test fixture for {@link WebSocketMessageBrokerConfigurationSupport}.
6869
*
6970
* @author Rossen Stoyanchev
71+
* @author Sebastien Deleuze
7072
*/
7173
public class WebSocketMessageBrokerConfigurationSupportTests {
7274

@@ -251,24 +253,25 @@ static class TestChannelConfig extends DelegatingWebSocketMessageBrokerConfigura
251253

252254
@Override
253255
@Bean
254-
public AbstractSubscribableChannel clientInboundChannel() {
256+
public AbstractSubscribableChannel clientInboundChannel(TaskExecutor clientInboundChannelExecutor) {
255257
TestChannel channel = new TestChannel();
256-
channel.setInterceptors(super.clientInboundChannel().getInterceptors());
258+
channel.setInterceptors(super.clientInboundChannel(clientInboundChannelExecutor).getInterceptors());
257259
return channel;
258260
}
259261

260262
@Override
261263
@Bean
262-
public AbstractSubscribableChannel clientOutboundChannel() {
264+
public AbstractSubscribableChannel clientOutboundChannel(TaskExecutor clientOutboundChannelExecutor) {
263265
TestChannel channel = new TestChannel();
264-
channel.setInterceptors(super.clientOutboundChannel().getInterceptors());
266+
channel.setInterceptors(super.clientOutboundChannel(clientOutboundChannelExecutor).getInterceptors());
265267
return channel;
266268
}
267269

268270
@Override
269-
public AbstractSubscribableChannel brokerChannel() {
271+
public AbstractSubscribableChannel brokerChannel(AbstractSubscribableChannel clientInboundChannel,
272+
AbstractSubscribableChannel clientOutboundChannel, TaskExecutor brokerChannelExecutor) {
270273
TestChannel channel = new TestChannel();
271-
channel.setInterceptors(super.brokerChannel().getInterceptors());
274+
channel.setInterceptors(super.brokerChannel(clientInboundChannel, clientOutboundChannel, brokerChannelExecutor).getInterceptors());
272275
return channel;
273276
}
274277
}

spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompWebSocketIntegrationTests.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.context.annotation.Configuration;
3232
import org.springframework.context.annotation.Scope;
3333
import org.springframework.context.annotation.ScopedProxyMode;
34+
import org.springframework.core.task.TaskExecutor;
3435
import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
3536
import org.springframework.messaging.handler.annotation.MessageMapping;
3637
import org.springframework.messaging.simp.annotation.SendToUser;
@@ -59,6 +60,7 @@
5960
*
6061
* @author Rossen Stoyanchev
6162
* @author Sam Brannen
63+
* @author Sebastien Deleuze
6264
*/
6365
class StompWebSocketIntegrationTests extends AbstractWebSocketIntegrationTests {
6466

@@ -331,13 +333,13 @@ static class TestMessageBrokerConfiguration extends DelegatingWebSocketMessageBr
331333

332334
@Override
333335
@Bean
334-
public AbstractSubscribableChannel clientInboundChannel() {
336+
public AbstractSubscribableChannel clientInboundChannel(TaskExecutor clientInboundChannelExecutor) {
335337
return new ExecutorSubscribableChannel(); // synchronous
336338
}
337339

338340
@Override
339341
@Bean
340-
public AbstractSubscribableChannel clientOutboundChannel() {
342+
public AbstractSubscribableChannel clientOutboundChannel(TaskExecutor clientOutboundChannelExecutor) {
341343
return new ExecutorSubscribableChannel(); // synchronous
342344
}
343345
}

0 commit comments

Comments
 (0)