17
17
package org .springframework .scheduling .annotation ;
18
18
19
19
import java .lang .reflect .Method ;
20
+ import java .util .Collections ;
20
21
import java .util .HashMap ;
22
+ import java .util .LinkedHashSet ;
21
23
import java .util .Map ;
24
+ import java .util .Set ;
22
25
import java .util .TimeZone ;
26
+ import java .util .concurrent .ConcurrentHashMap ;
23
27
import java .util .concurrent .ScheduledExecutorService ;
24
28
25
29
import org .springframework .aop .support .AopUtils ;
@@ -81,6 +85,9 @@ public class ScheduledAnnotationBeanPostProcessor
81
85
82
86
private final ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar ();
83
87
88
+ private final Set <Class <?>> nonAnnotatedClasses =
89
+ Collections .newSetFromMap (new ConcurrentHashMap <Class <?>, Boolean >(64 ));
90
+
84
91
85
92
@ Override
86
93
public int getOrder () {
@@ -134,12 +141,11 @@ else if (schedulers.size() == 1) {
134
141
this .registrar .setScheduler (schedulers .values ().iterator ().next ());
135
142
}
136
143
else if (schedulers .size () >= 2 ){
137
- throw new IllegalStateException (
138
- "More than one TaskScheduler and/or ScheduledExecutorService " +
139
- "exist within the context. Remove all but one of the beans; or " +
140
- "implement the SchedulingConfigurer interface and call " +
141
- "ScheduledTaskRegistrar#setScheduler explicitly within the " +
142
- "configureTasks() callback. Found the following beans: " + schedulers .keySet ());
144
+ throw new IllegalStateException ("More than one TaskScheduler and/or ScheduledExecutorService " +
145
+ "exist within the context. Remove all but one of the beans; or implement the " +
146
+ "SchedulingConfigurer interface and call ScheduledTaskRegistrar#setScheduler " +
147
+ "explicitly within the configureTasks() callback. Found the following beans: " +
148
+ schedulers .keySet ());
143
149
}
144
150
}
145
151
@@ -154,15 +160,23 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
154
160
155
161
@ Override
156
162
public Object postProcessAfterInitialization (final Object bean , String beanName ) {
157
- Class <?> targetClass = AopUtils .getTargetClass (bean );
158
- ReflectionUtils .doWithMethods (targetClass , new MethodCallback () {
159
- @ Override
160
- public void doWith (Method method ) throws IllegalArgumentException , IllegalAccessException {
161
- for (Scheduled scheduled : AnnotationUtils .getRepeatableAnnotation (method , Schedules .class , Scheduled .class )) {
162
- processScheduled (scheduled , method , bean );
163
+ if (!this .nonAnnotatedClasses .contains (bean .getClass ())) {
164
+ final Set <Method > annotatedMethods = new LinkedHashSet <Method >(1 );
165
+ Class <?> targetClass = AopUtils .getTargetClass (bean );
166
+ ReflectionUtils .doWithMethods (targetClass , new MethodCallback () {
167
+ @ Override
168
+ public void doWith (Method method ) throws IllegalArgumentException , IllegalAccessException {
169
+ for (Scheduled scheduled :
170
+ AnnotationUtils .getRepeatableAnnotation (method , Schedules .class , Scheduled .class )) {
171
+ processScheduled (scheduled , method , bean );
172
+ annotatedMethods .add (method );
173
+ }
163
174
}
175
+ });
176
+ if (annotatedMethods .isEmpty ()) {
177
+ this .nonAnnotatedClasses .add (bean .getClass ());
164
178
}
165
- });
179
+ }
166
180
return bean ;
167
181
}
168
182
0 commit comments