0
# Auto-Configuration
1
2
Comprehensive auto-configuration for RSocket infrastructure, providing automatic setup of codecs, servers, clients, and messaging handlers based on classpath detection and configuration properties.
3
4
## Capabilities
5
6
### RSocket Strategies Auto-Configuration
7
8
Automatically configures RSocketStrategies bean providing encoding/decoding infrastructure for RSocket payloads.
9
10
```java { .api }
11
/**
12
* Auto-configures RSocketStrategies with CBOR and JSON codecs
13
*/
14
@AutoConfiguration(after = JacksonAutoConfiguration.class)
15
@ConditionalOnClass({ io.rsocket.RSocket.class, RSocketStrategies.class, PooledByteBufAllocator.class })
16
class RSocketStrategiesAutoConfiguration {
17
18
@Bean
19
@ConditionalOnMissingBean
20
RSocketStrategies rSocketStrategies(ObjectProvider<RSocketStrategiesCustomizer> customizers);
21
22
@Bean
23
@Order(0)
24
@ConditionalOnBean(Jackson2ObjectMapperBuilder.class)
25
RSocketStrategiesCustomizer jacksonCborRSocketStrategyCustomizer(Jackson2ObjectMapperBuilder builder);
26
27
@Bean
28
@Order(1)
29
@ConditionalOnBean(ObjectMapper.class)
30
RSocketStrategiesCustomizer jacksonJsonRSocketStrategyCustomizer(ObjectMapper objectMapper);
31
}
32
```
33
34
**Default Codec Order:**
35
1. CBOR codecs with Jackson (priority 0)
36
2. JSON codecs with Jackson (priority 1)
37
38
**Usage Example:**
39
40
```java
41
@Component
42
public class CustomCodecConfiguration {
43
44
@Bean
45
@Order(-1) // Higher priority than defaults
46
public RSocketStrategiesCustomizer customCodecCustomizer() {
47
return strategies -> {
48
strategies.encoder(new MyCustomEncoder());
49
strategies.decoder(new MyCustomDecoder());
50
};
51
}
52
}
53
```
54
55
### RSocket Server Auto-Configuration
56
57
Automatically configures RSocket server infrastructure supporting both standalone servers and WebFlux integration.
58
59
```java { .api }
60
/**
61
* Auto-configures RSocket server based on application type and properties
62
*/
63
@AutoConfiguration(after = RSocketStrategiesAutoConfiguration.class)
64
@ConditionalOnClass({ RSocketServer.class, RSocketStrategies.class, HttpServer.class, TcpServerTransport.class })
65
@ConditionalOnBean(RSocketMessageHandler.class)
66
@EnableConfigurationProperties(RSocketProperties.class)
67
class RSocketServerAutoConfiguration {
68
69
// WebFlux integration configuration
70
@Conditional(OnRSocketWebServerCondition.class)
71
@Configuration
72
static class WebFluxServerConfiguration {
73
74
@Bean
75
@ConditionalOnMissingBean
76
RSocketWebSocketNettyRouteProvider rSocketWebsocketRouteProvider(
77
RSocketProperties properties,
78
RSocketMessageHandler messageHandler,
79
ObjectProvider<RSocketServerCustomizer> customizers
80
);
81
}
82
83
// Standalone server configuration
84
@ConditionalOnProperty(prefix = "spring.rsocket.server", name = "port")
85
@Configuration
86
static class EmbeddedServerConfiguration {
87
88
@Bean
89
@ConditionalOnMissingBean
90
RSocketServerFactory rSocketServerFactory(
91
RSocketProperties properties,
92
ReactorResourceFactory resourceFactory,
93
ObjectProvider<RSocketServerCustomizer> customizers
94
);
95
96
@Bean
97
@ConditionalOnMissingBean
98
RSocketServerBootstrap rSocketServerBootstrap(
99
RSocketServerFactory rSocketServerFactory,
100
RSocketMessageHandler rSocketMessageHandler
101
);
102
103
@Bean
104
RSocketServerCustomizer frameDecoderRSocketServerCustomizer(
105
RSocketMessageHandler rSocketMessageHandler
106
);
107
}
108
}
109
```
110
111
**Server Configuration Conditions:**
112
113
- **WebSocket Integration**: Requires reactive web application, WebSocket transport, mapping path configured
114
- **Standalone Server**: Requires server port configuration
115
116
### RSocket Requester Auto-Configuration
117
118
Automatically configures RSocketRequester.Builder for client connections.
119
120
```java { .api }
121
/**
122
* Auto-configures prototype RSocketRequester.Builder beans
123
*/
124
@AutoConfiguration(after = RSocketStrategiesAutoConfiguration.class)
125
@ConditionalOnClass({ RSocketRequester.class, io.rsocket.RSocket.class, HttpServer.class, TcpServerTransport.class })
126
class RSocketRequesterAutoConfiguration {
127
128
@Bean
129
@Scope("prototype")
130
@ConditionalOnMissingBean
131
RSocketRequester.Builder rSocketRequesterBuilder(
132
RSocketStrategies strategies,
133
ObjectProvider<RSocketConnectorConfigurer> connectorConfigurers
134
);
135
}
136
```
137
138
**Usage Example:**
139
140
```java
141
@Service
142
public class RSocketClientService {
143
144
private final RSocketRequester.Builder requesterBuilder;
145
146
public RSocketClientService(RSocketRequester.Builder requesterBuilder) {
147
this.requesterBuilder = requesterBuilder; // New instance per injection
148
}
149
150
public Mono<String> makeRequest() {
151
return requesterBuilder
152
.tcp("localhost", 9898)
153
.route("greeting")
154
.data("Hello")
155
.retrieveMono(String.class);
156
}
157
}
158
```
159
160
### RSocket Messaging Auto-Configuration
161
162
Automatically configures Spring messaging support for RSocket with @MessageMapping annotations.
163
164
```java { .api }
165
/**
166
* Auto-configures RSocketMessageHandler for Spring messaging integration
167
*/
168
@AutoConfiguration(after = RSocketStrategiesAutoConfiguration.class)
169
@ConditionalOnClass({ RSocketRequester.class, io.rsocket.RSocket.class, TcpServerTransport.class })
170
class RSocketMessagingAutoConfiguration {
171
172
@Bean
173
@ConditionalOnMissingBean
174
RSocketMessageHandler messageHandler(
175
RSocketStrategies rSocketStrategies,
176
ObjectProvider<RSocketMessageHandlerCustomizer> customizers
177
);
178
}
179
```
180
181
**Usage Example:**
182
183
```java
184
@Controller
185
public class MessagingController {
186
187
@MessageMapping("user.{id}")
188
public Mono<User> getUser(@DestinationVariable String id) {
189
return userService.findById(id);
190
}
191
192
@MessageMapping("notifications")
193
public Flux<Notification> streamNotifications() {
194
return notificationService.stream();
195
}
196
}
197
```
198
199
## Configuration Conditions
200
201
The auto-configuration uses several conditional annotations to enable features:
202
203
### Class Conditions
204
- `@ConditionalOnClass`: Activates when specific classes are on classpath
205
- RSocket core classes (`io.rsocket.RSocket`, `RSocketServer`)
206
- Spring messaging classes (`RSocketRequester`, `RSocketStrategies`)
207
- Netty classes (`HttpServer`, `TcpServerTransport`)
208
209
### Bean Conditions
210
- `@ConditionalOnBean(RSocketMessageHandler.class)`: Server config requires message handler
211
- `@ConditionalOnMissingBean`: Provides default beans when none exist
212
213
### Property Conditions
214
- `@ConditionalOnProperty("spring.rsocket.server.port")`: Standalone server needs port
215
- `@ConditionalOnWebApplication(Type.REACTIVE)`: WebSocket integration for reactive apps
216
217
### Custom Conditions
218
- `OnRSocketWebServerCondition`: Complex condition for WebSocket server integration
219
220
## Customization Points
221
222
All auto-configuration supports customization through various customizer interfaces:
223
224
```java { .api }
225
// Customize RSocket strategies (codecs, routing)
226
@Component
227
class MyStrategiesCustomizer implements RSocketStrategiesCustomizer {
228
@Override
229
public void customize(RSocketStrategies.Builder strategies) {
230
strategies.encoder(myEncoder);
231
strategies.decoder(myDecoder);
232
}
233
}
234
235
// Customize message handler
236
@Component
237
class MyMessageHandlerCustomizer implements RSocketMessageHandlerCustomizer {
238
@Override
239
public void customize(RSocketMessageHandler messageHandler) {
240
messageHandler.setDefaultDataMimeType(MimeTypeUtils.APPLICATION_CBOR);
241
}
242
}
243
244
// Customize server configuration
245
@Component
246
class MyServerCustomizer implements RSocketServerCustomizer {
247
@Override
248
public void customize(RSocketServer server) {
249
server.payloadDecoder(PayloadDecoder.ZERO_COPY);
250
}
251
}
252
```