diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java index 547cabf284..3c050a0856 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java @@ -122,11 +122,11 @@ public class BatchAutoConfiguration { SpringBootBatchConfiguration(DataSource dataSource, @BatchDataSource ObjectProvider batchDataSource, PlatformTransactionManager transactionManager, BatchProperties properties, - List batchConversionServiceCustomizers) { + ObjectProvider batchConversionServiceCustomizers) { this.dataSource = batchDataSource.getIfAvailable(() -> dataSource); this.transactionManager = transactionManager; this.properties = properties; - this.batchConversionServiceCustomizers = batchConversionServiceCustomizers; + this.batchConversionServiceCustomizers = batchConversionServiceCustomizers.orderedStream().toList(); } @Override diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchConversionServiceCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchConversionServiceCustomizer.java index af3d365082..a75a9a30d8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchConversionServiceCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchConversionServiceCustomizer.java @@ -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 diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java index 627ac5edf2..19a02f1f1d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -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 { - - @Override - public Bar convert(Foo source) { - return null; - } - + @Bean + @Order(2) + BatchConversionServiceCustomizer anotherBatchConversionServiceCustomizer() { + return mock(BatchConversionServiceCustomizer.class); } }