DefaultListableBeanFactory defensively handles BeanDefinition access in getBean(Class)

Issue: SPR-10542
This commit is contained in:
Juergen Hoeller 2014-08-20 10:48:35 +02:00
parent 891335a604
commit 9d3d6d5919
2 changed files with 27 additions and 5 deletions

View File

@ -305,7 +305,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
if (beanNames.length > 1) { if (beanNames.length > 1) {
ArrayList<String> autowireCandidates = new ArrayList<String>(); ArrayList<String> autowireCandidates = new ArrayList<String>();
for (String beanName : beanNames) { for (String beanName : beanNames) {
if (getBeanDefinition(beanName).isAutowireCandidate()) { if (!containsBeanDefinition(beanName) || getBeanDefinition(beanName).isAutowireCandidate()) {
autowireCandidates.add(beanName); autowireCandidates.add(beanName);
} }
} }

View File

@ -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"); * 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.
@ -18,7 +18,11 @@ package org.springframework.context.support;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import static org.junit.Assert.*;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
@ -33,4 +37,22 @@ public class GenericApplicationContextTests {
new GenericApplicationContext(bf).refresh(); 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
}
}
} }