11/*
2- * Copyright 2002-2014 the original author or authors.
2+ * Copyright 2002-2015 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
3939import org .springframework .beans .factory .SmartInitializingSingleton ;
4040import org .springframework .beans .factory .config .BeanPostProcessor ;
4141import org .springframework .context .ApplicationContext ;
42+ import org .springframework .context .ApplicationContextAware ;
43+ import org .springframework .context .ApplicationListener ;
4244import org .springframework .context .EmbeddedValueResolverAware ;
45+ import org .springframework .context .event .ContextRefreshedEvent ;
4346import org .springframework .core .Ordered ;
4447import org .springframework .core .annotation .AnnotationUtils ;
4548import org .springframework .scheduling .TaskScheduler ;
8083 * @see org.springframework.scheduling.config.ScheduledTaskRegistrar
8184 */
8285public class ScheduledAnnotationBeanPostProcessor implements BeanPostProcessor , Ordered ,
83- EmbeddedValueResolverAware , BeanFactoryAware , SmartInitializingSingleton , DisposableBean {
86+ EmbeddedValueResolverAware , BeanFactoryAware , ApplicationContextAware ,
87+ SmartInitializingSingleton , ApplicationListener <ContextRefreshedEvent >, DisposableBean {
8488
8589 protected final Log logger = LogFactory .getLog (getClass ());
8690
@@ -90,6 +94,8 @@ public class ScheduledAnnotationBeanPostProcessor implements BeanPostProcessor,
9094
9195 private BeanFactory beanFactory ;
9296
97+ private ApplicationContext applicationContext ;
98+
9399 private final ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar ();
94100
95101 private final Set <Class <?>> nonAnnotatedClasses =
@@ -126,10 +132,13 @@ public void setBeanFactory(BeanFactory beanFactory) {
126132 }
127133
128134 /**
129- * @deprecated as of Spring 4.1, in favor of {@link #setBeanFactory}
135+ * Setting an {@link ApplicationContext} is optional: If set, registered
136+ * tasks will be activated in the {@link ContextRefreshedEvent} phase;
137+ * if not set, it will happen at {@link #afterSingletonsInstantiated} time.
130138 */
131- @ Deprecated
139+ @ Override
132140 public void setApplicationContext (ApplicationContext applicationContext ) {
141+ this .applicationContext = applicationContext ;
133142 if (this .beanFactory == null ) {
134143 this .beanFactory = applicationContext ;
135144 }
@@ -138,6 +147,23 @@ public void setApplicationContext(ApplicationContext applicationContext) {
138147
139148 @ Override
140149 public void afterSingletonsInstantiated () {
150+ if (this .applicationContext == null ) {
151+ // Not running in an ApplicationContext -> register tasks early...
152+ finishRegistration ();
153+ }
154+ }
155+
156+ @ Override
157+ public void onApplicationEvent (ContextRefreshedEvent event ) {
158+ if (event .getApplicationContext () == this .applicationContext ) {
159+ // Running in an ApplicationContext -> register tasks this late...
160+ // giving other ContextRefreshedEvent listeners a chance to perform
161+ // their work at the same time (e.g. Spring Batch's job registration).
162+ finishRegistration ();
163+ }
164+ }
165+
166+ private void finishRegistration () {
141167 if (this .scheduler != null ) {
142168 this .registrar .setScheduler (this .scheduler );
143169 }
0 commit comments