org.springframework.beans.factory.support
Class StaticListableBeanFactory

java.lang.Object
  extended byorg.springframework.beans.factory.support.StaticListableBeanFactory
All Implemented Interfaces:
BeanFactory, ListableBeanFactory

public class StaticListableBeanFactory
extends java.lang.Object
implements ListableBeanFactory

Static factory that allows to register existing singleton instances programmatically.

Since:
06-Jan-03
Version:
$Id: StaticListableBeanFactory.java,v 1.9 2004/03/18 02:46:08 trisberg Exp $
Author:
Rod Johnson

Constructor Summary
StaticListableBeanFactory()
           
 
Method Summary
 void addBean(java.lang.String name, java.lang.Object bean)
          Add a new singleton bean.
 boolean containsBean(java.lang.String name)
          Does this bean factory contain a bean with the given name?
 boolean containsBeanDefinition(java.lang.String name)
          Check if this bean factory contains a bean definition with the given name.
 java.lang.String[] getAliases(java.lang.String name)
          Return the aliases for the given bean name, if defined.
 java.lang.Object getBean(java.lang.String name)
          Return an instance (possibly shared or independent) of the given bean name.
 java.lang.Object getBean(java.lang.String name, java.lang.Class requiredType)
          Return an instance (possibly shared or independent) of the given bean name.
 int getBeanDefinitionCount()
          Return the number of beans defined in the factory.
 java.lang.String[] getBeanDefinitionNames()
          Return the names of all beans defined in this factory.
 java.lang.String[] getBeanDefinitionNames(java.lang.Class type)
          Return the names of beans matching the given type (including subclasses), judging from the bean definitions.
 java.util.Map getBeansOfType(java.lang.Class type, boolean includePrototypes, boolean includeFactoryBeans)
          Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType() in the case of FactoryBeans.
 boolean isSingleton(java.lang.String name)
          Is this bean a singleton? That is, will getBean() always return the same object?
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StaticListableBeanFactory

public StaticListableBeanFactory()
Method Detail

getBean

public java.lang.Object getBean(java.lang.String name)
                         throws BeansException
Description copied from interface: BeanFactory
Return an instance (possibly shared or independent) of the given bean name. This method allows a bean factory to be used as a replacement for the Singleton or Prototype design pattern.

Note that callers should retain references to returned objects. There is no guarantee that this method will be implemented to be efficient. For example, it may be synchronized, or may need to run an RDBMS query.

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
getBean in interface BeanFactory
Parameters:
name - name of the bean to return
Returns:
the instance of the bean
Throws:
BeansException - if the bean could not be created

getBean

public java.lang.Object getBean(java.lang.String name,
                                java.lang.Class requiredType)
                         throws BeansException
Description copied from interface: BeanFactory
Return an instance (possibly shared or independent) of the given bean name. Provides a measure of type safety by throwing an exception if the bean is not of the required type.

Note that callers should retain references to returned objects. There is no guarantee that this method will be implemented to be efficient. For example, it may be synchronized, or may need to run an RDBMS query.

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
getBean in interface BeanFactory
Parameters:
name - name of the bean to return
requiredType - type the bean may match. Can be an interface or superclass of the actual class. For example, if the value is Object.class, this method will succeed whatever the class of the returned instance.
Returns:
the instance of the bean
Throws:
BeansException - if the bean could not be created

containsBean

public boolean containsBean(java.lang.String name)
Description copied from interface: BeanFactory
Does this bean factory contain a bean with the given name?

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
containsBean in interface BeanFactory
Parameters:
name - name of the bean to query
Returns:
whether a bean with the given name is defined

isSingleton

public boolean isSingleton(java.lang.String name)
                    throws NoSuchBeanDefinitionException
Description copied from interface: BeanFactory
Is this bean a singleton? That is, will getBean() always return the same object?

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
isSingleton in interface BeanFactory
Parameters:
name - name of the bean to query
Returns:
is this bean a singleton
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name

getAliases

public java.lang.String[] getAliases(java.lang.String name)
Description copied from interface: BeanFactory
Return the aliases for the given bean name, if defined.

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
getAliases in interface BeanFactory
Parameters:
name - the bean name to check for aliases
Returns:
the aliases, or an empty array if none

getBeanDefinitionCount

public int getBeanDefinitionCount()
Description copied from interface: ListableBeanFactory
Return the number of beans defined in the factory. Does not consider any hierarchy this factory may participate in.

Note: Ignores any singleton beans that have been registered by other means than bean definitions.

Specified by:
getBeanDefinitionCount in interface ListableBeanFactory
Returns:
the number of beans defined in the factory

getBeanDefinitionNames

public java.lang.String[] getBeanDefinitionNames()
Description copied from interface: ListableBeanFactory
Return the names of all beans defined in this factory. Does not consider any hierarchy this factory may participate in.

Note: Ignores any singleton beans that have been registered by other means than bean definitions.

Specified by:
getBeanDefinitionNames in interface ListableBeanFactory
Returns:
the names of all beans defined in this factory, or an empty array if none defined

getBeanDefinitionNames

public java.lang.String[] getBeanDefinitionNames(java.lang.Class type)
Description copied from interface: ListableBeanFactory
Return the names of beans matching the given type (including subclasses), judging from the bean definitions. Will not consider FactoryBeans, as the type of their created objects is not known before instantiation. Does not consider any hierarchy this factory may participate in.

Note: Ignores any singleton beans that have been registered by other means than bean definitions.

Specified by:
getBeanDefinitionNames in interface ListableBeanFactory
Parameters:
type - class or interface to match, or null for all bean names
Returns:
the names of beans matching the given object type (including subclasses), or an empty array if none

containsBeanDefinition

public boolean containsBeanDefinition(java.lang.String name)
Description copied from interface: ListableBeanFactory
Check if this bean factory contains a bean definition with the given name. Does not consider any hierarchy this factory may participate in.

Note: Ignores any singleton beans that have been registered by other means than bean definitions.

Specified by:
containsBeanDefinition in interface ListableBeanFactory
Parameters:
name - the name of the bean to look for
Returns:
if this bean factory contains a bean definition with the given name

getBeansOfType

public java.util.Map getBeansOfType(java.lang.Class type,
                                    boolean includePrototypes,
                                    boolean includeFactoryBeans)
Description copied from interface: ListableBeanFactory
Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType() in the case of FactoryBeans. Does not consider any hierarchy this factory may participate in.

If FactoryBean's getObjectType() returns null and the bean is a singleton, the type of the actually created objects should be evaluated. Prototypes without explicit object type specification should be ignored.

Note: Ignores any singleton beans that have been registered by other means than bean definitions.

Specified by:
getBeansOfType in interface ListableBeanFactory
Parameters:
type - class or interface to match
includePrototypes - whether to include prototype beans too or just singletons (also applies to FactoryBeans)
includeFactoryBeans - whether to include FactoryBeans too or just normal beans
Returns:
a Map with the matching beans, containing the bean names as keys and the corresponding bean instances as values

addBean

public void addBean(java.lang.String name,
                    java.lang.Object bean)
Add a new singleton bean.



Copyright (C) 2003-2004 The Spring Framework Project.