From 10be0ef9e763d368a24a7ac466da50a2224c5d41 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Tue, 13 Sep 2011 22:09:12 +0000 Subject: [PATCH] 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 --- .../beans/factory/BeanFactory.java | 19 +++++++++++++------ .../DefaultListableBeanFactoryTests.java | 13 ++++++++++++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/BeanFactory.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/BeanFactory.java index 4e00489b1c9..58bba15a4f0 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/BeanFactory.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/BeanFactory.java @@ -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"); * 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; /** - * Does this bean factory contain a bean with the given name? More specifically, - * is {@link #getBean} able to obtain a bean instance for the given name? - *

Translates aliases back to the corresponding canonical bean name. - * Will ask the parent factory if the bean cannot be found in this factory instance. + * Does this bean factory contain a bean definition or externally registered singleton + * instance with the given name? + *

If the given name is an alias, it will be translated back to the corresponding + * canonical bean name. + *

If this factory is hierarchical, will ask any parent factory if the bean cannot + * be found in this factory instance. + *

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 - * @return whether a bean with the given name is defined + * @return whether a bean with the given name is present */ boolean containsBean(String name); diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index 31e92fa1f11..25d07a81015 100644 --- a/org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/org.springframework.beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -16,14 +16,17 @@ package org.springframework.beans.factory; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; import java.lang.reflect.Field; 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.DependenciesBean; import org.springframework.beans.propertyeditors.CustomNumberEditor; -import org.springframework.core.LocalVariableTableParameterNameDiscoverer; import org.springframework.core.MethodParameter; import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.support.DefaultConversionService; @@ -2154,6 +2156,15 @@ public class DefaultListableBeanFactoryTests { 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 {