Polish "Add customizer for conversion service used by Spring Batch"

See gh-34769
pull/35090/head
Andy Wilkinson 2 years ago
parent cafa6f5d9c
commit f5e654748c

@ -122,11 +122,11 @@ public class BatchAutoConfiguration {
SpringBootBatchConfiguration(DataSource dataSource, @BatchDataSource ObjectProvider<DataSource> batchDataSource,
PlatformTransactionManager transactionManager, BatchProperties properties,
List<BatchConversionServiceCustomizer> batchConversionServiceCustomizers) {
ObjectProvider<BatchConversionServiceCustomizer> batchConversionServiceCustomizers) {
this.dataSource = batchDataSource.getIfAvailable(() -> dataSource);
this.transactionManager = transactionManager;
this.properties = properties;
this.batchConversionServiceCustomizers = batchConversionServiceCustomizers;
this.batchConversionServiceCustomizers = batchConversionServiceCustomizers.orderedStream().toList();
}
@Override

@ -1,5 +1,5 @@
/*
* Copyright 2023 the original author or authors.
* Copyright 2012-2023 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.
@ -16,13 +16,14 @@
package org.springframework.boot.autoconfigure.batch;
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
import org.springframework.core.convert.support.ConfigurableConversionService;
/**
* Callback interface that can be implemented by beans wishing to further customize the
* {@link ConfigurableConversionService} used in
* {@link org.springframework.batch.core.configuration.support.DefaultBatchConfiguration}
* retaining its default auto-configuration.
* Callback interface that can be implemented by beans wishing to customize the
* {@link ConfigurableConversionService} that is
* {@link DefaultBatchConfiguration#getConversionService provided by
* DefaultBatchAutoConfiguration} while retaining its default auto-configuration.
*
* @author Claudio Nave
* @since 3.1.0

@ -24,6 +24,8 @@ import javax.sql.DataSource;
import jakarta.persistence.EntityManagerFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
@ -71,7 +73,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.annotation.Order;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcTemplate;
@ -403,17 +405,20 @@ class BatchAutoConfigurationTests {
}
@Test
void userProvidedCustomConverter() {
void conversionServiceCustomizersAreCalled() {
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
.withUserConfiguration(RegisterCustomConverter.class)
.withUserConfiguration(ConversionServiceCustomizersConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(SpringBootBatchConfiguration.class);
BatchConversionServiceCustomizer customizer = context.getBean("batchConversionServiceCustomizer",
BatchConversionServiceCustomizer.class);
BatchConversionServiceCustomizer anotherCustomizer = context
.getBean("anotherBatchConversionServiceCustomizer", BatchConversionServiceCustomizer.class);
InOrder inOrder = Mockito.inOrder(customizer, anotherCustomizer);
ConfigurableConversionService configurableConversionService = context
.getBean(SpringBootBatchConfiguration.class)
.getConversionService();
assertThat(configurableConversionService.canConvert(RegisterCustomConverter.Foo.class,
RegisterCustomConverter.Bar.class))
.isTrue();
inOrder.verify(customizer).customize(configurableConversionService);
inOrder.verify(anotherCustomizer).customize(configurableConversionService);
});
}
@ -698,29 +703,18 @@ class BatchAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
static class RegisterCustomConverter {
static class ConversionServiceCustomizersConfiguration {
@Bean
@Order(1)
BatchConversionServiceCustomizer batchConversionServiceCustomizer() {
return (configurableConversionService) -> configurableConversionService
.addConverter(new FooToBarConverter());
}
static class Foo {
return mock(BatchConversionServiceCustomizer.class);
}
static class Bar {
}
static class FooToBarConverter implements Converter<Foo, Bar> {
@Override
public Bar convert(Foo source) {
return null;
}
@Bean
@Order(2)
BatchConversionServiceCustomizer anotherBatchConversionServiceCustomizer() {
return mock(BatchConversionServiceCustomizer.class);
}
}

Loading…
Cancel
Save