From 1cd3a54e889c74d849460a7235f17da6fffa40d5 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 9 Jul 2021 15:02:15 +0100 Subject: [PATCH] Rework Quartz DB init dependencies to use a detector Closes gh-27222 --- .../quartz/QuartzAutoConfiguration.java | 54 ++----------------- ...pendsOnDatabaseInitializationDetector.java | 43 +++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + 3 files changed, 48 insertions(+), 50 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/SchedulerDependsOnDatabaseInitializationDetector.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java index a872c6a91a..96ab880471 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-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. @@ -21,30 +21,28 @@ import java.util.Properties; import javax.sql.DataSource; -import liquibase.integration.spring.SpringLiquibase; import org.quartz.Calendar; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.Trigger; import org.springframework.beans.factory.ObjectProvider; -import org.springframework.boot.autoconfigure.AbstractDependsOnBeanFactoryPostProcessor; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; -import org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; 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.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import org.springframework.core.annotation.Order; import org.springframework.core.io.ResourceLoader; import org.springframework.scheduling.quartz.SchedulerFactoryBean; @@ -100,6 +98,7 @@ public class QuartzAutoConfiguration { @Configuration(proxyBeanMethods = false) @ConditionalOnSingleCandidate(DataSource.class) @ConditionalOnProperty(prefix = "spring.quartz", name = "job-store-type", havingValue = "jdbc") + @Import(DatabaseInitializationDependencyConfigurer.class) protected static class JdbcStoreTypeConfiguration { @Bean @@ -141,51 +140,6 @@ public class QuartzAutoConfiguration { return new QuartzDataSourceInitializer(dataSourceToUse, resourceLoader, properties); } - /** - * Additional configuration to ensure that {@link SchedulerFactoryBean} and - * {@link Scheduler} beans depend on any beans that perform data source - * initialization. - */ - @Configuration(proxyBeanMethods = false) - static class QuartzSchedulerDependencyConfiguration { - - @Bean - static SchedulerDependsOnBeanFactoryPostProcessor quartzSchedulerDataSourceInitializerDependsOnBeanFactoryPostProcessor() { - return new SchedulerDependsOnBeanFactoryPostProcessor(QuartzDataSourceInitializer.class); - } - - @Bean - @ConditionalOnBean(FlywayMigrationInitializer.class) - static SchedulerDependsOnBeanFactoryPostProcessor quartzSchedulerFlywayDependsOnBeanFactoryPostProcessor() { - return new SchedulerDependsOnBeanFactoryPostProcessor(FlywayMigrationInitializer.class); - } - - @Configuration(proxyBeanMethods = false) - @ConditionalOnClass(SpringLiquibase.class) - static class LiquibaseQuartzSchedulerDependencyConfiguration { - - @Bean - @ConditionalOnBean(SpringLiquibase.class) - static SchedulerDependsOnBeanFactoryPostProcessor quartzSchedulerLiquibaseDependsOnBeanFactoryPostProcessor() { - return new SchedulerDependsOnBeanFactoryPostProcessor(SpringLiquibase.class); - } - - } - - } - - } - - /** - * {@link AbstractDependsOnBeanFactoryPostProcessor} for Quartz {@link Scheduler} and - * {@link SchedulerFactoryBean}. - */ - private static class SchedulerDependsOnBeanFactoryPostProcessor extends AbstractDependsOnBeanFactoryPostProcessor { - - SchedulerDependsOnBeanFactoryPostProcessor(Class... dependencyTypes) { - super(Scheduler.class, SchedulerFactoryBean.class, dependencyTypes); - } - } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/SchedulerDependsOnDatabaseInitializationDetector.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/SchedulerDependsOnDatabaseInitializationDetector.java new file mode 100644 index 0000000000..9bdc02cc18 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/SchedulerDependsOnDatabaseInitializationDetector.java @@ -0,0 +1,43 @@ +/* + * Copyright 2012-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.quartz; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import org.quartz.Scheduler; + +import org.springframework.boot.sql.init.dependency.AbstractBeansOfTypeDependsOnDatabaseInitializationDetector; +import org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector; +import org.springframework.scheduling.quartz.SchedulerFactoryBean; + +/** + * A {@link DependsOnDatabaseInitializationDetector} for Quartz {@link Scheduler} and + * {@link SchedulerFactoryBean}. + * + * @author Andy Wilkinson + */ +class SchedulerDependsOnDatabaseInitializationDetector + extends AbstractBeansOfTypeDependsOnDatabaseInitializationDetector { + + @Override + protected Set> getDependsOnDatabaseInitializationBeanTypes() { + return new HashSet<>(Arrays.asList(Scheduler.class, SchedulerFactoryBean.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 22f1deac6e..33b91643fd 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 @@ -180,4 +180,5 @@ org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializerDatabase # Depends on database initialization detectors org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector=\ org.springframework.boot.autoconfigure.batch.JobRepositoryDependsOnDatabaseInitializationDetector,\ +org.springframework.boot.autoconfigure.quartz.SchedulerDependsOnDatabaseInitializationDetector,\ org.springframework.boot.autoconfigure.session.JdbcIndexedSessionRepositoryDependsOnDatabaseInitializationDetector