diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessorBean.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessorBean.java index 8094917c394..fdc07d705fc 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessorBean.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessorBean.java @@ -46,20 +46,24 @@ public class SchedulerAccessorBean extends SchedulerAccessor implements BeanFact /** - * Specify the Quartz Scheduler to operate on via its scheduler name in the Spring + * Specify the Quartz {@link Scheduler} to operate on via its scheduler name in the Spring * application context or also in the Quartz {@link org.quartz.impl.SchedulerRepository}. *

Schedulers can be registered in the repository through custom bootstrapping, * e.g. via the {@link org.quartz.impl.StdSchedulerFactory} or * {@link org.quartz.impl.DirectSchedulerFactory} factory classes. * However, in general, it's preferable to use Spring's {@link SchedulerFactoryBean} * which includes the job/trigger/listener capabilities of this accessor as well. + *

If not specified, this accessor will try to retrieve a default {@link Scheduler} + * bean from the containing application context. */ public void setSchedulerName(String schedulerName) { this.schedulerName = schedulerName; } /** - * Specify the Quartz Scheduler instance to operate on. + * Specify the Quartz {@link Scheduler} instance to operate on. + *

If not specified, this accessor will try to retrieve a default {@link Scheduler} + * bean from the containing application context. */ public void setScheduler(Scheduler scheduler) { this.scheduler = scheduler; @@ -82,12 +86,7 @@ public class SchedulerAccessorBean extends SchedulerAccessor implements BeanFact @Override public void afterPropertiesSet() throws SchedulerException { if (this.scheduler == null) { - if (this.schedulerName != null) { - this.scheduler = findScheduler(this.schedulerName); - } - else { - throw new IllegalStateException("No Scheduler specified"); - } + this.scheduler = (this.schedulerName != null ? findScheduler(this.schedulerName) : findDefaultScheduler()); } registerListeners(); registerJobsAndTriggers(); @@ -111,4 +110,14 @@ public class SchedulerAccessorBean extends SchedulerAccessor implements BeanFact return schedulerInRepo; } + protected Scheduler findDefaultScheduler() { + if (this.beanFactory != null) { + return this.beanFactory.getBean(Scheduler.class); + } + else { + throw new IllegalStateException( + "No Scheduler specified, and cannot find a default Scheduler without a BeanFactory"); + } + } + } diff --git a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerAccessorBean.xml b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerAccessorBean.xml index a7b1bf8cb12..3f85a53524b 100644 --- a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerAccessorBean.xml +++ b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerAccessorBean.xml @@ -6,7 +6,6 @@ - diff --git a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerRepositoryExposure.xml b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerRepositoryExposure.xml index 14fd5725c25..3305cc660be 100644 --- a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerRepositoryExposure.xml +++ b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/schedulerRepositoryExposure.xml @@ -8,6 +8,10 @@ + + + +