Skip to content

Commit 2a052dc

Browse files
committed
Add AnnotatedControllerConfigurerCustomizer to provide a friendly API to modify the AnnotatedControllerConfigurer. End users could provide HandlerMethodArgumentResolvers without having to override the entire AnnotatedControllerConfigurer bean.
1 parent f896ce7 commit 2a052dc

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3939
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
4040
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
41+
import org.springframework.boot.autoconfigure.graphql.data.AnnotatedControllerConfigurerCustomizer;
4142
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
4243
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4344
import org.springframework.boot.convert.ApplicationConversionService;
@@ -154,11 +155,13 @@ public ExecutionGraphQlService executionGraphQlService(GraphQlSource graphQlSour
154155
@Bean
155156
@ConditionalOnMissingBean
156157
public AnnotatedControllerConfigurer annotatedControllerConfigurer(
157-
@Qualifier(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME) ObjectProvider<Executor> executorProvider) {
158+
@Qualifier(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME) ObjectProvider<Executor> executorProvider,
159+
ObjectProvider<AnnotatedControllerConfigurerCustomizer> customizers) {
158160
AnnotatedControllerConfigurer controllerConfigurer = new AnnotatedControllerConfigurer();
159161
controllerConfigurer
160162
.addFormatterRegistrar((registry) -> ApplicationConversionService.addBeans(registry, this.beanFactory));
161163
executorProvider.ifAvailable(controllerConfigurer::setExecutor);
164+
customizers.orderedStream().forEach((customizer) -> customizer.customize(controllerConfigurer));
162165
return controllerConfigurer;
163166
}
164167

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2012-2024 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.boot.autoconfigure.graphql.data;
18+
19+
import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer;
20+
21+
/**
22+
* Callback interface that can be implemented by beans wishing to customize properties of
23+
* {@link AnnotatedControllerConfigurer} whilst retaining default auto-configuration.
24+
*
25+
* @author Max Hovens
26+
*/
27+
@FunctionalInterface
28+
public interface AnnotatedControllerConfigurerCustomizer {
29+
30+
/**
31+
* Customize the {@link AnnotatedControllerConfigurer} instance.
32+
* @param configurer the configurer to customize
33+
*/
34+
void customize(AnnotatedControllerConfigurer configurer);
35+
36+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
3737
import org.springframework.boot.autoconfigure.AutoConfigurations;
3838
import org.springframework.boot.autoconfigure.graphql.GraphQlAutoConfiguration.GraphQlResourcesRuntimeHints;
39+
import org.springframework.boot.autoconfigure.graphql.GraphQlAutoConfigurationTests.AnnotatedControllerConfigurerCustomizerConfiguration.CustomAnnotatedControllerConfigurerCustomizer;
40+
import org.springframework.boot.autoconfigure.graphql.data.AnnotatedControllerConfigurerCustomizer;
3941
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
4042
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
4143
import org.springframework.boot.test.system.CapturedOutput;
@@ -241,6 +243,15 @@ void whenCustomExecutorIsDefinedThenAnnotatedControllerConfigurerDoesNotUseIt()
241243
});
242244
}
243245

246+
@Test
247+
void whenAnnotatedControllerConfigurerCustomizerIsDefinedThenAnnotatedControllerConfigurerShouldUseIt() {
248+
this.contextRunner.withUserConfiguration(CustomAnnotatedControllerConfigurerCustomizer.class).run((context) -> {
249+
CustomAnnotatedControllerConfigurerCustomizer customizer = context
250+
.getBean(CustomAnnotatedControllerConfigurerCustomizer.class);
251+
assertThat(customizer.applied).isTrue();
252+
});
253+
}
254+
244255
@Configuration(proxyBeanMethods = false)
245256
static class CustomGraphQlBuilderConfiguration {
246257

@@ -336,4 +347,25 @@ Executor customExecutor() {
336347

337348
}
338349

350+
static class AnnotatedControllerConfigurerCustomizerConfiguration {
351+
352+
@Bean
353+
CustomAnnotatedControllerConfigurerCustomizer customAnnotatedControllerConfigurerCustomizer() {
354+
return new CustomAnnotatedControllerConfigurerCustomizer();
355+
}
356+
357+
public static class CustomAnnotatedControllerConfigurerCustomizer
358+
implements AnnotatedControllerConfigurerCustomizer {
359+
360+
public boolean applied = false;
361+
362+
@Override
363+
public void customize(AnnotatedControllerConfigurer configurer) {
364+
this.applied = true;
365+
}
366+
367+
}
368+
369+
}
370+
339371
}

0 commit comments

Comments
 (0)