Skip to content

Commit 1c0bcba

Browse files
committed
Add HTTP Service registrar tests
Closes gh-33992
1 parent 42409e2 commit 1c0bcba

File tree

10 files changed

+435
-51
lines changed

10 files changed

+435
-51
lines changed

spring-web/src/main/java/org/springframework/web/service/registry/AbstractHttpServiceRegistrar.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.core.type.filter.AnnotationTypeFilter;
4747
import org.springframework.util.Assert;
4848
import org.springframework.util.ClassUtils;
49+
import org.springframework.util.StringUtils;
4950
import org.springframework.web.service.annotation.HttpExchange;
5051

5152
/**
@@ -123,12 +124,17 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
123124

124125
@Override
125126
public final void registerBeanDefinitions(
126-
AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry beanRegistry,
127-
BeanNameGenerator beanNameGenerator) {
127+
AnnotationMetadata metadata, BeanDefinitionRegistry registry, BeanNameGenerator generator) {
128128

129-
registerHttpServices(new DefaultGroupRegistry(), importingClassMetadata);
129+
registerBeanDefinitions(metadata, registry);
130+
}
131+
132+
@Override
133+
public final void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry beanRegistry) {
134+
135+
registerHttpServices(new DefaultGroupRegistry(), metadata);
130136

131-
String proxyRegistryBeanName = HttpServiceProxyRegistry.class.getName();
137+
String proxyRegistryBeanName = StringUtils.uncapitalize(HttpServiceProxyRegistry.class.getSimpleName());
132138
GenericBeanDefinition proxyRegistryBeanDef;
133139

134140
if (!beanRegistry.containsBeanDefinition(proxyRegistryBeanName)) {
@@ -142,23 +148,19 @@ public final void registerBeanDefinitions(
142148
proxyRegistryBeanDef = (GenericBeanDefinition) beanRegistry.getBeanDefinition(proxyRegistryBeanName);
143149
}
144150

145-
mergeHttpServices(proxyRegistryBeanDef);
151+
mergeGroups(proxyRegistryBeanDef);
146152

147153
this.groupMap.forEach((groupName, group) -> group.httpServiceTypeNames().forEach(type -> {
148154
GenericBeanDefinition proxyBeanDef = new GenericBeanDefinition();
149155
proxyBeanDef.setBeanClassName(type);
150-
String beanName = (groupName + "." + beanNameGenerator.generateBeanName(proxyBeanDef, beanRegistry));
156+
String beanName = (groupName + "#" + type);
151157
proxyBeanDef.setInstanceSupplier(() -> getProxyInstance(proxyRegistryBeanName, groupName, type));
152158
if (!beanRegistry.containsBeanDefinition(beanName)) {
153159
beanRegistry.registerBeanDefinition(beanName, proxyBeanDef);
154160
}
155161
}));
156162
}
157163

158-
@Override
159-
public final void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
160-
}
161-
162164
/**
163165
* This method is called before any bean definition registrations are made.
164166
* Subclasses must implement it to register the HTTP Services for which bean
@@ -181,7 +183,7 @@ private ClassPathScanningCandidateComponentProvider getScanner() {
181183
}
182184

183185
@SuppressWarnings("unchecked")
184-
private void mergeHttpServices(GenericBeanDefinition proxyRegistryBeanDef) {
186+
private void mergeGroups(GenericBeanDefinition proxyRegistryBeanDef) {
185187
ConstructorArgumentValues args = proxyRegistryBeanDef.getConstructorArgumentValues();
186188
ConstructorArgumentValues.ValueHolder valueHolder = args.getArgumentValue(0, Map.class);
187189
Assert.state(valueHolder != null, "Expected Map constructor argument at index 0");
@@ -233,7 +235,9 @@ protected interface GroupRegistry {
233235
/**
234236
* Perform HTTP Service registrations for the given group.
235237
*/
236-
GroupSpec forGroup(String name);
238+
default GroupSpec forGroup(String name) {
239+
return forGroup(name, HttpServiceGroup.ClientType.UNSPECIFIED);
240+
}
237241

