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