Skip to content

Commit 997fb5f

Browse files
committed
DefaultListableBeanFactory allows early type matching against ScopedProxyFactoryBean
Issue: SPR-14816 (cherry picked from commit 3633244)
1 parent 2b45988 commit 997fb5f

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyAutowireTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.aop.scope;
1818

19+
import java.util.Arrays;
20+
1921
import org.junit.Test;
2022

2123
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -28,18 +30,21 @@
2830
/**
2931
* @author Mark Fisher
3032
* @author Chris Beams
33+
* @author Juergen Hoeller
3134
*/
32-
public final class ScopedProxyAutowireTests {
35+
public class ScopedProxyAutowireTests {
3336

3437
private static final Class<?> CLASS = ScopedProxyAutowireTests.class;
3538

3639
private static final Resource SCOPED_AUTOWIRE_TRUE_CONTEXT = qualifiedResource(CLASS, "scopedAutowireTrue.xml");
3740
private static final Resource SCOPED_AUTOWIRE_FALSE_CONTEXT = qualifiedResource(CLASS, "scopedAutowireFalse.xml");
3841

42+
3943
@Test
4044
public void testScopedProxyInheritsAutowireCandidateFalse() {
4145
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
4246
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(SCOPED_AUTOWIRE_FALSE_CONTEXT);
47+
assertTrue(Arrays.asList(bf.getBeanNamesForType(TestBean.class, false, false)).contains("scoped"));
4348
TestBean autowired = (TestBean) bf.getBean("autowired");
4449
TestBean unscoped = (TestBean) bf.getBean("unscoped");
4550
assertSame(unscoped, autowired.getChild());
@@ -49,6 +54,7 @@ public void testScopedProxyInheritsAutowireCandidateFalse() {
4954
public void testScopedProxyReplacesAutowireCandidateTrue() {
5055
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
5156
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(SCOPED_AUTOWIRE_TRUE_CONTEXT);
57+
assertTrue(Arrays.asList(bf.getBeanNamesForType(TestBean.class, false, false)).contains("scoped"));
5258
TestBean autowired = (TestBean) bf.getBean("autowired");
5359
TestBean scoped = (TestBean) bf.getBean("scoped");
5460
assertSame(scoped, autowired.getChild());

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,11 @@ private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSi
418418
!requiresEagerInitForType(mbd.getFactoryBeanName()))) {
419419
// In case of FactoryBean, match object created by FactoryBean.
420420
boolean isFactoryBean = isFactoryBean(beanName, mbd);
421-
boolean matchFound = (allowEagerInit || !isFactoryBean || containsSingleton(beanName)) &&
422-
(includeNonSingletons || isSingleton(beanName)) && isTypeMatch(beanName, type);
421+
boolean matchFound = (allowEagerInit || !isFactoryBean ||
422+
(mbd.getDecoratedDefinition() != null && !mbd.isLazyInit()) ||
423+
containsSingleton(beanName)) &&
424+
(includeNonSingletons || isSingleton(beanName)) &&
425+
isTypeMatch(beanName, type);
423426
if (!matchFound && isFactoryBean) {
424427
// In case of FactoryBean, try to match FactoryBean instance itself next.
425428
beanName = FACTORY_BEAN_PREFIX + beanName;
@@ -1091,7 +1094,7 @@ public Object doResolveDependency(DependencyDescriptor descriptor, String beanNa
10911094
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor);
10921095
if (matchingBeans.isEmpty()) {
10931096
if (descriptor.isRequired()) {
1094-
raiseNoMatchingBeanFound(type, descriptor.getResolvableType().toString(), descriptor);
1097+
raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor);
10951098
}
10961099
return null;
10971100
}
@@ -1454,11 +1457,11 @@ private boolean isSelfReference(String beanName, String candidateName) {
14541457
* for an unresolvable dependency.
14551458
*/
14561459
private void raiseNoMatchingBeanFound(
1457-
Class<?> type, String dependencyDescription, DependencyDescriptor descriptor) throws BeansException {
1460+
Class<?> type, ResolvableType resolvableType, DependencyDescriptor descriptor) throws BeansException {
14581461

14591462
checkBeanNotOfRequiredType(type, descriptor);
14601463

1461-
throw new NoSuchBeanDefinitionException(type, dependencyDescription,
1464+
throw new NoSuchBeanDefinitionException(resolvableType,
14621465
"expected at least 1 bean which qualifies as autowire candidate. " +
14631466
"Dependency annotations: " + ObjectUtils.nullSafeToString(descriptor.getAnnotations()));
14641467
}

0 commit comments

Comments
 (0)