238242
/**
239243
* Variant of {@link #forGroup(String)} with a client type.
@@ -278,11 +282,6 @@ interface GroupSpec {
278282
*/
279283
private class DefaultGroupRegistry implements GroupRegistry {
280284

281-
@Override
282-
public GroupSpec forGroup(String name) {
283-
return forGroup(name, HttpServiceGroup.ClientType.UNSPECIFIED);
284-
}
285-
286285
@Override
287286
public GroupSpec forGroup(String name, HttpServiceGroup.ClientType clientType) {
288287
return new DefaultGroupSpec(name, clientType);
@@ -313,36 +312,36 @@ else if (defaultClientType != HttpServiceGroup.ClientType.UNSPECIFIED) {
313312

314313
@Override
315314
public GroupSpec register(Class<?>... serviceTypes) {
316-
getOrCreateGroup(groupName, clientType).addHttpServiceTypes(serviceTypes);
315+
getOrCreateGroup().addHttpServiceTypes(serviceTypes);
317316
return this;
318317
}
319318

320319
@Override
321320
public GroupSpec detectInBasePackages(Class<?>... packageClasses) {
322321
for (Class<?> packageClass : packageClasses) {
323-
detect(this.groupName, this.clientType, packageClass.getPackageName());
322+
detect(packageClass.getPackageName());
324323
}
325324
return this;
326325
}
327326

328327
@Override
329328
public GroupSpec detectInBasePackages(String... packageNames) {
330329
for (String packageName : packageNames) {
331-
detect(this.groupName, this.clientType, packageName);
330+
detect(packageName);
332331
}
333332
return this;
334333
}
335334

336-
private void detect(String groupName, HttpServiceGroup.ClientType clientType, String packageName) {
335+
private void detect(String packageName) {
337336
for (BeanDefinition definition : getScanner().findCandidateComponents(packageName)) {
338337
if (definition.getBeanClassName() != null) {
339-
getOrCreateGroup(groupName, clientType).addHttpServiceTypeName(definition.getBeanClassName());
338+
getOrCreateGroup().addHttpServiceTypeName(definition.getBeanClassName());
340339
}
341340
}
342341
}
343342

344-
private RegisteredGroup getOrCreateGroup(String groupName, HttpServiceGroup.ClientType clientType) {
345-
return groupMap.computeIfAbsent(groupName, name -> new RegisteredGroup(name, clientType));
343+
private RegisteredGroup getOrCreateGroup() {
344+
return groupMap.computeIfAbsent(this.groupName, name -> new RegisteredGroup(name, this.clientType));
346345
}
347346
}
348347
}
@@ -376,7 +375,7 @@ public Set<String> httpServiceTypeNames() {
376375

377376
@Override
378377
public Set<Class<?>> httpServiceTypes() {
379-
return httpServiceTypeNames.stream()
378+
return this.httpServiceTypeNames.stream()
380379
.map(AbstractHttpServiceRegistrar::loadClass)
381380
.collect(Collectors.toSet());
382381
}

spring-web/src/main/java/org/springframework/web/service/registry/AnnotationHttpServiceRegistrar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* @author Rossen Stoyanchev
2828
* @since 7.0
2929
*/
30-
final class AnnotationHttpServiceRegistrar extends AbstractHttpServiceRegistrar {
30+
class AnnotationHttpServiceRegistrar extends AbstractHttpServiceRegistrar {
3131

3232
@Override
3333
protected void registerHttpServices(GroupRegistry registry, AnnotationMetadata importMetadata) {

spring-web/src/main/java/org/springframework/web/service/registry/HttpServiceProxyRegistryFactoryBean.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ public void afterPropertiesSet() {
9696
Assert.notNull(this.applicationContext, "ApplicationContext not initialized");
9797

9898
// Apply group configurers
99-
this.groupSet.forEach(group ->
100-
this.applicationContext.getBeanProvider(group.getConfigurerType())
101-
.orderedStream()
102-
.forEach(configurer -> configurer.configureGroups(new DefaultGroups<>(group.clientType()))));
99+
groupAdapters.forEach((clientType, groupAdapter) ->
100+
this.applicationContext.getBeanProvider(groupAdapter.getConfigurerType())
101+
.orderedStream()
102+
.forEach(configurer -> configurer.configureGroups(new DefaultGroups<>(clientType))));
103103

104104
// Create proxies
105105
Map<String, Map<Class<?>, Object>> groupProxyMap = this.groupSet.stream()
@@ -193,13 +193,13 @@ public <CB> void apply(
193193
}
194194

195195
public Map<Class<?>, Object> createProxies() {
196-
Map<Class<?>, Object> proxyMap = new LinkedHashMap<>(httpServiceTypes().size());
196+
Map<Class<?>, Object> map = new LinkedHashMap<>(httpServiceTypes().size());
197197
HttpExchangeAdapter exchangeAdapter = initExchangeAdapter();
198198
HttpServiceProxyFactory.Builder proxyFactoryBuilder = HttpServiceProxyFactory.builderFor(exchangeAdapter);
199199
this.proxyFactoryConfigurer.accept(this, proxyFactoryBuilder);
200-
HttpServiceProxyFactory proxyFactory = proxyFactoryBuilder.build();
201-
httpServiceTypes().forEach(type -> proxyMap.put(type, proxyFactory.createClient(type)));
202-
return proxyMap;
200+
HttpServiceProxyFactory factory = proxyFactoryBuilder.build();
201+
httpServiceTypes().forEach(type -> map.put(type, factory.createClient(type)));
202+
return map;
203203
}
204204

205205
@SuppressWarnings("unchecked")
@@ -219,12 +219,10 @@ public String toString() {
219219
*/
220220
private final class DefaultGroups<CB> implements HttpServiceGroupConfigurer.Groups<CB> {
221221

222-
private final HttpServiceGroup.ClientType clientType;
223-
224-
private @Nullable Predicate<HttpServiceGroup> filter;
222+
private Predicate<HttpServiceGroup> filter;
225223

226224
DefaultGroups(HttpServiceGroup.ClientType clientType) {
227-
this.clientType = clientType;
225+
this.filter = group -> group.clientType().equals(clientType);
228226
}
229227

230228
@Override
@@ -234,7 +232,7 @@ public HttpServiceGroupConfigurer.Groups<CB> filterByName(String... groupNames)
234232

235233
@Override
236234
public HttpServiceGroupConfigurer.Groups<CB> filter(Predicate<HttpServiceGroup> predicate) {
237-
this.filter = (this.filter != null ? this.filter.or(predicate) : predicate);
235+
this.filter = this.filter.or(predicate);
238236
return this;
239237
}
240238

@@ -260,10 +258,8 @@ public void configure(
260258
BiConsumer<HttpServiceGroup, CB> clientConfigurer,
261259
BiConsumer<HttpServiceGroup, HttpServiceProxyFactory.Builder> proxyFactoryConfigurer) {
262260

263-
groupSet.stream()
264-
.filter(group -> group.clientType().equals(this.clientType))
265-
.filter(groups -> this.filter == null || this.filter.test(groups))
266-
.forEach(group -> group.apply(clientConfigurer, proxyFactoryConfigurer));
261+
groupSet.stream().filter(this.filter).forEach(group ->
262+
group.apply(clientConfigurer, proxyFactoryConfigurer));
267263
}
268264
}
269265

spring-web/src/test/java/org/springframework/web/client/support/RestClientProxyRegistryIntegrationTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
import org.springframework.context.annotation.Configuration;
3232
import org.springframework.context.annotation.Import;
3333
import org.springframework.core.type.AnnotationMetadata;
34-
import org.springframework.web.client.support.echo.EchoA;
35-
import org.springframework.web.client.support.echo.EchoB;
36-
import org.springframework.web.client.support.greeting.GreetingA;
37-
import org.springframework.web.client.support.greeting.GreetingB;
3834
import org.springframework.web.service.registry.AbstractHttpServiceRegistrar;
3935
import org.springframework.web.service.registry.HttpServiceProxyRegistry;
4036
import org.springframework.web.service.registry.ImportHttpServices;
37+
import org.springframework.web.service.registry.echo.EchoA;
38+
import org.springframework.web.service.registry.echo.EchoB;
39+
import org.springframework.web.service.registry.greeting.GreetingA;
40+
import org.springframework.web.service.registry.greeting.GreetingB;
4141

4242
import static org.assertj.core.api.Assertions.assertThat;
4343

0 commit comments

Comments
 (0)