Browse Source

ScheduledAnnotationBeanPostProcessor avoids needless re-scanning of non-annotated classes

Issue: SPR-12189
(cherry picked from commit 58b22ce)
pull/642/head
Juergen Hoeller 12 years ago
parent
commit
d2e8b7e6ee
  1. 40
      spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java

40
spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java

@ -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;
} }

Loading…
Cancel
Save