Polish AbstractHttpServiceRegistrar.DefaultGroupSpec
Remove `DefaultGroupRegistry` since it's not really needed and polish `DefaultGroupSpec` a little.
This commit is contained in:
committed by
rstoyanchev
parent
d2246162c3
commit
42e854e5ff
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.web.service.registry;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
@@ -141,7 +144,7 @@ public abstract class AbstractHttpServiceRegistrar implements
|
||||
@Override
|
||||
public final void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry beanRegistry) {
|
||||
|
||||
registerHttpServices(new DefaultGroupRegistry(), metadata);
|
||||
registerHttpServices(DefaultGroupSpec::new, metadata);
|
||||
|
||||
RootBeanDefinition proxyRegistryBeanDef = createOrGetRegistry(beanRegistry);
|
||||
|
||||
@@ -261,58 +264,45 @@ public abstract class AbstractHttpServiceRegistrar implements
|
||||
|
||||
|
||||
/**
|
||||
* Default implementation of {@link GroupRegistry}.
|
||||
* Default implementation of {@link GroupSpec}.
|
||||
*/
|
||||
private class DefaultGroupRegistry implements GroupRegistry {
|
||||
private class DefaultGroupSpec implements GroupRegistry.GroupSpec {
|
||||
|
||||
@Override
|
||||
public GroupSpec forGroup(String name, HttpServiceGroup.ClientType clientType) {
|
||||
return new DefaultGroupSpec(name, clientType);
|
||||
private final GroupsMetadata.Registration registration;
|
||||
|
||||
DefaultGroupSpec(String groupName, HttpServiceGroup.ClientType clientType) {
|
||||
clientType = (clientType != HttpServiceGroup.ClientType.UNSPECIFIED ? clientType : defaultClientType);
|
||||
this.registration = groupsMetadata.getOrCreateGroup(groupName, clientType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Default implementation of {@link GroupSpec}.
|
||||
*/
|
||||
private class DefaultGroupSpec implements GroupSpec {
|
||||
@Override
|
||||
public GroupRegistry.GroupSpec register(Class<?>... serviceTypes) {
|
||||
Arrays.stream(serviceTypes).map(Class::getName).forEach(this::register);
|
||||
return this;
|
||||
}
|
||||
|
||||
private final GroupsMetadata.Registration registration;
|
||||
@Override
|
||||
public GroupRegistry.GroupSpec detectInBasePackages(Class<?>... packageClasses) {
|
||||
Arrays.stream(packageClasses).map(Class::getPackageName).forEach(this::detectInBasePackage);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DefaultGroupSpec(String groupName, HttpServiceGroup.ClientType clientType) {
|
||||
clientType = (clientType != HttpServiceGroup.ClientType.UNSPECIFIED ? clientType : defaultClientType);
|
||||
this.registration = groupsMetadata.getOrCreateGroup(groupName, clientType);
|
||||
}
|
||||
@Override
|
||||
public GroupRegistry.GroupSpec detectInBasePackages(String... packageNames) {
|
||||
Arrays.stream(packageNames).forEach(this::detectInBasePackage);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroupSpec register(Class<?>... serviceTypes) {
|
||||
for (Class<?> serviceType : serviceTypes) {
|
||||
this.registration.httpServiceTypeNames().add(serviceType.getName());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
private void detectInBasePackage(String packageName) {
|
||||
getScanner().findCandidateComponents(packageName)
|
||||
.stream()
|
||||
.map(BeanDefinition::getBeanClassName)
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(this::register);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroupSpec detectInBasePackages(Class<?>... packageClasses) {
|
||||
for (Class<?> packageClass : packageClasses) {
|
||||
detect(packageClass.getPackageName());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroupSpec detectInBasePackages(String... packageNames) {
|
||||
for (String packageName : packageNames) {
|
||||
detect(packageName);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
private void detect(String packageName) {
|
||||
for (BeanDefinition definition : getScanner().findCandidateComponents(packageName)) {
|
||||
if (definition.getBeanClassName() != null) {
|
||||
this.registration.httpServiceTypeNames().add(definition.getBeanClassName());
|
||||
}
|
||||
}
|
||||
}
|
||||
private void register(String httpServiceTypeName) {
|
||||
this.registration.httpServiceTypeNames().add(httpServiceTypeName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -180,19 +180,12 @@ public class AnnotationHttpServiceRegistrarTests {
|
||||
|
||||
@Override
|
||||
public GroupSpec forGroup(String name, ClientType clientType) {
|
||||
return new TestGroupSpec(name, clientType);
|
||||
return new TestGroupSpec(this.groupMap, name, clientType);
|
||||
}
|
||||
|
||||
private class TestGroupSpec implements GroupSpec {
|
||||
|
||||
private final String groupName;
|
||||
|
||||
private final ClientType clientType;
|
||||
|
||||
public TestGroupSpec(String groupName, ClientType clientType) {
|
||||
this.groupName = groupName;
|
||||
this.clientType = clientType;
|
||||
}
|
||||
private record TestGroupSpec(Map<String, StubGroup> groupMap, String groupName,
|
||||
ClientType clientType) implements GroupSpec {
|
||||
|
||||
@Override
|
||||
public GroupSpec register(Class<?>... serviceTypes) {
|
||||
@@ -213,7 +206,7 @@ public class AnnotationHttpServiceRegistrarTests {
|
||||
}
|
||||
|
||||
private StubGroup getOrCreateGroup() {
|
||||
return groupMap.computeIfAbsent(this.groupName, name -> new StubGroup(name, this.clientType));
|
||||
return this.groupMap.computeIfAbsent(this.groupName, name -> new StubGroup(name, this.clientType));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user