Browse Source

Continue with pre-instantiation when current bean is in creation already

Closes gh-34349
pull/34362/head
Juergen Hoeller 11 months ago
parent
commit
323e52b5a9
  1. 9
      spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
  2. 48
      spring-context/src/test/java/org/springframework/context/annotation/BackgroundBootstrapTests.java

9
spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

@ -1089,8 +1089,15 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
"without bootstrap executor configured - falling back to mainline initialization"); "without bootstrap executor configured - falling back to mainline initialization");
} }
} }
if (!mbd.isLazyInit()) { if (!mbd.isLazyInit()) {
instantiateSingleton(beanName); try {
instantiateSingleton(beanName);
}
catch (BeanCurrentlyInCreationException ex) {
logger.info("Bean '" + beanName + "' marked for pre-instantiation (not lazy-init) " +
"but currently initialized by other thread - skipping it in mainline thread");
}
} }
return null; return null;
} }

48
spring-context/src/test/java/org/springframework/context/annotation/BackgroundBootstrapTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,11 +19,14 @@ package org.springframework.context.annotation;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout; import org.junit.jupiter.api.Timeout;
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.testfixture.EnabledForTestGroups; import org.springframework.core.testfixture.EnabledForTestGroups;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.context.annotation.Bean.Bootstrap.BACKGROUND; import static org.springframework.context.annotation.Bean.Bootstrap.BACKGROUND;
import static org.springframework.core.testfixture.TestGroup.LONG_RUNNING; import static org.springframework.core.testfixture.TestGroup.LONG_RUNNING;
@ -33,6 +36,17 @@ import static org.springframework.core.testfixture.TestGroup.LONG_RUNNING;
*/ */
class BackgroundBootstrapTests { class BackgroundBootstrapTests {
@Test
@Timeout(5)
@EnabledForTestGroups(LONG_RUNNING)
void bootstrapWithUnmanagedThread() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(UnmanagedThreadBeanConfig.class);
ctx.getBean("testBean1", TestBean.class);
assertThatExceptionOfType(BeanCurrentlyInCreationException.class).isThrownBy( // late - not during refresh
() -> ctx.getBean("testBean2", TestBean.class));
ctx.close();
}
@Test @Test
@Timeout(5) @Timeout(5)
@EnabledForTestGroups(LONG_RUNNING) @EnabledForTestGroups(LONG_RUNNING)
@ -45,7 +59,35 @@ class BackgroundBootstrapTests {
} }
@Configuration @Configuration(proxyBeanMethods = false)
static class UnmanagedThreadBeanConfig {
@Bean
public TestBean testBean1(ObjectProvider<TestBean> testBean2) {
new Thread(testBean2::getObject).start();
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
return new TestBean();
}
@Bean
public TestBean testBean2() {
try {
Thread.sleep(2000);
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
return new TestBean();
}
}
@Configuration(proxyBeanMethods = false)
static class CustomExecutorBeanConfig { static class CustomExecutorBeanConfig {
@Bean @Bean
@ -58,7 +100,7 @@ class BackgroundBootstrapTests {
} }
@Bean(bootstrap = BACKGROUND) @DependsOn("testBean3") @Bean(bootstrap = BACKGROUND) @DependsOn("testBean3")
public TestBean testBean1(TestBean testBean3) throws InterruptedException{ public TestBean testBean1(TestBean testBean3) throws InterruptedException {
Thread.sleep(3000); Thread.sleep(3000);
return new TestBean(); return new TestBean();
} }

Loading…
Cancel
Save