Skip to content

Commit

Permalink
DefaultListableBeanFactory defensively handles BeanDefinition access …
Browse files Browse the repository at this point in the history
…in getBean(Class)

Issue: SPR-10542
  • Loading branch information
jhoeller committed Aug 20, 2014
1 parent 891335a commit 9d3d6d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Expand Up @@ -305,7 +305,7 @@ public <T> T getBean(Class<T> requiredType, Object... args) throws BeansExceptio
if (beanNames.length > 1) {
ArrayList<String> autowireCandidates = new ArrayList<String>();
for (String beanName : beanNames) {
if (getBeanDefinition(beanName).isAutowireCandidate()) {
if (!containsBeanDefinition(beanName) || getBeanDefinition(beanName).isAutowireCandidate()) {
autowireCandidates.add(beanName);
}
}
Expand Down
@@ -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.
Expand All @@ -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
Expand All @@ -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
}
}

}

0 comments on commit 9d3d6d5

Please sign in to comment.