ScheduledAnnotationBeanPostProcessor avoids needless re-scanning of non-annotated classes
Issue: SPR-12189
(cherry picked from commit 58b22ce)
This commit is contained in:
@@ -17,9 +17,13 @@
|
||||
package org.springframework.scheduling.annotation;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
@@ -81,6 +85,9 @@ public class ScheduledAnnotationBeanPostProcessor
|
||||
|
||||
private final ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar();
|
||||
|
||||
private final Set<Class<?>> nonAnnotatedClasses =
|
||||
Collections.newSetFromMap(new ConcurrentHashMap<Class<?>, Boolean>(64));
|
||||
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
@@ -134,12 +141,11 @@ public class ScheduledAnnotationBeanPostProcessor
|
||||
this.registrar.setScheduler(schedulers.values().iterator().next());
|
||||
}
|
||||
else if (schedulers.size() >= 2){
|
||||
throw new IllegalStateException(
|
||||
"More than one TaskScheduler and/or ScheduledExecutorService " +
|
||||
"exist within the context. Remove all but one of the beans; or " +
|
||||
"implement the SchedulingConfigurer interface and call " +
|
||||
"ScheduledTaskRegistrar#setScheduler explicitly within the " +
|
||||
"configureTasks() callback. Found the following beans: " + schedulers.keySet());
|
||||
throw new IllegalStateException("More than one TaskScheduler and/or ScheduledExecutorService " +
|
||||
"exist within the context. Remove all but one of the beans; or implement the " +
|
||||
"SchedulingConfigurer interface and call ScheduledTaskRegistrar#setScheduler " +
|
||||
"explicitly within the configureTasks() callback. Found the following beans: " +
|
||||
schedulers.keySet());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,15 +160,23 @@ public class ScheduledAnnotationBeanPostProcessor
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(final Object bean, String beanName) {
|
||||
Class<?> targetClass = AopUtils.getTargetClass(bean);
|
||||
ReflectionUtils.doWithMethods(targetClass, new MethodCallback() {
|
||||
@Override
|
||||
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
|
||||
for (Scheduled scheduled : AnnotationUtils.getRepeatableAnnotation(method, Schedules.class, Scheduled.class)) {
|
||||
processScheduled(scheduled, method, bean);
|
||||
if (!this.nonAnnotatedClasses.contains(bean.getClass())) {
|
||||
final Set<Method> annotatedMethods = new LinkedHashSet<Method>(1);
|
||||
Class<?> targetClass = AopUtils.getTargetClass(bean);
|
||||
ReflectionUtils.doWithMethods(targetClass, new MethodCallback() {
|
||||
@Override
|
||||
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
|
||||
for (Scheduled scheduled :
|
||||
AnnotationUtils.getRepeatableAnnotation(method, Schedules.class, Scheduled.class)) {
|
||||
processScheduled(scheduled, method, bean);
|
||||
annotatedMethods.add(method);
|
||||
}
|
||||
}
|
||||
});
|
||||
if (annotatedMethods.isEmpty()) {
|
||||
this.nonAnnotatedClasses.add(bean.getClass());
|
||||
}
|
||||
});
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user