@@ -87,6 +87,14 @@ public class ScheduledAnnotationBeanPostProcessor implements BeanPostProcessor,
87
87
EmbeddedValueResolverAware , BeanFactoryAware , ApplicationContextAware ,
88
88
SmartInitializingSingleton , ApplicationListener <ContextRefreshedEvent >, DisposableBean {
89
89
90
+ /**
91
+ * The default name of the TaskScheduler bean to pick up: "taskScheduler".
92
+ * <p>Note that the initial lookup happens by type; this is just the fallback
93
+ * in case of multiple scheduler beans found in the context.
94
+ */
95
+ public static final String DEFAULT_TASK_SCHEDULER_BEAN_NAME = "taskScheduler" ;
96
+
97
+
90
98
protected final Log logger = LogFactory .getLog (getClass ());
91
99
92
100
private Object scheduler ;
@@ -181,12 +189,19 @@ private void finishRegistration() {
181
189
Assert .state (this .beanFactory != null , "BeanFactory must be set to find scheduler by type" );
182
190
try {
183
191
// Search for TaskScheduler bean...
184
- this .registrar .setScheduler (this .beanFactory .getBean (TaskScheduler .class ));
192
+ this .registrar .setTaskScheduler (this .beanFactory .getBean (TaskScheduler .class ));
185
193
}
186
194
catch (NoUniqueBeanDefinitionException ex ) {
187
- throw new IllegalStateException ("More than one TaskScheduler exists within the context. " +
188
- "Remove all but one of the beans; or implement the SchedulingConfigurer interface and call " +
189
- "ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback." , ex );
195
+ try {
196
+ this .registrar .setTaskScheduler (
197
+ this .beanFactory .getBean (DEFAULT_TASK_SCHEDULER_BEAN_NAME , TaskScheduler .class ));
198
+ }
199
+ catch (NoSuchBeanDefinitionException ex2 ) {
200
+ throw new IllegalStateException ("More than one TaskScheduler bean exists within the context, and " +
201
+ "none is named 'taskScheduler'. Mark one of them as primary or name it 'taskScheduler' " +
202
+ "(possibly as an alias); or implement the SchedulingConfigurer interface and call " +
203
+ "ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback." , ex );
204
+ }
190
205
}
191
206
catch (NoSuchBeanDefinitionException ex ) {
192
207
logger .debug ("Could not find default TaskScheduler bean" , ex );
@@ -195,9 +210,10 @@ private void finishRegistration() {
195
210
this .registrar .setScheduler (this .beanFactory .getBean (ScheduledExecutorService .class ));
196
211
}
197
212
catch (NoUniqueBeanDefinitionException ex2 ) {
198
- throw new IllegalStateException ("More than one ScheduledExecutorService exists within the context. " +
199
- "Remove all but one of the beans; or implement the SchedulingConfigurer interface and call " +
200
- "ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback." , ex );
213
+ throw new IllegalStateException ("More than one ScheduledExecutorService bean exists within " +
214
+ "the context. Mark one of them as primary; or implement the SchedulingConfigurer " +
215
+ "interface and call ScheduledTaskRegistrar#setScheduler explicitly within the " +
216
+ "configureTasks() callback." , ex );
201
217
}
202
218
catch (NoSuchBeanDefinitionException ex2 ) {
203
219
logger .debug ("Could not find default ScheduledExecutorService bean" , ex );
0 commit comments