Skip to content

Commit eeac150

Browse files
committed
Polish contribution
See gh-28616
1 parent 0ce9516 commit eeac150

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -585,8 +585,9 @@ private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSi
585585
if (!matchFound) {
586586
// In case of FactoryBean, try to match FactoryBean instance itself next.
587587
beanName = FACTORY_BEAN_PREFIX + beanName;
588-
matchFound = (includeNonSingletons || isSingleton(beanName, mbd, dbd)) && isTypeMatch(beanName, type, allowFactoryBeanInit);
589-
588+
if (includeNonSingletons || isSingleton(beanName, mbd, dbd)) {
589+
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
590+
}
590591
}
591592
}
592593
if (matchFound) {

spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,22 @@ void getBeanNamesForTypeAfterFactoryBeanCreation() {
18591859
assertBeanNamesForType(FactoryBean.class, false, false, "&factoryBean");
18601860
}
18611861

1862+
@Test // gh-28616
1863+
void getBeanNamesForTypeWithPrototypeScopedFactoryBean() {
1864+
FactoryBeanThatShouldntBeCalled.instantiated = false;
1865+
RootBeanDefinition beanDefinition = new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class);
1866+
beanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
1867+
lbf.registerBeanDefinition("factoryBean", beanDefinition);
1868+
assertThat(FactoryBeanThatShouldntBeCalled.instantiated).isFalse();
1869+
assertThat(lbf.containsSingleton("factoryBean")).isFalse();
1870+
1871+
// We should not find any beans of the following types if the FactoryBean itself is prototype-scoped.
1872+
assertBeanNamesForType(Runnable.class, false, false);
1873+
assertBeanNamesForType(Callable.class, false, false);
1874+
assertBeanNamesForType(RepositoryFactoryInformation.class, false, false);
1875+
assertBeanNamesForType(FactoryBean.class, false, false);
1876+
}
1877+
18621878
/**
18631879
* Verifies that a dependency on a {@link FactoryBean} can <strong>not</strong>
18641880
* be autowired <em>by name</em>, as &amp; is an illegal character in

0 commit comments

Comments
 (0)