Rework shellConversionService

- Replace old ConversionService impl with something more aligned
  with current boot code with partially shameless copy as
  shell is most likely working outside of web or webflux env
  by creating FormattingConversionService and hooking convertes
  from DefaultConversionService.
- This should work on most cases and giving user a hook
  to define their own service.
This commit is contained in:
Janne Valkealahti
2021-12-24 15:04:40 +00:00
parent 58792bc695
commit 7042d1f83b
3 changed files with 15 additions and 23 deletions

View File

@@ -4,7 +4,6 @@ import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.ObjectProvider;
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;
@@ -16,7 +15,7 @@ import org.springframework.shell.standard.ValueProvider;
public class ParameterResolverAutoConfiguration {
@Bean
public ParameterResolver standardParameterResolver(@Qualifier("spring-shell") ConversionService conversionService,
public ParameterResolver standardParameterResolver(ConversionService conversionService,
ObjectProvider<ValueProvider> valueProviders) {
Set<ValueProvider> collect = valueProviders.orderedStream().collect(Collectors.toSet());
return new StandardParameterResolver(conversionService, collect);

View File

@@ -16,19 +16,17 @@
package org.springframework.shell.boot;
import java.util.Collection;
import java.util.Set;
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.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.shell.CommandRegistry;
import org.springframework.shell.ResultHandler;
import org.springframework.shell.ResultHandlerService;
@@ -44,23 +42,13 @@ import org.springframework.shell.result.ResultHandlerConfig;
public class SpringShellAutoConfiguration {
@Bean
@Qualifier("spring-shell")
@ConditionalOnMissingBean(ConversionService.class)
public ConversionService shellConversionService(ApplicationContext applicationContext) {
Collection<Converter> converters = applicationContext.getBeansOfType(Converter.class).values();
Collection<GenericConverter> genericConverters = applicationContext.getBeansOfType(GenericConverter.class).values();
Collection<ConverterFactory> converterFactories = applicationContext.getBeansOfType(ConverterFactory.class).values();
DefaultConversionService defaultConversionService = new DefaultConversionService();
for (Converter converter : converters) {
defaultConversionService.addConverter(converter);
}
for (GenericConverter genericConverter : genericConverters) {
defaultConversionService.addConverter(genericConverter);
}
for (ConverterFactory converterFactory : converterFactories) {
defaultConversionService.addConverterFactory(converterFactory);
}
return defaultConversionService;
FormattingConversionService service = new FormattingConversionService();
DefaultConversionService.addDefaultConverters(service);
DefaultConversionService.addCollectionConverters(service);
ApplicationConversionService.addBeans(service, applicationContext);
return service;
}
@Bean

View File

@@ -41,6 +41,11 @@ class DomainObject {
public String getValue() {
return value;
}
@Override
public String toString() {
return "DomainObject [value=" + value + "]";
}
}
@Component