@@ -46,20 +46,24 @@ public class SchedulerAccessorBean extends SchedulerAccessor implements BeanFact
4646
4747
4848 /**
49- * Specify the Quartz Scheduler to operate on via its scheduler name in the Spring
49+ * Specify the Quartz {@link Scheduler} to operate on via its scheduler name in the Spring
5050 * application context or also in the Quartz {@link org.quartz.impl.SchedulerRepository}.
5151 * <p>Schedulers can be registered in the repository through custom bootstrapping,
5252 * e.g. via the {@link org.quartz.impl.StdSchedulerFactory} or
5353 * {@link org.quartz.impl.DirectSchedulerFactory} factory classes.
5454 * However, in general, it's preferable to use Spring's {@link SchedulerFactoryBean}
5555 * which includes the job/trigger/listener capabilities of this accessor as well.
56+ * <p>If not specified, this accessor will try to retrieve a default {@link Scheduler}
57+ * bean from the containing application context.
5658 */
5759 public void setSchedulerName (String schedulerName ) {
5860 this .schedulerName = schedulerName ;
5961 }
6062
6163 /**
62- * Specify the Quartz Scheduler instance to operate on.
64+ * Specify the Quartz {@link Scheduler} instance to operate on.
65+ * <p>If not specified, this accessor will try to retrieve a default {@link Scheduler}
66+ * bean from the containing application context.
6367 */
6468 public void setScheduler (Scheduler scheduler ) {
6569 this .scheduler = scheduler ;
@@ -82,12 +86,7 @@ public void setBeanFactory(BeanFactory beanFactory) {
8286 @ Override
8387 public void afterPropertiesSet () throws SchedulerException {
8488 if (this .scheduler == null ) {
85- if (this .schedulerName != null ) {
86- this .scheduler = findScheduler (this .schedulerName );
87- }
88- else {
89- throw new IllegalStateException ("No Scheduler specified" );
90- }
89+ this .scheduler = (this .schedulerName != null ? findScheduler (this .schedulerName ) : findDefaultScheduler ());
9190 }
9291 registerListeners ();
9392 registerJobsAndTriggers ();
@@ -111,4 +110,14 @@ protected Scheduler findScheduler(String schedulerName) throws SchedulerExceptio
111110 return schedulerInRepo ;
112111 }
113112
113+ protected Scheduler findDefaultScheduler () {
114+ if (this .beanFactory != null ) {
115+ return this .beanFactory .getBean (Scheduler .class );
116+ }
117+ else {
118+ throw new IllegalStateException (
119+ "No Scheduler specified, and cannot find a default Scheduler without a BeanFactory" );
120+ }
121+ }
122+
114123}
0 commit comments