diff --git a/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyAutowireTests.java b/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyAutowireTests.java index 9886a0a145c..d13676c5e48 100644 --- a/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyAutowireTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyAutowireTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.aop.scope; +import java.util.Arrays; + import org.junit.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; @@ -28,18 +30,21 @@ import static org.springframework.tests.TestResourceUtils.*; /** * @author Mark Fisher * @author Chris Beams + * @author Juergen Hoeller */ -public final class ScopedProxyAutowireTests { +public class ScopedProxyAutowireTests { private static final Class CLASS = ScopedProxyAutowireTests.class; private static final Resource SCOPED_AUTOWIRE_TRUE_CONTEXT = qualifiedResource(CLASS, "scopedAutowireTrue.xml"); private static final Resource SCOPED_AUTOWIRE_FALSE_CONTEXT = qualifiedResource(CLASS, "scopedAutowireFalse.xml"); + @Test public void testScopedProxyInheritsAutowireCandidateFalse() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); 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 unscoped = (TestBean) bf.getBean("unscoped"); assertSame(unscoped, autowired.getChild()); @@ -49,6 +54,7 @@ public final class ScopedProxyAutowireTests { public void testScopedProxyReplacesAutowireCandidateTrue() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); 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 scoped = (TestBean) bf.getBean("scoped"); assertSame(scoped, autowired.getChild()); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 4c5ca7c4962..c85ef3ca0cf 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -418,8 +418,11 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto !requiresEagerInitForType(mbd.getFactoryBeanName()))) { // In case of FactoryBean, match object created by FactoryBean. boolean isFactoryBean = isFactoryBean(beanName, mbd); - boolean matchFound = (allowEagerInit || !isFactoryBean || containsSingleton(beanName)) && - (includeNonSingletons || isSingleton(beanName)) && isTypeMatch(beanName, type); + boolean matchFound = (allowEagerInit || !isFactoryBean || + (mbd.getDecoratedDefinition() != null && !mbd.isLazyInit()) || + containsSingleton(beanName)) && + (includeNonSingletons || isSingleton(beanName)) && + isTypeMatch(beanName, type); if (!matchFound && isFactoryBean) { // In case of FactoryBean, try to match FactoryBean instance itself next. beanName = FACTORY_BEAN_PREFIX + beanName; @@ -1091,7 +1094,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto Map matchingBeans = findAutowireCandidates(beanName, type, descriptor); if (matchingBeans.isEmpty()) { if (descriptor.isRequired()) { - raiseNoMatchingBeanFound(type, descriptor.getResolvableType().toString(), descriptor); + raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor); } return null; } @@ -1454,11 +1457,11 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto * for an unresolvable dependency. */ private void raiseNoMatchingBeanFound( - Class type, String dependencyDescription, DependencyDescriptor descriptor) throws BeansException { + Class type, ResolvableType resolvableType, DependencyDescriptor descriptor) throws BeansException { checkBeanNotOfRequiredType(type, descriptor); - throw new NoSuchBeanDefinitionException(type, dependencyDescription, + throw new NoSuchBeanDefinitionException(resolvableType, "expected at least 1 bean which qualifies as autowire candidate. " + "Dependency annotations: " + ObjectUtils.nullSafeToString(descriptor.getAnnotations())); }