Skip to content

Commit 0479ca6

Browse files
committed
ScheduledAnnotationBeanPostProcessor registers tasks in ContextRefreshedEvent phase (again)
Issue: SPR-12641
1 parent a4fe49e commit 0479ca6

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -39,7 +39,10 @@
3939
import org.springframework.beans.factory.SmartInitializingSingleton;
4040
import org.springframework.beans.factory.config.BeanPostProcessor;
4141
import org.springframework.context.ApplicationContext;
42+
import org.springframework.context.ApplicationContextAware;
43+
import org.springframework.context.ApplicationListener;
4244
import org.springframework.context.EmbeddedValueResolverAware;
45+
import org.springframework.context.event.ContextRefreshedEvent;
4346
import org.springframework.core.Ordered;
4447
import org.springframework.core.annotation.AnnotationUtils;
4548
import org.springframework.scheduling.TaskScheduler;
@@ -80,7 +83,8 @@
8083
* @see org.springframework.scheduling.config.ScheduledTaskRegistrar
8184
*/
8285
public 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

Comments
 (0)