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:
parent
bc57f72bbb
commit
10be0ef9e7
|
|
@ -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?
|
||||
* <p>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?
|
||||
* <p>If the given name is an alias, it will be translated back to the corresponding
|
||||
* 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
|
||||
* @return whether a bean with the given name is defined
|
||||
* @return whether a bean with the given name is present
|
||||
*/
|
||||
boolean containsBean(String name);
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue