Skip to content

Commit 30fbd67

Browse files
committed
Merge pull request #26341 from nguyensach
* pr/26341: Polish "Apply RSocketConnectorConfigurer beans to RSocketRequester.Builder" Apply RSocketConnectorConfigurer beans to RSocketRequester.Builder Closes gh-26341
2 parents 7879adf + 6b4efcc commit 30fbd67

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketRequesterAutoConfiguration.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 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,14 +19,17 @@
1919
import io.rsocket.transport.netty.server.TcpServerTransport;
2020
import reactor.netty.http.server.HttpServer;
2121

22+
import org.springframework.beans.factory.ObjectProvider;
2223
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2324
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2425
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2526
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2627
import org.springframework.context.annotation.Bean;
2728
import org.springframework.context.annotation.Configuration;
2829
import org.springframework.context.annotation.Scope;
30+
import org.springframework.messaging.rsocket.RSocketConnectorConfigurer;
2931
import org.springframework.messaging.rsocket.RSocketRequester;
32+
import org.springframework.messaging.rsocket.RSocketRequester.Builder;
3033
import org.springframework.messaging.rsocket.RSocketStrategies;
3134

3235
/**
@@ -47,8 +50,11 @@ public class RSocketRequesterAutoConfiguration {
4750
@Bean
4851
@Scope("prototype")
4952
@ConditionalOnMissingBean
50-
public RSocketRequester.Builder rSocketRequesterBuilder(RSocketStrategies strategies) {
51-
return RSocketRequester.builder().rsocketStrategies(strategies);
53+
public RSocketRequester.Builder rSocketRequesterBuilder(RSocketStrategies strategies,
54+
ObjectProvider<RSocketConnectorConfigurer> connectorConfigurers) {
55+
Builder builder = RSocketRequester.builder().rsocketStrategies(strategies);
56+
connectorConfigurers.orderedStream().forEach(builder::rsocketConnector);
57+
return builder;
5258
}
5359

5460
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketRequesterAutoConfigurationTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -16,21 +16,25 @@
1616

1717
package org.springframework.boot.autoconfigure.rsocket;
1818

19+
import org.assertj.core.api.InstanceOfAssertFactories;
1920
import org.junit.jupiter.api.Test;
2021

2122
import org.springframework.boot.autoconfigure.AutoConfigurations;
2223
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2324
import org.springframework.context.annotation.Bean;
2425
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.messaging.rsocket.RSocketConnectorConfigurer;
2527
import org.springframework.messaging.rsocket.RSocketRequester;
2628

29+
import static org.assertj.core.api.Assertions.as;
2730
import static org.assertj.core.api.Assertions.assertThat;
2831
import static org.mockito.Mockito.mock;
2932

3033
/**
3134
* Tests for {@link RSocketRequesterAutoConfiguration}
3235
*
3336
* @author Brian Clozel
37+
* @author Nguyen Bao Sach
3438
*/
3539
class RSocketRequesterAutoConfigurationTests {
3640

@@ -59,6 +63,19 @@ void shouldNotCreateBuilderIfAlreadyPresent() {
5963
});
6064
}
6165

66+
@Test
67+
void shouldCreateBuilderWithAvailableRSocketConnectorConfigurers() {
68+
RSocketConnectorConfigurer first = mock(RSocketConnectorConfigurer.class);
69+
RSocketConnectorConfigurer second = mock(RSocketConnectorConfigurer.class);
70+
this.contextRunner.withBean("first", RSocketConnectorConfigurer.class, () -> first)
71+
.withBean("second", RSocketConnectorConfigurer.class, () -> second).run((context) -> {
72+
assertThat(context).getBeans(RSocketConnectorConfigurer.class).hasSize(2);
73+
RSocketRequester.Builder builder = context.getBean(RSocketRequester.Builder.class);
74+
assertThat(builder).extracting("rsocketConnectorConfigurers", as(InstanceOfAssertFactories.LIST))
75+
.containsExactly(first, second);
76+
});
77+
}
78+
6279
@Configuration(proxyBeanMethods = false)
6380
static class CustomRSocketRequesterBuilder {
6481

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/rsocket.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Once the `RSocket` channel is established between server and client, any party c
7373

7474
As a server, you can get injected with an `RSocketRequester` instance on any handler method of an RSocket `@Controller`.
7575
As a client, you need to configure and establish an RSocket connection first.
76-
Spring Boot auto-configures an `RSocketRequester.Builder` for such cases with the expected codecs.
76+
Spring Boot auto-configures an `RSocketRequester.Builder` for such cases with the expected codecs and apply any `RSocketConnectorConfigurer` bean.
7777

7878
The `RSocketRequester.Builder` instance is a prototype bean, meaning each injection point will provide you with a new instance .
7979
This is done on purpose since this builder is stateful and you shouldn't create requesters with different setups using the same instance.

0 commit comments

Comments
 (0)