Skip to content

Commit 75c4bc8

Browse files
committed
Merge pull request #35038 from OlgaMaciaszek
* pr/35038: Polish "Add hints to instantiate HttpService group adapters" Add hints to instantiate HttpService group adapters Closes gh-35038
2 parents 082eb60 + 8bf9e0c commit 75c4bc8

File tree

4 files changed

+118
-5
lines changed

4 files changed

+118
-5
lines changed

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ 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.service.registry.HttpServiceProxyRegistryFactoryBean.HttpServiceProxyRegistryRuntimeHints,\
67
org.springframework.web.util.WebUtilRuntimeHints
78

89
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2002-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.client.support;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aot.hint.RuntimeHints;
22+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
23+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
24+
import org.springframework.beans.factory.aot.AotServices;
25+
import org.springframework.util.ClassUtils;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link RestClientHttpServiceGroupAdapter}.
31+
*
32+
* @author Olga Maciaszek-Sharma
33+
* @author Stephane Nicoll
34+
*/
35+
class RestClientHttpServiceGroupAdapterTests {
36+
37+
@Test
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()));
43+
assertThat(RuntimeHintsPredicates.reflection()
44+
.onConstructorInvocation(RestClientHttpServiceGroupAdapter.class.getConstructor())).accepts(hints);
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2002-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.reactive.function.client.support;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aot.hint.RuntimeHints;
22+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
23+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
24+
import org.springframework.beans.factory.aot.AotServices;
25+
import org.springframework.util.ClassUtils;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link WebClientHttpServiceGroupAdapter}.
31+
*
32+
* @author Olga Maciaszek-Sharma
33+
* @author Stephane Nicoll
34+
*/
35+
class WebClientHttpServiceGroupAdapterTests {
36+
37+
@Test
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()));
43+
assertThat(RuntimeHintsPredicates.reflection()
44+
.onConstructorInvocation(WebClientHttpServiceGroupAdapter.class.getConstructor())).accepts(hints);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)