Add AnnotatedControllerConfigurerCustomizer to provide a friendly API to modify the AnnotatedControllerConfigurer. End users could provide HandlerMethodArgumentResolvers without having to override the entire AnnotatedControllerConfigurer bean.

This commit is contained in:
maxhov 2024-04-17 17:58:03 +02:00
parent f896ce711f
commit 2a052dc75c
No known key found for this signature in database
3 changed files with 72 additions and 1 deletions

View File

@ -38,6 +38,7 @@ import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.graphql.data.AnnotatedControllerConfigurerCustomizer;
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.convert.ApplicationConversionService;
@ -154,11 +155,13 @@ public class GraphQlAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public AnnotatedControllerConfigurer annotatedControllerConfigurer(
@Qualifier(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME) ObjectProvider<Executor> executorProvider) {
@Qualifier(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME) ObjectProvider<Executor> executorProvider,
ObjectProvider<AnnotatedControllerConfigurerCustomizer> customizers) {
AnnotatedControllerConfigurer controllerConfigurer = new AnnotatedControllerConfigurer();
controllerConfigurer
.addFormatterRegistrar((registry) -> ApplicationConversionService.addBeans(registry, this.beanFactory));
executorProvider.ifAvailable(controllerConfigurer::setExecutor);
customizers.orderedStream().forEach((customizer) -> customizer.customize(controllerConfigurer));
return controllerConfigurer;
}

View File

@ -0,0 +1,36 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.graphql.data;
import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer;
/**
* Callback interface that can be implemented by beans wishing to customize properties of
* {@link AnnotatedControllerConfigurer} whilst retaining default auto-configuration.
*
* @author Max Hovens
*/
@FunctionalInterface
public interface AnnotatedControllerConfigurerCustomizer {
/**
* Customize the {@link AnnotatedControllerConfigurer} instance.
* @param configurer the configurer to customize
*/
void customize(AnnotatedControllerConfigurer configurer);
}

View File

@ -36,6 +36,8 @@ import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.graphql.GraphQlAutoConfiguration.GraphQlResourcesRuntimeHints;
import org.springframework.boot.autoconfigure.graphql.GraphQlAutoConfigurationTests.AnnotatedControllerConfigurerCustomizerConfiguration.CustomAnnotatedControllerConfigurerCustomizer;
import org.springframework.boot.autoconfigure.graphql.data.AnnotatedControllerConfigurerCustomizer;
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.system.CapturedOutput;
@ -241,6 +243,15 @@ class GraphQlAutoConfigurationTests {
});
}
@Test
void whenAnnotatedControllerConfigurerCustomizerIsDefinedThenAnnotatedControllerConfigurerShouldUseIt() {
this.contextRunner.withUserConfiguration(CustomAnnotatedControllerConfigurerCustomizer.class).run((context) -> {
CustomAnnotatedControllerConfigurerCustomizer customizer = context
.getBean(CustomAnnotatedControllerConfigurerCustomizer.class);
assertThat(customizer.applied).isTrue();
});
}
@Configuration(proxyBeanMethods = false)
static class CustomGraphQlBuilderConfiguration {
@ -336,4 +347,25 @@ class GraphQlAutoConfigurationTests {
}
static class AnnotatedControllerConfigurerCustomizerConfiguration {
@Bean
CustomAnnotatedControllerConfigurerCustomizer customAnnotatedControllerConfigurerCustomizer() {
return new CustomAnnotatedControllerConfigurerCustomizer();
}
public static class CustomAnnotatedControllerConfigurerCustomizer
implements AnnotatedControllerConfigurerCustomizer {
public boolean applied = false;
@Override
public void customize(AnnotatedControllerConfigurer configurer) {
this.applied = true;
}
}
}
}