org.springframework.beans.factory
Class BeanFactoryUtils

java.lang.Object
  extended byorg.springframework.beans.factory.BeanFactoryUtils

public abstract class BeanFactoryUtils
extends java.lang.Object

Convenience methods operating on bean factories, returning bean instances, names or counts taking into account the nesting hierarchy of a bean factory.

Since:
04-Jul-2003
Version:
$Id: BeanFactoryUtils.java,v 1.2 2004/03/18 02:46:07 trisberg Exp $
Author:
Rod Johnson, Juergen Hoeller

Constructor Summary
BeanFactoryUtils()
           
 
Method Summary
static java.lang.String[] beanNamesIncludingAncestors(ListableBeanFactory lbf)
          Return all bean names in the factory, including ancestor factories.
static java.lang.String[] beanNamesIncludingAncestors(ListableBeanFactory lbf, java.lang.Class type)
          Get all bean names for the given type, including those defined in ancestor factories.
static java.lang.Object beanOfType(ListableBeanFactory lbf, java.lang.Class type)
          Return a single bean of the given type or subtypes, not looking in ancestor factories.
static java.lang.Object beanOfType(ListableBeanFactory lbf, java.lang.Class type, boolean includePrototypes, boolean includeFactoryBeans)
          Return a single bean of the given type or subtypes, not looking in ancestor factories.
static java.lang.Object beanOfTypeIncludingAncestors(ListableBeanFactory lbf, java.lang.Class type, boolean includePrototypes, boolean includeFactoryBeans)
          Return a single bean of the given type or subtypes, also picking up beans defined in ancestor bean factories if the current bean factory is a HierarchicalBeanFactory.
static java.util.Map beansOfTypeIncludingAncestors(ListableBeanFactory lbf, java.lang.Class type, boolean includePrototypes, boolean includeFactoryBeans)
          Return all beans of the given type or subtypes, also picking up beans defined in ancestor bean factories if the current bean factory is a HierarchicalBeanFactory.
static int countBeansIncludingAncestors(ListableBeanFactory lbf)
          Count all bean definitions in any hierarchy in which this factory participates.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanFactoryUtils

public BeanFactoryUtils()
Method Detail

countBeansIncludingAncestors

public static int countBeansIncludingAncestors(ListableBeanFactory lbf)
Count all bean definitions in any hierarchy in which this factory participates. Includes counts of ancestor bean factories. Beans that are "overridden" (specified in a descendant factory with the same name) are counted only once.

Parameters:
lbf -
Returns:
int count of beans including those defined in ancestor factories

beanNamesIncludingAncestors

public static java.lang.String[] beanNamesIncludingAncestors(ListableBeanFactory lbf)
Return all bean names in the factory, including ancestor factories.

Parameters:
lbf - the bean factory
Returns:
the array of bean names, or an empty array if none

beanNamesIncludingAncestors

public static java.lang.String[] beanNamesIncludingAncestors(ListableBeanFactory lbf,
                                                             java.lang.Class type)
Get all bean names for the given type, including those defined in ancestor factories. Will return unique names in case of overridden bean definitions.

Parameters:
lbf - ListableBeanFactory. If this isn't also a HierarchicalBeanFactory, this method will return the same as it's own getBeanDefinitionNames() method.
type - the type that beans must match
Returns:
the array of bean names, or an empty array if none

beansOfTypeIncludingAncestors

public static java.util.Map beansOfTypeIncludingAncestors(ListableBeanFactory lbf,
                                                          java.lang.Class type,
                                                          boolean includePrototypes,
                                                          boolean includeFactoryBeans)
                                                   throws BeansException
Return all beans of the given type or subtypes, also picking up beans defined in ancestor bean factories if the current bean factory is a HierarchicalBeanFactory. The return list will only contain beans of this type. Useful convenience method when we don't care about bean names.

Parameters:
lbf - the bean factory
type - type of bean 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:
the Map of bean instances, or an empty Map if none
Throws:
BeansException - if the beans could not be created

beanOfTypeIncludingAncestors

public static java.lang.Object beanOfTypeIncludingAncestors(ListableBeanFactory lbf,
                                                            java.lang.Class type,
                                                            boolean includePrototypes,
                                                            boolean includeFactoryBeans)
                                                     throws BeansException
Return a single bean of the given type or subtypes, also picking up beans defined in ancestor bean factories if the current bean factory is a HierarchicalBeanFactory. Useful convenience method when we expect a single bean and don't care about the bean name.

Parameters:
lbf - the bean factory
type - type of bean 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:
the Map of bean instances, or an empty Map if none
Throws:
NoSuchBeanDefinitionException - if 0 or more than 1 beans of the given type were found
BeansException - if the bean could not be created

beanOfType

public static java.lang.Object beanOfType(ListableBeanFactory lbf,
                                          java.lang.Class type,
                                          boolean includePrototypes,
                                          boolean includeFactoryBeans)
                                   throws BeansException
Return a single bean of the given type or subtypes, not looking in ancestor factories. Useful convenience method when we expect a single bean and don't care about the bean name.

Parameters:
lbf - the bean factory
type - type of bean 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:
the Map of bean instances, or an empty Map if none
Throws:
NoSuchBeanDefinitionException - if 0 or more than 1 beans of the given type were found
BeansException - if the bean could not be created

beanOfType

public static java.lang.Object beanOfType(ListableBeanFactory lbf,
                                          java.lang.Class type)
                                   throws BeansException
Return a single bean of the given type or subtypes, not looking in ancestor factories. Useful convenience method when we expect a single bean and don't care about the bean name. This version of beanOfType automatically includes prototypes and FactoryBeans.

Parameters:
lbf - the bean factory
type - type of bean to match
Returns:
the Map of bean instances, or an empty Map if none
Throws:
NoSuchBeanDefinitionException - if 0 or more than 1 beans of the given type were found
BeansException - if the bean could not be created


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