Skip to content

Commit 06f0b91

Browse files
committed
Add @order to WebSocketMessageConverterConfiguration
Add `@Order` to `WebSocketMessageConverterConfiguration` so that custom `WebSocketMessageBrokerConfigurer` implementations can be added before or after ours. Fixes gh-42924
1 parent c2ab2dd commit 06f0b91

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketMessagingAutoConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
3333
import org.springframework.context.annotation.Bean;
3434
import org.springframework.context.annotation.Configuration;
35+
import org.springframework.core.annotation.Order;
3536
import org.springframework.core.task.AsyncTaskExecutor;
3637
import org.springframework.messaging.converter.ByteArrayMessageConverter;
3738
import org.springframework.messaging.converter.DefaultContentTypeResolver;
@@ -60,6 +61,7 @@ public class WebSocketMessagingAutoConfiguration {
6061
@Configuration(proxyBeanMethods = false)
6162
@ConditionalOnBean({ DelegatingWebSocketMessageBrokerConfiguration.class, ObjectMapper.class })
6263
@ConditionalOnClass({ ObjectMapper.class, AbstractMessageBrokerConfiguration.class })
64+
@Order(0)
6365
static class WebSocketMessageConverterConfiguration implements WebSocketMessageBrokerConfigurer {
6466

6567
private final ObjectMapper objectMapper;

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketMessagingAutoConfigurationTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import com.fasterxml.jackson.databind.ObjectMapper;
3131
import org.apache.tomcat.websocket.WsWebSocketContainer;
32+
import org.assertj.core.api.InstanceOfAssertFactories;
3233
import org.junit.jupiter.api.AfterEach;
3334
import org.junit.jupiter.api.BeforeEach;
3435
import org.junit.jupiter.api.Test;
@@ -40,12 +41,15 @@
4041
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
4142
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
4243
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
44+
import org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration.WebSocketMessageConverterConfiguration;
4345
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4446
import org.springframework.boot.test.util.TestPropertyValues;
4547
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
4648
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
4749
import org.springframework.context.annotation.Bean;
4850
import org.springframework.context.annotation.Configuration;
51+
import org.springframework.core.Ordered;
52+
import org.springframework.core.annotation.Order;
4953
import org.springframework.core.task.AsyncTaskExecutor;
5054
import org.springframework.core.task.SimpleAsyncTaskExecutor;
5155
import org.springframework.core.task.TaskExecutor;
@@ -62,6 +66,7 @@
6266
import org.springframework.messaging.simp.stomp.StompSessionHandler;
6367
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
6468
import org.springframework.security.util.FieldUtils;
69+
import org.springframework.stereotype.Component;
6570
import org.springframework.stereotype.Controller;
6671
import org.springframework.web.client.RestTemplate;
6772
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
@@ -162,6 +167,26 @@ void predefinedThreadExecutorIsSelectedForOutboundChannel() throws Throwable {
162167
assertThat(executor).isEqualTo(expectedExecutor);
163168
}
164169

170+
@Test
171+
void webSocketMessageBrokerConfigurerOrdering() throws Throwable {
172+
TestPropertyValues.of("server.port:0", "spring.jackson.serialization.indent-output:true").applyTo(this.context);
173+
this.context.register(WebSocketMessagingConfiguration.class, CustomLowWebSocketMessageBrokerConfigurer.class,
174+
CustomHighWebSocketMessageBrokerConfigurer.class);
175+
this.context.refresh();
176+
DelegatingWebSocketMessageBrokerConfiguration delegatingConfiguration = this.context
177+
.getBean(DelegatingWebSocketMessageBrokerConfiguration.class);
178+
CustomHighWebSocketMessageBrokerConfigurer high = this.context
179+
.getBean(CustomHighWebSocketMessageBrokerConfigurer.class);
180+
WebSocketMessageConverterConfiguration autoConfiguration = this.context
181+
.getBean(WebSocketMessagingAutoConfiguration.WebSocketMessageConverterConfiguration.class);
182+
WebSocketMessagingConfiguration configuration = this.context.getBean(WebSocketMessagingConfiguration.class);
183+
CustomLowWebSocketMessageBrokerConfigurer low = this.context
184+
.getBean(CustomLowWebSocketMessageBrokerConfigurer.class);
185+
assertThat(delegatingConfiguration).extracting("configurers")
186+
.asInstanceOf(InstanceOfAssertFactories.LIST)
187+
.containsExactly(high, autoConfiguration, configuration, low);
188+
}
189+
165190
private List<MessageConverter> getCustomizedConverters() {
166191
List<MessageConverter> customizedConverters = new ArrayList<>();
167192
WebSocketMessagingAutoConfiguration.WebSocketMessageConverterConfiguration configuration = new WebSocketMessagingAutoConfiguration.WebSocketMessageConverterConfiguration(
@@ -246,6 +271,7 @@ static class WebSocketMessagingConfiguration implements WebSocketMessageBrokerCo
246271

247272
@Override
248273
public void registerStompEndpoints(StompEndpointRegistry registry) {
274+
249275
registry.addEndpoint("/messaging").withSockJS();
250276
}
251277

@@ -271,6 +297,18 @@ TomcatWebSocketServletWebServerCustomizer tomcatCustomizer() {
271297

272298
}
273299

300+
@Component
301+
@Order(Ordered.HIGHEST_PRECEDENCE)
302+
static class CustomHighWebSocketMessageBrokerConfigurer implements WebSocketMessageBrokerConfigurer {
303+
304+
}
305+
306+
@Component
307+
@Order(Ordered.LOWEST_PRECEDENCE)
308+
static class CustomLowWebSocketMessageBrokerConfigurer implements WebSocketMessageBrokerConfigurer {
309+
310+
}
311+
274312
@Controller
275313
static class MessagingController {
276314

0 commit comments

Comments
 (0)