Clarify BeanFactory#containsBean Javadoc

Previously, #containsBean Javadoc advertised that a true return value
indicates that a call to #getBean for the same name would succeed.

This is actually not the case, and has never been.  The semantics
of #containsBean have always been to indicate whether a bean definition
with the given name is definied or a singleton instance with the given
name has been registered.

The Javadoc now reflects this accurately.

Issue: SPR-8690
This commit is contained in:
Chris Beams 2011-09-13 22:09:12 +00:00
parent bc57f72bbb
commit 10be0ef9e7
2 changed files with 25 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2011 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.
@ -181,12 +181,19 @@ public interface BeanFactory {
Object getBean(String name, Object... args) throws BeansException; Object getBean(String name, Object... args) throws BeansException;
/** /**
* Does this bean factory contain a bean with the given name? More specifically, * Does this bean factory contain a bean definition or externally registered singleton
* is {@link #getBean} able to obtain a bean instance for the given name? * instance with the given name?
* <p>Translates aliases back to the corresponding canonical bean name. * <p>If the given name is an alias, it will be translated back to the corresponding
* Will ask the parent factory if the bean cannot be found in this factory instance. * canonical bean name.
* <p>If this factory is hierarchical, will ask any parent factory if the bean cannot
* be found in this factory instance.
* <p>If a bean definition or singleton instance matching the given name is found,
* this method will return {@code true} whether the named bean definition is concrete
* or abstract, lazy or eager, in scope or not. Therefore, note that a {@code true}
* return value from this method does not necessarily indicate that {@link #getBean}
* will be able to obtain an instance for the same name.
* @param name the name of the bean to query * @param name the name of the bean to query
* @return whether a bean with the given name is defined * @return whether a bean with the given name is present
*/ */
boolean containsBean(String name); boolean containsBean(String name);

View File

@ -16,14 +16,17 @@
package org.springframework.beans.factory; package org.springframework.beans.factory;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -74,7 +77,6 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.ConstructorDependenciesBean; import org.springframework.beans.factory.xml.ConstructorDependenciesBean;
import org.springframework.beans.factory.xml.DependenciesBean; import org.springframework.beans.factory.xml.DependenciesBean;
import org.springframework.beans.propertyeditors.CustomNumberEditor; import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultConversionService;
@ -2154,6 +2156,15 @@ public class DefaultListableBeanFactoryTests {
assertEquals("user1", bean.getUserName()); assertEquals("user1", bean.getUserName());
} }
@Test
public void testContainsBeanReturnsTrueEvenForAbstractBeanDefinition() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("abs",
rootBeanDefinition(TestBean.class).setAbstract(true).getBeanDefinition());
assertThat(bf.containsBean("abs"), is(true));
assertThat(bf.containsBean("bogus"), is(false));
}
public static class NoDependencies { public static class NoDependencies {