Skip to content

Commit 0fc9e4e

Browse files
committed
Exclude HttpServiceClient from GroupRegistry scan
The detect methods in the GroupRegistry that find all interfaces with HttpExchange annotations now exclude HttpServiceClient interfaces that are instead supported by a dedicated registrar. This ensures there is no overlap between the HttpServiceClient registrar scan and the ImportHttpServices registrar scan or the scan of any other custom registrar. See gh-35244
1 parent 09917fa commit 0fc9e4e

File tree

6 files changed

+37
-16
lines changed

6 files changed

+37
-16
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ protected abstract void registerHttpServices(
188188
GroupRegistry registry, AnnotationMetadata importingClassMetadata);
189189

190190

191+
/**
192+
* Exposes the scan for HTTP Service types, looking for
193+
* interfaces with type or method {@link HttpExchange} annotations.
194+
* @param basePackage the packages to look under
195+
* @return match bean definitions
196+
*/
191197
protected Stream<BeanDefinition> findHttpServices(String basePackage) {
192198
if (this.scanner == null) {
193199
Assert.state(this.environment != null, "Environment has not been set");
@@ -257,7 +263,10 @@ interface GroupSpec {
257263

258264
/**
259265
* Detect HTTP Service types in the given packages, looking for
260-
* interfaces with a type and/or method {@link HttpExchange} annotation.
266+
* interfaces with type or method {@link HttpExchange} annotations.
267+
* <p>The performed scan, however, filters out any interfaces
268+
* annotated with {@link HttpServiceClient} that are instead supported
269+
* by {@link HttpServiceClientRegistrarSupport}.
261270
*/
262271
GroupSpec detectInBasePackages(Class<?>... packageClasses);
263272

@@ -314,11 +323,19 @@ public GroupRegistry.GroupSpec detectInBasePackages(String... packageNames) {
314323

315324
private void detectInBasePackage(String packageName) {
316325
findHttpServices(packageName)
326+
.filter(DefaultGroupSpec::isNotHttpServiceClientAnnotated)
317327
.map(BeanDefinition::getBeanClassName)
318328
.filter(Objects::nonNull)
319329
.forEach(this::registerServiceTypeName);
320330
}
321331

332+
private static boolean isNotHttpServiceClientAnnotated(BeanDefinition defintion) {
333+
if (defintion instanceof AnnotatedBeanDefinition abd) {
334+
return !abd.getMetadata().hasAnnotation(HttpServiceClient.class.getName());
335+
}
336+
return true;
337+
}
338+
322339
private void registerServiceTypeName(String httpServiceTypeName) {
323340
this.registration.httpServiceTypeNames().add(httpServiceTypeName);
324341
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@
7676
String group() default HttpServiceGroup.DEFAULT_GROUP_NAME;
7777

7878
/**
79-
* Detect HTTP Services in the packages of the specified classes by looking
80-
* for interfaces with type-level or method-level
81-
* {@link org.springframework.web.service.annotation.HttpExchange @HttpExchange}
82-
* annotations.
79+
* Detect HTTP Services in the packages of the specified classes, looking
80+
* for interfaces with type or method {@link HttpExchange} annotations.
81+
* <p>The performed scan, however, filters out interfaces annotated with
82+
* {@link HttpServiceClient} that are instead supported by
83+
* {@link HttpServiceClientRegistrarSupport}.
8384
*/
8485
Class<?>[] basePackageClasses() default {};
8586

spring-web/src/test/java/org/springframework/web/service/registry/HttpServiceClientRegistrarSupportTests.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import org.springframework.core.env.StandardEnvironment;
2626
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
2727
import org.springframework.core.type.AnnotationMetadata;
28-
import org.springframework.web.service.registry.client.DefaultClient;
29-
import org.springframework.web.service.registry.client.EchoClientA;
30-
import org.springframework.web.service.registry.client.EchoClientB;
28+
import org.springframework.web.service.registry.basic.BasicClient;
29+
import org.springframework.web.service.registry.echo.EchoClientA;
30+
import org.springframework.web.service.registry.echo.EchoClientB;
3131

3232
import static org.assertj.core.api.Assertions.assertThat;
3333
import static org.mockito.Mockito.mock;
@@ -43,21 +43,24 @@ public class HttpServiceClientRegistrarSupportTests {
4343

4444
@Test
4545
void register() {
46+
47+
List<String> basePackages = List.of(
48+
BasicClient.class.getPackageName(), EchoClientA.class.getPackageName());
49+
4650
HttpServiceClientRegistrarSupport registrar = new HttpServiceClientRegistrarSupport() {
4751

4852
@Override
4953
protected void registerHttpServices(GroupRegistry registry, AnnotationMetadata importingClassMetadata) {
50-
findAndRegisterHttpServiceClients(groupRegistry, List.of(getClass().getPackage().getName() + ".client"));
54+
findAndRegisterHttpServiceClients(groupRegistry, basePackages);
5155
}
5256
};
5357
registrar.setEnvironment(new StandardEnvironment());
5458
registrar.setResourceLoader(new PathMatchingResourcePatternResolver());
55-
5659
registrar.registerHttpServices(groupRegistry, mock(AnnotationMetadata.class));
5760

5861
assertGroups(
59-
TestGroup.ofListing("echo", EchoClientA.class, EchoClientB.class),
60-
TestGroup.ofListing("default", DefaultClient.class));
62+
TestGroup.ofListing("default", BasicClient.class),
63+
TestGroup.ofListing("echo", EchoClientA.class, EchoClientB.class));
6164
}
6265

6366
private void assertGroups(TestGroup... expectedGroups) {

spring-web/src/test/java/org/springframework/web/service/registry/client/DefaultClient.java renamed to spring-web/src/test/java/org/springframework/web/service/registry/basic/BasicClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.web.service.registry.client;
17+
package org.springframework.web.service.registry.basic;
1818

1919

2020
import org.springframework.web.bind.annotation.RequestParam;
2121
import org.springframework.web.service.annotation.GetExchange;
2222
import org.springframework.web.service.registry.HttpServiceClient;
2323

2424
@HttpServiceClient
25-
public interface DefaultClient {
25+
public interface BasicClient {
2626

2727
@GetExchange
2828
String handle(@RequestParam String input);

spring-web/src/test/java/org/springframework/web/service/registry/client/EchoClientA.java renamed to spring-web/src/test/java/org/springframework/web/service/registry/echo/EchoClientA.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.web.service.registry.client;
17+
package org.springframework.web.service.registry.echo;
1818

1919

2020
import org.springframework.web.bind.annotation.RequestParam;

spring-web/src/test/java/org/springframework/web/service/registry/client/EchoClientB.java renamed to spring-web/src/test/java/org/springframework/web/service/registry/echo/EchoClientB.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.web.service.registry.client;
17+
package org.springframework.web.service.registry.echo;
1818

1919

2020
import org.springframework.web.bind.annotation.RequestParam;

0 commit comments

Comments
 (0)