Defensive singleton check for non-registered bean
Closes gh-33286
This commit is contained in:
parent
5aa38833dd
commit
9d9e621efe
|
|
@ -308,8 +308,9 @@ public class ScheduledAnnotationBeanPostProcessor
|
||||||
logger.trace(annotatedMethods.size() + " @Scheduled methods processed on bean '" + beanName +
|
logger.trace(annotatedMethods.size() + " @Scheduled methods processed on bean '" + beanName +
|
||||||
"': " + annotatedMethods);
|
"': " + annotatedMethods);
|
||||||
}
|
}
|
||||||
if ((this.beanFactory != null && !this.beanFactory.isSingleton(beanName)) ||
|
if ((this.beanFactory != null &&
|
||||||
(this.beanFactory instanceof SingletonBeanRegistry sbr && sbr.containsSingleton(beanName))) {
|
(!this.beanFactory.containsBean(beanName) || !this.beanFactory.isSingleton(beanName)) ||
|
||||||
|
(this.beanFactory instanceof SingletonBeanRegistry sbr && sbr.containsSingleton(beanName)))) {
|
||||||
// Either a prototype/scoped bean or a FactoryBean with a pre-existing managed singleton
|
// Either a prototype/scoped bean or a FactoryBean with a pre-existing managed singleton
|
||||||
// -> trigger manual cancellation when ContextClosedEvent comes in
|
// -> trigger manual cancellation when ContextClosedEvent comes in
|
||||||
this.manualCancellationOnContextClose.add(bean);
|
this.manualCancellationOnContextClose.add(bean);
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
|
@ -294,6 +295,24 @@ class ScheduledAnnotationBeanPostProcessorTests {
|
||||||
assertThat(task.getInitialDelayDuration()).isEqualTo(Duration.ofMillis(2_000L));
|
assertThat(task.getInitialDelayDuration()).isEqualTo(Duration.ofMillis(2_000L));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void oneTimeTaskOnNonRegisteredBean() {
|
||||||
|
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
|
||||||
|
context.registerBeanDefinition("postProcessor", processorDefinition);
|
||||||
|
context.refresh();
|
||||||
|
|
||||||
|
ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
|
||||||
|
assertThat(postProcessor.getScheduledTasks()).hasSize(0);
|
||||||
|
|
||||||
|
Object target = context.getAutowireCapableBeanFactory().createBean(OneTimeTaskBean.class);
|
||||||
|
assertThat(postProcessor.getScheduledTasks()).hasSize(1);
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Set<Object> manualTasks = (Set<Object>)
|
||||||
|
new DirectFieldAccessor(postProcessor).getPropertyValue("manualCancellationOnContextClose");
|
||||||
|
assertThat(manualTasks).hasSize(1);
|
||||||
|
assertThat(manualTasks).contains(target);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void cronTask() {
|
void cronTask() {
|
||||||
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
|
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue