@@ -115,6 +115,8 @@ public class ScheduledAnnotationBeanPostProcessor
115
115
116
116
protected final Log logger = LogFactory .getLog (getClass ());
117
117
118
+ private final ScheduledTaskRegistrar registrar ;
119
+
118
120
@ Nullable
119
121
private Object scheduler ;
120
122
@@ -130,13 +132,30 @@ public class ScheduledAnnotationBeanPostProcessor
130
132
@ Nullable
131
133
private ApplicationContext applicationContext ;
132
134
133
- private final ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar ();
134
-
135
135
private final Set <Class <?>> nonAnnotatedClasses = Collections .newSetFromMap (new ConcurrentHashMap <>(64 ));
136
136
137
137
private final Map <Object , Set <ScheduledTask >> scheduledTasks = new IdentityHashMap <>(16 );
138
138
139
139
140
+ /**
141
+ * Create a default {@code ScheduledAnnotationBeanPostProcessor}.
142
+ */
143
+ public ScheduledAnnotationBeanPostProcessor () {
144
+ this .registrar = new ScheduledTaskRegistrar ();
145
+ }
146
+
147
+ /**
148
+ * Create a {@code ScheduledAnnotationBeanPostProcessor} delegating to the
149
+ * specified {@link ScheduledTaskRegistrar}.
150
+ * @param registrar the ScheduledTaskRegistrar to register @Scheduled tasks on
151
+ * @since 5.1
152
+ */
153
+ public ScheduledAnnotationBeanPostProcessor (ScheduledTaskRegistrar registrar ) {
154
+ Assert .notNull (registrar , "ScheduledTaskRegistrar is required" );
155
+ this .registrar = registrar ;
156
+ }
157
+
158
+
140
159
@ Override
141
160
public int getOrder () {
142
161
return LOWEST_PRECEDENCE ;
@@ -340,13 +359,16 @@ public Object postProcessAfterInitialization(final Object bean, String beanName)
340
359
return bean ;
341
360
}
342
361
362
+ /**
363
+ * Process the given {@code @Scheduled} method declaration on the given bean.
364
+ * @param scheduled the @Scheduled annotation
365
+ * @param method the method that the annotation has been declared on
366
+ * @param bean the target bean instance
367
+ * @see #createRunnable(Object, Method)
368
+ */
343
369
protected void processScheduled (Scheduled scheduled , Method method , Object bean ) {
344
370
try {
345
- Assert .isTrue (method .getParameterCount () == 0 ,
346
- "Only no-arg methods may be annotated with @Scheduled" );
347
-
348
- Method invocableMethod = AopUtils .selectInvocableMethod (method , bean .getClass ());
349
- Runnable runnable = new ScheduledMethodRunnable (bean , invocableMethod );
371
+ Runnable runnable = createRunnable (bean , method );
350
372
boolean processedSchedule = false ;
351
373
String errorMessage =
352
374
"Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required" ;
@@ -470,6 +492,23 @@ protected void processScheduled(Scheduled scheduled, Method method, Object bean)
470
492
}
471
493
}
472
494
495
+ /**
496
+ * Create a {@link Runnable} for the given bean instance,
497
+ * calling the specified scheduled method.
498
+ * <p>The default implementation creates a {@link ScheduledMethodRunnable}.
499
+ * @param target the target bean instance
500
+ * @param method the scheduled method to call
501
+ * @since 5.1
502
+ * @see ScheduledMethodRunnable#ScheduledMethodRunnable(Object, Method)
503
+ */
504
+ protected Runnable createRunnable (Object target , Method method ) {
505
+ Assert .isTrue (method .getParameterCount () == 0 ,
506
+ "Only no-arg methods may be annotated with @Scheduled" );
507
+
508
+ Method invocableMethod = AopUtils .selectInvocableMethod (method , target .getClass ());
509
+ return new ScheduledMethodRunnable (target , invocableMethod );
510
+ }
511
+
473
512
private static long parseDelayAsLong (String value ) throws RuntimeException {
474
513
if (value .length () > 1 && (isP (value .charAt (0 )) || isP (value .charAt (1 )))) {
475
514
return Duration .parse (value ).toMillis ();
0 commit comments