Skip to content

Commit 8bf9e0c

Browse files
committed
Polish "Add hints to instantiate HttpService group adapters"
See gh-35038
1 parent 8142f80 commit 8bf9e0c

File tree

7 files changed

+45
-131
lines changed

7 files changed

+45
-131
lines changed

spring-web/src/main/java/org/springframework/web/client/support/RestClientHttpServiceGroupAdapterRuntimeHints.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727

2828
import org.jspecify.annotations.Nullable;
2929

30+
import org.springframework.aot.hint.MemberCategory;
31+
import org.springframework.aot.hint.RuntimeHints;
32+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
33+
import org.springframework.aot.hint.TypeReference;
3034
import org.springframework.beans.BeanUtils;
3135
import org.springframework.beans.BeansException;
3236
import org.springframework.beans.factory.BeanClassLoaderAware;
@@ -126,14 +130,15 @@ public HttpServiceProxyRegistry getObject() {
126130

127131
private static class GroupAdapterInitializer {
128132

133+
private static final String REST_CLIENT_HTTP_SERVICE_GROUP_ADAPTER = "org.springframework.web.client.support.RestClientHttpServiceGroupAdapter";
134+
135+
private static final String WEB_CLIENT_HTTP_SERVICE_GROUP_ADAPTER = "org.springframework.web.reactive.function.client.support.WebClientHttpServiceGroupAdapter";
136+
129137
static Map<HttpServiceGroup.ClientType, HttpServiceGroupAdapter<?>> initGroupAdapters() {
130138
Map<HttpServiceGroup.ClientType, HttpServiceGroupAdapter<?>> map = new LinkedHashMap<>(2);
131139

132-
addGroupAdapter(map, HttpServiceGroup.ClientType.REST_CLIENT,
133-
"org.springframework.web.client.support.RestClientHttpServiceGroupAdapter");
134-
135-
addGroupAdapter(map, HttpServiceGroup.ClientType.WEB_CLIENT,
136-
"org.springframework.web.reactive.function.client.support.WebClientHttpServiceGroupAdapter");
140+
addGroupAdapter(map, HttpServiceGroup.ClientType.REST_CLIENT, REST_CLIENT_HTTP_SERVICE_GROUP_ADAPTER);
141+
addGroupAdapter(map, HttpServiceGroup.ClientType.WEB_CLIENT, WEB_CLIENT_HTTP_SERVICE_GROUP_ADAPTER);
137142

138143
return map;
139144
}
@@ -314,4 +319,17 @@ private Map<Class<?>, Object> getProxyMapForGroup(String groupName) {
314319
}
315320
}
316321

322+
static class HttpServiceProxyRegistryRuntimeHints implements RuntimeHintsRegistrar {
323+
324+
@Override
325+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
326+
hints.reflection()
327+
.registerType(TypeReference.of(GroupAdapterInitializer.REST_CLIENT_HTTP_SERVICE_GROUP_ADAPTER),
328+
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)
329+
.registerTypeIfPresent(classLoader, GroupAdapterInitializer.WEB_CLIENT_HTTP_SERVICE_GROUP_ADAPTER,
330+
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
331+
}
332+
333+
}
334+
317335
}

spring-web/src/main/resources/META-INF/spring/aot.factories

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ org.springframework.http.HttpMimeTypesRuntimeHints,\
33
org.springframework.http.codec.CodecConfigurerRuntimeHints,\
44
org.springframework.http.converter.json.JacksonModulesRuntimeHints,\
55
org.springframework.http.converter.json.ProblemDetailRuntimeHints,\
6-
org.springframework.web.util.WebUtilRuntimeHints,\
7-
org.springframework.web.client.support.RestClientHttpServiceGroupAdapterRuntimeHints
6+
org.springframework.web.service.registry.HttpServiceProxyRegistryFactoryBean.HttpServiceProxyRegistryRuntimeHints,\
7+
org.springframework.web.util.WebUtilRuntimeHints
88

99
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
1010
org.springframework.web.service.annotation.HttpExchangeBeanRegistrationAotProcessor,\
Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,32 @@
1616

1717
package org.springframework.web.client.support;
1818

19-
import org.junit.jupiter.api.BeforeEach;
2019
import org.junit.jupiter.api.Test;
2120

