diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java index 1753e04761..38307f3a5a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java @@ -29,6 +29,7 @@ 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.ConditionalOnClass; @@ -87,14 +88,6 @@ public class QuartzAutoConfiguration { this.applicationContext = applicationContext; } - @Bean - @ConditionalOnSingleCandidate(DataSource.class) - @ConditionalOnMissingBean - public QuartzDatabaseInitializer quartzDatabaseInitializer(DataSource dataSource, - ResourceLoader resourceLoader) { - return new QuartzDatabaseInitializer(dataSource, resourceLoader, this.properties); - } - @Bean @ConditionalOnMissingBean public SchedulerFactoryBean quartzScheduler() { @@ -155,6 +148,28 @@ public class QuartzAutoConfiguration { }; } + @Bean + @ConditionalOnMissingBean + public QuartzDatabaseInitializer quartzDatabaseInitializer(DataSource dataSource, + ResourceLoader resourceLoader, QuartzProperties properties) { + return new QuartzDatabaseInitializer(dataSource, resourceLoader, properties); + } + + @Bean + public static DatabaseInitializerSchedulerDependencyPostProcessor databaseInitializerSchedulerDependencyPostProcessor() { + return new DatabaseInitializerSchedulerDependencyPostProcessor(); + } + + private static class DatabaseInitializerSchedulerDependencyPostProcessor + extends AbstractDependsOnBeanFactoryPostProcessor { + + DatabaseInitializerSchedulerDependencyPostProcessor() { + super(Scheduler.class, SchedulerFactoryBean.class, + "quartzDatabaseInitializer"); + } + + } + } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfigurationTests.java index 06665d1e15..4ff0d41b43 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfigurationTests.java @@ -50,11 +50,13 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import org.springframework.core.env.Environment; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.scheduling.quartz.LocalDataSourceJobStore; import org.springframework.scheduling.quartz.LocalTaskExecutorThreadPool; import org.springframework.scheduling.quartz.QuartzJobBean; +import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -174,7 +176,7 @@ public class QuartzAutoConfigurationTests { @Test public void withCustomizer() throws Exception { - load(QuartzCustomConfig.class); + load(QuartzCustomConfiguration.class); assertThat(this.context.getBeansOfType(Scheduler.class)).hasSize(1); Scheduler scheduler = this.context.getBean(Scheduler.class); assertThat(scheduler.getSchedulerName()).isEqualTo("fooScheduler"); @@ -199,8 +201,14 @@ public class QuartzAutoConfigurationTests { this.context = ctx; } + @Import(ComponentThatUsesScheduler.class) @Configuration - protected static class QuartzJobsConfiguration { + protected static class BaseQuartzConfiguration { + + } + + @Configuration + protected static class QuartzJobsConfiguration extends BaseQuartzConfiguration { @Bean public JobDetail fooJob() { @@ -217,7 +225,7 @@ public class QuartzAutoConfigurationTests { } @Configuration - protected static class QuartzFullConfiguration { + protected static class QuartzFullConfiguration extends BaseQuartzConfiguration { @Bean public JobDetail fooJob() { @@ -237,7 +245,7 @@ public class QuartzAutoConfigurationTests { } @Configuration - protected static class QuartzCalendarsConfiguration { + protected static class QuartzCalendarsConfiguration extends BaseQuartzConfiguration { @Bean public Calendar weekly() { @@ -252,7 +260,7 @@ public class QuartzAutoConfigurationTests { } @Configuration - protected static class QuartzExecutorConfiguration { + protected static class QuartzExecutorConfiguration extends BaseQuartzConfiguration { @Bean public Executor executor() { @@ -262,7 +270,7 @@ public class QuartzAutoConfigurationTests { } @Configuration - protected static class QuartzCustomConfig { + protected static class QuartzCustomConfiguration extends BaseQuartzConfiguration { @Bean public SchedulerFactoryBeanCustomizer customizer() { @@ -272,6 +280,17 @@ public class QuartzAutoConfigurationTests { } + public static class ComponentThatUsesScheduler { + + private Scheduler scheduler; + + public ComponentThatUsesScheduler(Scheduler scheduler) { + Assert.notNull(scheduler, "Scheduler must not be null"); + this.scheduler = scheduler; + } + + } + public static class FooJob extends QuartzJobBean { @Autowired diff --git a/spring-boot-samples/spring-boot-sample-quartz/src/main/java/sample/quartz/SampleQuartzApplication.java b/spring-boot-samples/spring-boot-sample-quartz/src/main/java/sample/quartz/SampleQuartzApplication.java index 0836fa78a4..54f82f1734 100644 --- a/spring-boot-samples/spring-boot-sample-quartz/src/main/java/sample/quartz/SampleQuartzApplication.java +++ b/spring-boot-samples/spring-boot-sample-quartz/src/main/java/sample/quartz/SampleQuartzApplication.java @@ -35,9 +35,8 @@ public class SampleQuartzApplication { @Bean public JobDetail sampleJobDetail() { - return JobBuilder.newJob().ofType(SampleJob.class).withIdentity("sampleJob") - .usingJobData("name", "World") - .storeDurably().build(); + return JobBuilder.newJob(SampleJob.class).withIdentity("sampleJob") + .usingJobData("name", "World").storeDurably().build(); } @Bean