DefaultListableBeanFactory allows early type matching against ScopedProxyFactoryBean

Issue: SPR-14816
This commit is contained in:
Juergen Hoeller 2016-10-28 23:37:58 +02:00
parent c946924431
commit 36332441ae
2 changed files with 12 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2016 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.
@ -16,6 +16,8 @@
package org.springframework.aop.scope; package org.springframework.aop.scope;
import java.util.Arrays;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@ -28,6 +30,7 @@ import static org.springframework.tests.TestResourceUtils.*;
/** /**
* @author Mark Fisher * @author Mark Fisher
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller
*/ */
public class ScopedProxyAutowireTests { public class ScopedProxyAutowireTests {
@ -36,10 +39,12 @@ public class ScopedProxyAutowireTests {
private static final Resource SCOPED_AUTOWIRE_TRUE_CONTEXT = qualifiedResource(CLASS, "scopedAutowireTrue.xml"); private static final Resource SCOPED_AUTOWIRE_TRUE_CONTEXT = qualifiedResource(CLASS, "scopedAutowireTrue.xml");
private static final Resource SCOPED_AUTOWIRE_FALSE_CONTEXT = qualifiedResource(CLASS, "scopedAutowireFalse.xml"); private static final Resource SCOPED_AUTOWIRE_FALSE_CONTEXT = qualifiedResource(CLASS, "scopedAutowireFalse.xml");
@Test @Test
public void testScopedProxyInheritsAutowireCandidateFalse() { public void testScopedProxyInheritsAutowireCandidateFalse() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(SCOPED_AUTOWIRE_FALSE_CONTEXT); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(SCOPED_AUTOWIRE_FALSE_CONTEXT);
assertTrue(Arrays.asList(bf.getBeanNamesForType(TestBean.class, false, false)).contains("scoped"));
TestBean autowired = (TestBean) bf.getBean("autowired"); TestBean autowired = (TestBean) bf.getBean("autowired");
TestBean unscoped = (TestBean) bf.getBean("unscoped"); TestBean unscoped = (TestBean) bf.getBean("unscoped");
assertSame(unscoped, autowired.getChild()); assertSame(unscoped, autowired.getChild());
@ -49,6 +54,7 @@ public class ScopedProxyAutowireTests {
public void testScopedProxyReplacesAutowireCandidateTrue() { public void testScopedProxyReplacesAutowireCandidateTrue() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(SCOPED_AUTOWIRE_TRUE_CONTEXT); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(SCOPED_AUTOWIRE_TRUE_CONTEXT);
assertTrue(Arrays.asList(bf.getBeanNamesForType(TestBean.class, false, false)).contains("scoped"));
TestBean autowired = (TestBean) bf.getBean("autowired"); TestBean autowired = (TestBean) bf.getBean("autowired");
TestBean scoped = (TestBean) bf.getBean("scoped"); TestBean scoped = (TestBean) bf.getBean("scoped");
assertSame(scoped, autowired.getChild()); assertSame(scoped, autowired.getChild());

View File

@ -408,8 +408,11 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
!requiresEagerInitForType(mbd.getFactoryBeanName()))) { !requiresEagerInitForType(mbd.getFactoryBeanName()))) {
// In case of FactoryBean, match object created by FactoryBean. // In case of FactoryBean, match object created by FactoryBean.
boolean isFactoryBean = isFactoryBean(beanName, mbd); boolean isFactoryBean = isFactoryBean(beanName, mbd);
boolean matchFound = (allowEagerInit || !isFactoryBean || containsSingleton(beanName)) && boolean matchFound = (allowEagerInit || !isFactoryBean ||
(includeNonSingletons || isSingleton(beanName)) && isTypeMatch(beanName, type); (mbd.getDecoratedDefinition() != null && !mbd.isLazyInit()) ||
containsSingleton(beanName)) &&
(includeNonSingletons || isSingleton(beanName)) &&
isTypeMatch(beanName, type);
if (!matchFound && isFactoryBean) { if (!matchFound && isFactoryBean) {
// In case of FactoryBean, try to match FactoryBean instance itself next. // In case of FactoryBean, try to match FactoryBean instance itself next.
beanName = FACTORY_BEAN_PREFIX + beanName; beanName = FACTORY_BEAN_PREFIX + beanName;