Skip to content

Commit 13a8f90

Browse files
committed
ScheduledAnnotationBeanPostProcessor properly deals with nested proxies
Issue: SPR-16196
1 parent a15975d commit 13a8f90

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import org.apache.commons.logging.Log;
3333
import org.apache.commons.logging.LogFactory;
3434

35+
import org.springframework.aop.framework.AopProxyUtils;
3536
import org.springframework.aop.support.AopUtils;
3637
import org.springframework.beans.factory.BeanFactory;
3738
import org.springframework.beans.factory.BeanFactoryAware;
@@ -311,7 +312,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
311312

312313
@Override
313314
public Object postProcessAfterInitialization(final Object bean, String beanName) {
314-
Class<?> targetClass = AopUtils.getTargetClass(bean);
315+
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
315316
if (!this.nonAnnotatedClasses.contains(targetClass)) {
316317
Map<Method, Set<Scheduled>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
317318
(MethodIntrospector.MetadataLookup<Set<Scheduled>>) method -> {

spring-context/src/test/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessorTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
import org.junit.After;
3434
import org.junit.Test;
3535

36+
import org.springframework.aop.framework.ProxyFactory;
3637
import org.springframework.beans.DirectFieldAccessor;
3738
import org.springframework.beans.factory.BeanCreationException;
3839
import org.springframework.beans.factory.config.BeanDefinition;
@@ -179,6 +180,14 @@ public void severalFixedRatesOnDefaultMethod() {
179180
severalFixedRates(context, processorDefinition, targetDefinition);
180181
}
181182

183+
@Test
184+
public void severalFixedRatesAgainstNestedCglibProxy() {
185+
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
186+
BeanDefinition targetDefinition = new RootBeanDefinition(SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean.class);
187+
targetDefinition.setFactoryMethodName("nestedProxy");
188+
severalFixedRates(context, processorDefinition, targetDefinition);
189+
}
190+
182191
private void severalFixedRates(StaticApplicationContext context,
183192
BeanDefinition processorDefinition, BeanDefinition targetDefinition) {
184193

@@ -631,6 +640,14 @@ static class SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean {
631640
@Scheduled(fixedRate = 4000, initialDelay = 2000)
632641
public void fixedRate() {
633642
}
643+
644+
static SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean nestedProxy() {
645+
ProxyFactory pf1 = new ProxyFactory(new SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean());
646+
pf1.setProxyTargetClass(true);
647+
ProxyFactory pf2 = new ProxyFactory(pf1.getProxy());
648+
pf2.setProxyTargetClass(true);
649+
return (SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean) pf2.getProxy();
650+
}
634651
}
635652

636653

0 commit comments

Comments
 (0)