22-
import org.springframework.aot.hint.MemberCategory;
2321
import org.springframework.aot.hint.RuntimeHints;
2422
import org.springframework.aot.hint.RuntimeHintsRegistrar;
2523
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
26-
import org.springframework.core.io.support.SpringFactoriesLoader;
24+
import org.springframework.beans.factory.aot.AotServices;
2725
import org.springframework.util.ClassUtils;
2826

2927
import static org.assertj.core.api.Assertions.assertThat;
3028

3129
/**
32-
* Tests for {@link RestClientHttpServiceGroupAdapterRuntimeHints}.
30+
* Tests for {@link RestClientHttpServiceGroupAdapter}.
3331
*
3432
* @author Olga Maciaszek-Sharma
33+
* @author Stephane Nicoll
3534
*/
36-
class RestClientHttpServiceGroupAdapterRuntimeHintsRegistrarTests {
37-
38-
private RuntimeHints hints;
39-
40-
@BeforeEach
41-
void setUp() {
42-
this.hints = new RuntimeHints();
43-
SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
44-
.load(RuntimeHintsRegistrar.class)
45-
.forEach(registrar ->
46-
registrar.registerHints(this.hints, ClassUtils.getDefaultClassLoader()));
47-
}
35+
class RestClientHttpServiceGroupAdapterTests {
4836

4937
@Test
50-
void shouldRegisterHintsForAdapter() {
38+
void registerInstantiationHints() throws Exception {
39+
RuntimeHints hints = new RuntimeHints();
40+
AotServices.factories().load(RuntimeHintsRegistrar.class)
41+
.forEach(registrar -> registrar.registerHints(hints,
42+
ClassUtils.getDefaultClassLoader()));
5143
assertThat(RuntimeHintsPredicates.reflection()
52-
.onType(RestClientHttpServiceGroupAdapter.class)
53-
.withMemberCategory(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))
54-
.accepts(this.hints);
55-
44+
.onConstructorInvocation(RestClientHttpServiceGroupAdapter.class.getConstructor())).accepts(hints);
5645
}
5746

5847
}

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/support/WebClientHttpServiceGroupAdapterRuntimeHints.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

spring-webflux/src/main/resources/META-INF/spring/aot.factories

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,32 @@
1616

1717
package org.springframework.web.reactive.function.client.support;
1818

19-
import org.junit.jupiter.api.BeforeEach;
2019
import org.junit.jupiter.api.Test;
2120

22-
import org.springframework.aot.hint.MemberCategory;
2321
import org.springframework.aot.hint.RuntimeHints;
2422
import org.springframework.aot.hint.RuntimeHintsRegistrar;
2523
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
26-
import org.springframework.core.io.support.SpringFactoriesLoader;
24+
import org.springframework.beans.factory.aot.AotServices;
2725
import org.springframework.util.ClassUtils;
2826

2927
import static org.assertj.core.api.Assertions.assertThat;
3028

3129
/**
32-
* Tests for {@link WebClientHttpServiceGroupAdapterRuntimeHints}.
30+
* Tests for {@link WebClientHttpServiceGroupAdapter}.
3331
*
3432
* @author Olga Maciaszek-Sharma
33+
* @author Stephane Nicoll
3534
*/
36-
class WebClientHttpServiceGroupAdapterRuntimeHintsTests {
37-
38-
private RuntimeHints hints;
39-
40-
@BeforeEach
41-
void setUp() {
42-
this.hints = new RuntimeHints();
43-
SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
44-
.load(RuntimeHintsRegistrar.class)
45-
.forEach(registrar ->
46-
registrar.registerHints(this.hints, ClassUtils.getDefaultClassLoader()));
47-
}
35+
class WebClientHttpServiceGroupAdapterTests {
4836

4937
@Test
50-
void shouldRegisterHintsForAdapter() {
38+
void registerInstantiationHints() throws Exception {
39+
RuntimeHints hints = new RuntimeHints();
40+
AotServices.factories().load(RuntimeHintsRegistrar.class)
41+
.forEach(registrar -> registrar.registerHints(hints,
42+
ClassUtils.getDefaultClassLoader()));
5143
assertThat(RuntimeHintsPredicates.reflection()
52-
.onType(WebClientHttpServiceGroupAdapter.class)
53-
.withMemberCategory(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))
54-
.accepts(this.hints);
55-
44+
.onConstructorInvocation(WebClientHttpServiceGroupAdapter.class.getConstructor())).accepts(hints);
5645
}
5746

5847
}

0 commit comments

Comments
 (0)