ScheduledAnnotationBeanPostProcessor accepts non-void methods as well

Issue: SPR-14175
This commit is contained in:
Juergen Hoeller 2016-04-14 21:59:02 +02:00
parent 999112216d
commit b28d96af7d
2 changed files with 56 additions and 39 deletions

View File

@ -287,8 +287,6 @@ public class ScheduledAnnotationBeanPostProcessor implements BeanPostProcessor,
protected void processScheduled(Scheduled scheduled, Method method, Object bean) {
try {
Assert.isTrue(void.class == method.getReturnType(),
"Only void-returning methods may be annotated with @Scheduled");
Assert.isTrue(method.getParameterTypes().length == 0,
"Only no-arg methods may be annotated with @Scheduled");

View File

@ -539,6 +539,31 @@ public class ScheduledAnnotationBeanPostProcessorTests {
assertEquals(businessHoursCronExpression, task.getExpression());
}
@Test
public void nonVoidReturnType() {
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
BeanDefinition targetDefinition = new RootBeanDefinition(NonVoidReturnTypeTestBean.class);
context.registerBeanDefinition("postProcessor", processorDefinition);
context.registerBeanDefinition("target", targetDefinition);
context.refresh();
Object postProcessor = context.getBean("postProcessor");
Object target = context.getBean("target");
ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
@SuppressWarnings("unchecked")
List<CronTask> cronTasks = (List<CronTask>)
new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
assertEquals(1, cronTasks.size());
CronTask task = cronTasks.get(0);
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
Object targetObject = runnable.getTarget();
Method targetMethod = runnable.getMethod();
assertEquals(target, targetObject);
assertEquals("cron", targetMethod.getName());
assertEquals("0 0 9-17 * * MON-FRI", task.getExpression());
}
@Test(expected = BeanCreationException.class)
public void emptyAnnotation() {
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
@ -557,15 +582,6 @@ public class ScheduledAnnotationBeanPostProcessorTests {
context.refresh();
}
@Test(expected = BeanCreationException.class)
public void nonVoidReturnType() {
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
BeanDefinition targetDefinition = new RootBeanDefinition(NonVoidReturnTypeTestBean.class);
context.registerBeanDefinition("postProcessor", processorDefinition);
context.registerBeanDefinition("target", targetDefinition);
context.refresh();
}
@Test(expected = BeanCreationException.class)
public void nonEmptyParamList() {
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
@ -672,6 +688,15 @@ public class ScheduledAnnotationBeanPostProcessorTests {
}
static class NonVoidReturnTypeTestBean {
@Scheduled(cron = "0 0 9-17 * * MON-FRI")
public String cron() {
return "oops";
}
}
static class EmptyAnnotationTestBean {
@Scheduled
@ -688,15 +713,6 @@ public class ScheduledAnnotationBeanPostProcessorTests {
}
static class NonVoidReturnTypeTestBean {
@Scheduled(fixedRate=3000)
public String invalid() {
return "oops";
}
}
static class NonEmptyParamListTestBean {
@Scheduled(fixedRate = 3000)
@ -728,6 +744,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
long fixedRate() default -1;
}
static class MetaAnnotationFixedRateTestBean {
@EveryFiveSeconds
@ -735,6 +752,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
}
}
static class ComposedAnnotationFixedRateTestBean {
@WaitASec(fixedRate = 5000)
@ -742,6 +760,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
}
}
static class MetaAnnotationCronTestBean {
@Hourly