Skip to content

Commit 67f3ff5

Browse files
committed
Polishing in HTTP service registry
1 parent 42e854e commit 67f3ff5

File tree

5 files changed

+67
-61
lines changed

5 files changed

+67
-61
lines changed

spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceProxyFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ public <S> S createClient(Class<S> serviceType) {
9595
@SuppressWarnings("unchecked")
9696
private <S> S getProxy(Class<S> serviceType, List<HttpServiceMethod> httpServiceMethods) {
9797
MethodInterceptor interceptor = new HttpServiceMethodInterceptor(httpServiceMethods);
98-
ProxyFactory proxyFactory = new ProxyFactory(serviceType, interceptor);
99-
return (S) proxyFactory.getProxy(serviceType.getClassLoader());
98+
ProxyFactory factory = new ProxyFactory(serviceType, interceptor);
99+
return (S) factory.getProxy(serviceType.getClassLoader());
100100
}
101101

102102
private boolean isExchangeMethod(Method method) {

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

+39-32
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public final void registerBeanDefinitions(
144144
@Override
145145
public final void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry beanRegistry) {
146146

147-
registerHttpServices(DefaultGroupSpec::new, metadata);
147+
registerHttpServices(new DefaultGroupRegistry(), metadata);
148148

149149
RootBeanDefinition proxyRegistryBeanDef = createOrGetRegistry(beanRegistry);
150150

@@ -264,45 +264,52 @@ interface GroupSpec {
264264

265265

266266
/**
267-
* Default implementation of {@link GroupSpec}.
267+
* Default implementation of {@link GroupRegistry}.
268268
*/
269-
private class DefaultGroupSpec implements GroupRegistry.GroupSpec {
270-
271-
private final GroupsMetadata.Registration registration;
272-
273-
DefaultGroupSpec(String groupName, HttpServiceGroup.ClientType clientType) {
274-
clientType = (clientType != HttpServiceGroup.ClientType.UNSPECIFIED ? clientType : defaultClientType);
275-
this.registration = groupsMetadata.getOrCreateGroup(groupName, clientType);
276-
}
269+
private class DefaultGroupRegistry implements GroupRegistry {
277270

278271
@Override
279-
public GroupRegistry.GroupSpec register(Class<?>... serviceTypes) {
280-
Arrays.stream(serviceTypes).map(Class::getName).forEach(this::register);
281-
return this;
272+
public GroupSpec forGroup(String name, HttpServiceGroup.ClientType clientType) {
273+
return new DefaultGroupSpec(name, clientType);
282274
}
283275

284-
@Override
285-
public GroupRegistry.GroupSpec detectInBasePackages(Class<?>... packageClasses) {
286-
Arrays.stream(packageClasses).map(Class::getPackageName).forEach(this::detectInBasePackage);
287-
return this;
288-
}
276+
private class DefaultGroupSpec implements GroupSpec {
289277

290-
@Override
291-
public GroupRegistry.GroupSpec detectInBasePackages(String... packageNames) {
292-
Arrays.stream(packageNames).forEach(this::detectInBasePackage);
293-
return this;
294-
}
278+
private final GroupsMetadata.Registration registration;
295279

296-
private void detectInBasePackage(String packageName) {
297-
getScanner().findCandidateComponents(packageName)
298-
.stream()
299-
.map(BeanDefinition::getBeanClassName)
300-
.filter(Objects::nonNull)
301-
.forEach(this::register);
302-
}
280+
DefaultGroupSpec(String groupName, HttpServiceGroup.ClientType clientType) {
281+
clientType = (clientType != HttpServiceGroup.ClientType.UNSPECIFIED ? clientType : defaultClientType);
282+
this.registration = groupsMetadata.getOrCreateGroup(groupName, clientType);
283+
}
303284

304-
private void register(String httpServiceTypeName) {
305-
this.registration.httpServiceTypeNames().add(httpServiceTypeName);
285+
@Override
286+
public GroupRegistry.GroupSpec register(Class<?>... serviceTypes) {
287+
Arrays.stream(serviceTypes).map(Class::getName).forEach(this::registerServiceTypeName);
288+
return this;
289+
}
290+
291+
@Override
292+
public GroupRegistry.GroupSpec detectInBasePackages(Class<?>... packageClasses) {
293+
Arrays.stream(packageClasses).map(Class::getPackageName).forEach(this::detectInBasePackage);
294+
return this;
295+
}
296+
297+
@Override
298+
public GroupRegistry.GroupSpec detectInBasePackages(String... packageNames) {
299+
Arrays.stream(packageNames).forEach(this::detectInBasePackage);
300+
return this;
301+
}
302+
303+
private void detectInBasePackage(String packageName) {
304+
getScanner().findCandidateComponents(packageName).stream()
305+
.map(BeanDefinition::getBeanClassName)
306+
.filter(Objects::nonNull)
307+
.forEach(this::registerServiceTypeName);
308+
}
309+
310+
private void registerServiceTypeName(String httpServiceTypeName) {
311+
this.registration.httpServiceTypeNames().add(httpServiceTypeName);
312+
}
306313
}
307314
}
308315

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
/**
3535
* Container for HTTP Service type registrations, initially storing HTTP Service
3636
* type names as {@link Registration}s, and later exposing access to those
37-
* registrations as {@link HttpServiceGroup}s via {@link #groups()}.
37+
* registrations as {@link HttpServiceGroup}s via {@link #groups(ClassLoader)}.
3838
*
3939
* @author Rossen Stoyanchev
4040
* @since 7.0
@@ -85,14 +85,13 @@ public void forEachRegistration(BiConsumer<String, Set<String>> consumer) {
8585
* Create the {@link HttpServiceGroup}s for all registrations.
8686
*/
8787
public Collection<HttpServiceGroup> groups(@Nullable ClassLoader classLoader) {
88-
return this.groupMap.values()
89-
.stream()
88+
return this.groupMap.values().stream()
9089
.map(registration -> registration.toHttpServiceGroup(classLoader))
9190
.toList();
9291
}
9392

9493
/**
95-
* Return the raw {@link DefaultRegistration registrations}.
94+
* Return the raw {@link Registration registrations}.
9695
*/
9796
Stream<Registration> registrations() {
9897
return this.groupMap.values().stream();
@@ -146,11 +145,15 @@ public Registration clientType(HttpServiceGroup.ClientType other) {
146145
* Create the {@link HttpServiceGroup} from the metadata.
147146
*/
148147
public HttpServiceGroup toHttpServiceGroup(@Nullable ClassLoader classLoader) {
149-
return new RegisteredGroup(this.name,
150-
(this.clientType.isUnspecified() ? HttpServiceGroup.ClientType.REST_CLIENT : this.clientType),
151-
this.httpServiceTypeNames.stream()
152-
.map(typeName -> ClassUtils.resolveClassName(typeName, classLoader))
153-
.collect(Collectors.toSet()));
148+
149+
HttpServiceGroup.ClientType clientTypeToUse =
150+
(this.clientType.isUnspecified() ? HttpServiceGroup.ClientType.REST_CLIENT : this.clientType);
151+
152+
Set<Class<?>> httpServiceTypes = this.httpServiceTypeNames.stream()
153+
.map(typeName -> ClassUtils.resolveClassName(typeName, classLoader))
154+
.collect(Collectors.toSet());
155+
156+
return new RegisteredGroup(this.name, clientTypeToUse, httpServiceTypes);
154157
}
155158

156159
@Override

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

+10-15
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@ public void afterPropertiesSet() {
101101
Assert.notNull(this.beanClassLoader, "BeanClassLoader not initialized");
102102

103103
// Create the groups from the metadata
104-
Set<ProxyHttpServiceGroup> groups = this.groupsMetadata.groups(this.beanClassLoader)
105-
.stream()
106-
.map(ProxyHttpServiceGroup::new)
107-
.collect(Collectors.toSet());
104+
Set<ProxyHttpServiceGroup> groups = this.groupsMetadata.groups(this.beanClassLoader).stream()
105+
.map(ProxyHttpServiceGroup::new)
106+
.collect(Collectors.toSet());
108107

109108
// Apply group configurers
110109
groupAdapters.forEach((clientType, groupAdapter) ->
@@ -170,15 +169,16 @@ private static final class ProxyHttpServiceGroup implements HttpServiceGroup {
170169
private BiConsumer<HttpServiceGroup, HttpServiceProxyFactory.Builder> proxyFactoryConfigurer = (group, builder) -> {};
171170

172171
ProxyHttpServiceGroup(HttpServiceGroup group) {
173-
this(group, getHttpServiceGroupAdapter(group.clientType()));
174-
}
175-
176-
ProxyHttpServiceGroup(HttpServiceGroup group, HttpServiceGroupAdapter<?> groupAdapter) {
177172
this.declaredGroup = group;
178-
this.groupAdapter = groupAdapter;
179-
this.clientBuilder = groupAdapter.createClientBuilder();
173+
this.groupAdapter = getGroupAdapter(group.clientType());
174+
this.clientBuilder = this.groupAdapter.createClientBuilder();
180175
}
181176

177+
private static HttpServiceGroupAdapter<?> getGroupAdapter(HttpServiceGroup.ClientType clientType) {
178+
HttpServiceGroupAdapter<?> adapter = groupAdapters.get(clientType);
179+
Assert.state(adapter != null, "No HttpServiceGroupAdapter for type " + clientType);
180+
return adapter;
181+
}
182182

183183
@Override
184184
public String name() {
@@ -224,11 +224,6 @@ public String toString() {
224224
return getClass().getSimpleName() + "[id=" + name() + "]";
225225
}
226226

227-
private static HttpServiceGroupAdapter<?> getHttpServiceGroupAdapter(HttpServiceGroup.ClientType clientType) {
228-
HttpServiceGroupAdapter<?> adapter = groupAdapters.get(clientType);
229-
Assert.state(adapter != null, "No HttpServiceGroupAdapter for type " + clientType);
230-
return adapter;
231-
}
232227
}
233228

234229

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,14 @@ protected boolean isEligibleForOverriding(String className) {
123123
return className.contains("EchoA");
124124
};
125125
};
126+
126127
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
127128
context.setClassLoader(beanClassLoader);
128129
context.register(ClassUtils.resolveClassName(ListingConfig.class.getName(), beanClassLoader));
129130
context.refresh();
130-
assertThat(context.getBean(ClassUtils.resolveClassName(EchoA.class.getName(), beanClassLoader))
131-
.getClass()
132-
.getClassLoader()).isSameAs(beanClassLoader);
131+
132+
Class<?> echoClass = ClassUtils.resolveClassName(EchoA.class.getName(), beanClassLoader);
133+
assertThat(context.getBean(echoClass).getClass().getClassLoader()).isSameAs(beanClassLoader);
133134
}
134135

135136
private static class ClientConfig {

0 commit comments

Comments
 (0)