Merge branch '6.2.x'

This commit is contained in:
Juergen Hoeller 2025-04-03 12:04:38 +02:00
commit 18989123ac
2 changed files with 33 additions and 2 deletions

View File

@ -627,10 +627,15 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
}
}
else {
if (includeNonSingletons || isNonLazyDecorated ||
(allowFactoryBeanInit && isSingleton(beanName, mbd, dbd))) {
if (includeNonSingletons || isNonLazyDecorated) {
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
}
else if (allowFactoryBeanInit) {
// Type check before singleton check, avoiding FactoryBean instantiation
// for early FactoryBean.isSingleton() calls on non-matching beans.
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit) &&
isSingleton(beanName, mbd, dbd);
}
if (!matchFound) {
// In case of FactoryBean, try to match FactoryBean instance itself next.
beanName = FACTORY_BEAN_PREFIX + beanName;

View File

@ -263,6 +263,32 @@ class DefaultListableBeanFactoryTests {
assertThat(DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isFalse();
}
@Test
void nonInitializedFactoryBeanIgnoredByEagerTypeMatching() {
RootBeanDefinition bd = new RootBeanDefinition(DummyFactory.class);
bd.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, String.class);
lbf.registerBeanDefinition("x1", bd);
assertBeanNamesForType(TestBean.class, false, true);
assertThat(lbf.getBeanNamesForAnnotation(SuppressWarnings.class)).isEmpty();
assertThat(lbf.containsSingleton("x1")).isFalse();
assertThat(lbf.containsBean("x1")).isTrue();
assertThat(lbf.containsBean("&x1")).isTrue();
assertThat(lbf.isSingleton("x1")).isTrue();
assertThat(lbf.isSingleton("&x1")).isTrue();
assertThat(lbf.isPrototype("x1")).isFalse();
assertThat(lbf.isPrototype("&x1")).isFalse();
assertThat(lbf.isTypeMatch("x1", TestBean.class)).isTrue();
assertThat(lbf.isTypeMatch("&x1", TestBean.class)).isFalse();
assertThat(lbf.isTypeMatch("&x1", DummyFactory.class)).isTrue();
assertThat(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class))).isTrue();
assertThat(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class))).isTrue();
assertThat(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class))).isFalse();
assertThat(lbf.getType("x1")).isEqualTo(TestBean.class);
assertThat(lbf.getType("&x1")).isEqualTo(DummyFactory.class);
}
@Test
void initializedFactoryBeanFoundByNonEagerTypeMatching() {
Properties p = new Properties();