From cf69785393137bf512eb6c9521f923cf4f7c0cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henning=20P=C3=B6ttker?= Date: Fri, 9 Jul 2021 01:53:36 +0200 Subject: [PATCH] Detect JobRepository as depending on DB init See gh-27221 --- .../batch/BatchAutoConfiguration.java | 3 +- ...pendsOnDatabaseInitializationDetector.java | 40 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 4 ++ .../batch/BatchAutoConfigurationTests.java | 16 ++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobRepositoryDependsOnDatabaseInitializationDetector.java 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 72244720e2..ce670528d4 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 @@ -35,6 +35,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.sql.init.dependency.DatabaseInitializationDependencyConfigurer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -64,7 +65,7 @@ import org.springframework.util.StringUtils; @AutoConfigureAfter(HibernateJpaAutoConfiguration.class) @ConditionalOnBean(JobLauncher.class) @EnableConfigurationProperties(BatchProperties.class) -@Import(BatchConfigurerConfiguration.class) +@Import({ BatchConfigurerConfiguration.class, DatabaseInitializationDependencyConfigurer.class }) public class BatchAutoConfiguration { @Bean diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobRepositoryDependsOnDatabaseInitializationDetector.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobRepositoryDependsOnDatabaseInitializationDetector.java new file mode 100644 index 0000000000..6d1b5cb907 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobRepositoryDependsOnDatabaseInitializationDetector.java @@ -0,0 +1,40 @@ +/* + * Copyright 2021 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.batch; + +import java.util.Collections; +import java.util.Set; + +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.boot.sql.init.dependency.AbstractBeansOfTypeDependsOnDatabaseInitializationDetector; +import org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector; + +/** + * {@link DependsOnDatabaseInitializationDetector} for Spring Batch's + * {@link JobRepository}. + * + * @author Henning Pƶttker + */ +class JobRepositoryDependsOnDatabaseInitializationDetector + extends AbstractBeansOfTypeDependsOnDatabaseInitializationDetector { + + @Override + protected Set> getDependsOnDatabaseInitializationBeanTypes() { + return Collections.singleton(JobRepository.class); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index a438278ef2..54df138891 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -176,3 +176,7 @@ org.springframework.boot.autoconfigure.web.servlet.JspTemplateAvailabilityProvid # DataSource initializer detectors org.springframework.boot.sql.init.dependency.DatabaseInitializerDetector=\ org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializerDatabaseInitializerDetector + +# Depends on database initialization detectors +org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector=\ +org.springframework.boot.autoconfigure.batch.JobRepositoryDependsOnDatabaseInitializationDetector 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 c71d2f0bd1..22935ccc96 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 @@ -41,6 +41,7 @@ import org.springframework.batch.core.repository.JobRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.DefaultApplicationArguments; +import org.springframework.boot.LazyInitializationBeanFactoryPostProcessor; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; @@ -153,6 +154,21 @@ class BatchAutoConfigurationTests { }); } + @Test + void testDefinesAndLaunchesNamedJobWithLazyInitialization() { + this.contextRunner + .withUserConfiguration(NamedJobConfigurationWithRegisteredJob.class, + EmbeddedDataSourceConfiguration.class) + .withInitializer((context) -> context + .addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor())) + .withPropertyValues("spring.batch.job.names:discreteRegisteredJob").run((context) -> { + assertThat(context).hasSingleBean(JobLauncher.class); + context.getBean(JobLauncherApplicationRunner.class).run(); + assertThat(context.getBean(JobRepository.class).getLastJobExecution("discreteRegisteredJob", + new JobParameters())).isNotNull(); + }); + } + @Test void testDefinesAndLaunchesLocalJob() { this.contextRunner