diff --git a/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ParameterResolverAutoConfiguration.java b/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ParameterResolverAutoConfiguration.java index f1879d36..a8b47709 100644 --- a/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ParameterResolverAutoConfiguration.java +++ b/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ParameterResolverAutoConfiguration.java @@ -3,10 +3,8 @@ package org.springframework.shell.boot; import java.util.ArrayList; import java.util.List; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.convert.ConversionService; import org.springframework.messaging.handler.annotation.support.HeadersMethodArgumentResolver; import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; import org.springframework.shell.command.ArgumentHeaderMethodArgumentResolver; @@ -14,6 +12,7 @@ import org.springframework.shell.command.CommandContextMethodArgumentResolver; import org.springframework.shell.command.CommandExecution.CommandExecutionHandlerMethodArgumentResolvers; import org.springframework.shell.completion.CompletionResolver; import org.springframework.shell.completion.DefaultCompletionResolver; +import org.springframework.shell.config.ShellConversionServiceSupplier; import org.springframework.shell.standard.ShellOptionMethodArgumentResolver; @Configuration(proxyBeanMethods = false) @@ -26,12 +25,12 @@ public class ParameterResolverAutoConfiguration { @Bean public CommandExecutionHandlerMethodArgumentResolvers commandExecutionHandlerMethodArgumentResolvers( - @Qualifier("shellConversionService") ConversionService shellConversionService) { + ShellConversionServiceSupplier shellConversionServiceSupplier) { List resolvers = new ArrayList<>(); - resolvers.add(new ArgumentHeaderMethodArgumentResolver(shellConversionService, null)); + resolvers.add(new ArgumentHeaderMethodArgumentResolver(shellConversionServiceSupplier.get(), null)); resolvers.add(new HeadersMethodArgumentResolver()); resolvers.add(new CommandContextMethodArgumentResolver()); - resolvers.add(new ShellOptionMethodArgumentResolver(shellConversionService, null)); + resolvers.add(new ShellOptionMethodArgumentResolver(shellConversionServiceSupplier.get(), null)); return new CommandExecutionHandlerMethodArgumentResolvers(resolvers); } } diff --git a/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/SpringShellAutoConfiguration.java b/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/SpringShellAutoConfiguration.java index daff4cdb..aa2fb492 100644 --- a/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/SpringShellAutoConfiguration.java +++ b/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/SpringShellAutoConfiguration.java @@ -20,20 +20,19 @@ import java.util.Set; import org.jline.terminal.Terminal; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.convert.ApplicationConversionService; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; -import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.format.support.FormattingConversionService; import org.springframework.shell.ResultHandler; import org.springframework.shell.ResultHandlerService; import org.springframework.shell.Shell; import org.springframework.shell.command.CommandCatalog; +import org.springframework.shell.config.ShellConversionServiceSupplier; import org.springframework.shell.context.ShellContext; import org.springframework.shell.exit.ExitCodeMappings; import org.springframework.shell.result.GenericResultHandlerService; @@ -47,13 +46,13 @@ import org.springframework.shell.result.ResultHandlerConfig; public class SpringShellAutoConfiguration { @Bean - @ConditionalOnMissingBean(value = ConversionService.class, name = { "shellConversionService" }) - public ConversionService shellConversionService(ApplicationContext applicationContext) { + @ConditionalOnMissingBean + public ShellConversionServiceSupplier shellConversionServiceSupplier(ApplicationContext applicationContext) { FormattingConversionService service = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(service); DefaultConversionService.addCollectionConverters(service); ApplicationConversionService.addBeans(service, applicationContext); - return service; + return () -> service; } @Bean @@ -67,10 +66,10 @@ public class SpringShellAutoConfiguration { @Bean public Shell shell(ResultHandlerService resultHandlerService, CommandCatalog commandRegistry, Terminal terminal, - @Qualifier("shellConversionService") ConversionService shellConversionService, ShellContext shellContext, + ShellConversionServiceSupplier shellConversionServiceSupplier, ShellContext shellContext, ExitCodeMappings exitCodeMappings) { Shell shell = new Shell(resultHandlerService, commandRegistry, terminal, shellContext, exitCodeMappings); - shell.setConversionService(shellConversionService); + shell.setConversionService(shellConversionServiceSupplier.get()); return shell; } } diff --git a/spring-shell-autoconfigure/src/test/java/org/springframework/shell/boot/ParameterResolverAutoConfigurationTests.java b/spring-shell-autoconfigure/src/test/java/org/springframework/shell/boot/ParameterResolverAutoConfigurationTests.java index bbe6b1d1..44c3c6c6 100644 --- a/spring-shell-autoconfigure/src/test/java/org/springframework/shell/boot/ParameterResolverAutoConfigurationTests.java +++ b/spring-shell-autoconfigure/src/test/java/org/springframework/shell/boot/ParameterResolverAutoConfigurationTests.java @@ -21,10 +21,10 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.shell.command.CommandExecution.CommandExecutionHandlerMethodArgumentResolvers; import org.springframework.shell.completion.CompletionResolver; +import org.springframework.shell.config.ShellConversionServiceSupplier; import static org.assertj.core.api.Assertions.assertThat; @@ -49,31 +49,12 @@ public class ParameterResolverAutoConfigurationTests { }); } - @Test - void multipleConversionServiceBeans() { - this.contextRunner - .withUserConfiguration(CustomShellConversionServiceConfiguration.class, - ExtraConversionServiceConfiguration.class) - .run((context) -> { - assertThat(context).hasSingleBean(CommandExecutionHandlerMethodArgumentResolvers.class); - }); - } - @Configuration static class CustomShellConversionServiceConfiguration { @Bean - ConversionService shellConversionService() { - return new DefaultConversionService(); - } - } - - @Configuration - static class ExtraConversionServiceConfiguration { - - @Bean - ConversionService conversionService() { - return new DefaultConversionService(); + ShellConversionServiceSupplier shellConversionServiceSupplier() { + return () -> new DefaultConversionService(); } } } diff --git a/spring-shell-core/src/main/java/org/springframework/shell/config/ShellConversionServiceSupplier.java b/spring-shell-core/src/main/java/org/springframework/shell/config/ShellConversionServiceSupplier.java new file mode 100644 index 00000000..2031d2fc --- /dev/null +++ b/spring-shell-core/src/main/java/org/springframework/shell/config/ShellConversionServiceSupplier.java @@ -0,0 +1,28 @@ +/* + * Copyright 2022 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.shell.config; + +import java.util.function.Supplier; + +import org.springframework.core.convert.ConversionService; + +/** + * Interface to provide shell spesific {@link ConversionService}. + * + * @author Janne Valkealahti + */ +public interface ShellConversionServiceSupplier extends Supplier { +}