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 8685cd40555a..1f14df139e12 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 @@ -305,7 +305,7 @@ public T getBean(Class requiredType, Object... args) throws BeansExceptio if (beanNames.length > 1) { ArrayList autowireCandidates = new ArrayList(); for (String beanName : beanNames) { - if (getBeanDefinition(beanName).isAutowireCandidate()) { + if (!containsBeanDefinition(beanName) || getBeanDefinition(beanName).isAutowireCandidate()) { autowireCandidates.add(beanName); } } diff --git a/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java b/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java index 8587ff38ced1..abfd80b9bb79 100644 --- a/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java +++ b/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2014 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. @@ -18,7 +18,11 @@ import org.junit.Test; +import org.springframework.beans.factory.NoUniqueBeanDefinitionException; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.support.RootBeanDefinition; + +import static org.junit.Assert.*; /** * @author Juergen Hoeller @@ -28,9 +32,27 @@ public class GenericApplicationContextTests { @Test public void nullBeanRegistration() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerSingleton("nullBean", null); - new GenericApplicationContext(bf).refresh(); + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + bf.registerSingleton("nullBean", null); + new GenericApplicationContext(bf).refresh(); + } + + @Test + public void getBeanForClass() { + GenericApplicationContext ac = new GenericApplicationContext(); + ac.registerBeanDefinition("testBean", new RootBeanDefinition(String.class)); + ac.refresh(); + + assertSame(ac.getBean("testBean"), ac.getBean(String.class)); + assertSame(ac.getBean("testBean"), ac.getBean(CharSequence.class)); + + try { + assertSame(ac.getBean("testBean"), ac.getBean(Object.class)); + fail("Should have thrown NoUniqueBeanDefinitionException"); + } + catch (NoUniqueBeanDefinitionException ex) { + // expected + } } }