Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2002-2008 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.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.beans.factory;
 
 import java.util.List;
 import java.util.Map;
 
Convenience methods operating on bean factories, in particular on the ListableBeanFactory interface.

Returns bean counts, bean names or bean instances, taking into account the nesting hierarchy of a bean factory (which the methods defined on the ListableBeanFactory interface don't, in contrast to the methods defined on the BeanFactory interface).

Author(s):
Rod Johnson
Juergen Hoeller
Since:
04.07.2003
 
 public abstract class BeanFactoryUtils {

Separator for generated bean names. If a class name or parent name is not unique, "#1", "#2" etc will be appended, until the name becomes unique.
 
 	public static final String GENERATED_BEAN_NAME_SEPARATOR = "#";


Return whether the given name is a factory dereference (beginning with the factory dereference prefix).

Parameters:
name the name of the bean
Returns:
whether the given name is a factory dereference
See also:
BeanFactory.FACTORY_BEAN_PREFIX
 
 	public static boolean isFactoryDereference(String name) {
 		return (name != null && name.startsWith(.));
 	}

Return the actual bean name, stripping out the factory dereference prefix (if any, also stripping repeated factory prefixes if found).

Parameters:
name the name of the bean
Returns:
the transformed name
See also:
BeanFactory.FACTORY_BEAN_PREFIX
 
 	public static String transformedBeanName(String name) {
 		Assert.notNull(name"'name' must not be null");
 		String beanName = name;
 			beanName = beanName.substring(..length());
 		}
 		return beanName;
 	}

Return whether the given name is a bean name which has been generated by the default naming strategy (containing a "#..." part).

 
 	public static boolean isGeneratedBeanName(String name) {
 		return (name != null && name.indexOf() != -1);
 	}

Extract the "raw" bean name from the given (potentially generated) bean name, excluding any "#..." suffixes which might have been added for uniqueness.

Parameters:
name the potentially generated bean name
Returns:
the raw bean name
See also:
GENERATED_BEAN_NAME_SEPARATOR
 
 	public static String originalBeanName(String name) {
		Assert.notNull(name"'name' must not be null");
		int separatorIndex = name.indexOf();
		return (separatorIndex != -1 ? name.substring(0, separatorIndex) : name);
	}


Count all beans 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 only counted once.

Parameters:
lbf the bean factory
Returns:
count of beans including those defined in ancestor factories
		return beanNamesIncludingAncestors(lbf).length;
	}

Return all bean names in the factory, including ancestor factories.

Parameters:
lbf the bean factory
Returns:
the array of matching bean names, or an empty array if none
See also:
beanNamesForTypeIncludingAncestors(org.springframework.beans.factory.ListableBeanFactory,java.lang.Class)
	}


Get all bean names for the given type, including those defined in ancestor factories. Will return unique names in case of overridden bean definitions.

Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.

This version of beanNamesForTypeIncludingAncestors automatically includes prototypes and FactoryBeans.

Parameters:
lbf the bean factory
type the type that beans must match
Returns:
the array of matching bean names, or an empty array if none
		Assert.notNull(lbf"ListableBeanFactory must not be null");
		String[] result = lbf.getBeanNamesForType(type);
		if (lbf instanceof HierarchicalBeanFactory) {
			if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
				List resultList = new ArrayList();
				resultList.addAll(Arrays.asList(result));
				for (int i = 0; i < parentResult.lengthi++) {
					String beanName = parentResult[i];
					if (!resultList.contains(beanName) && !hbf.containsLocalBean(beanName)) {
						resultList.add(beanName);
					}
				}
				result = StringUtils.toStringArray(resultList);
			}
		}
		return result;
	}

Get all bean names for the given type, including those defined in ancestor factories. Will return unique names in case of overridden bean definitions.

Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).

Parameters:
lbf the bean factory
includeNonSingletons whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
allowEagerInit whether to initialize lazy-init singletons and objects created by FactoryBeans (or by factory methods with a "factory-bean" reference) for the type check. Note that FactoryBeans need to be eagerly initialized to determine their type: So be aware that passing in "true" for this flag will initialize FactoryBeans and "factory-bean" references.
type the type that beans must match
Returns:
the array of matching bean names, or an empty array if none
			ListableBeanFactory lbfClass typeboolean includeNonSingletonsboolean allowEagerInit) {
		Assert.notNull(lbf"ListableBeanFactory must not be null");
		String[] result = lbf.getBeanNamesForType(typeincludeNonSingletonsallowEagerInit);
		if (lbf instanceof HierarchicalBeanFactory) {
			if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
						(ListableBeanFactoryhbf.getParentBeanFactory(), typeincludeNonSingletonsallowEagerInit);
				List resultList = new ArrayList();
				resultList.addAll(Arrays.asList(result));
				for (int i = 0; i < parentResult.lengthi++) {
					String beanName = parentResult[i];
					if (!resultList.contains(beanName) && !hbf.containsLocalBean(beanName)) {
						resultList.add(beanName);
					}
				}
				result = StringUtils.toStringArray(resultList);
			}
		}
		return result;
	}

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 returned Map will only contain beans of this type.

Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.

Parameters:
lbf the bean factory
type type of bean to match
Returns:
the Map of matching bean instances, or an empty Map if none
Throws:
org.springframework.beans.BeansException if a bean could not be created
	    throws BeansException {
		Assert.notNull(lbf"ListableBeanFactory must not be null");
		Map result = new LinkedHashMap(4);
		result.putAll(lbf.getBeansOfType(type));
		if (lbf instanceof HierarchicalBeanFactory) {
			if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
				for (Iterator it = parentResult.entrySet().iterator(); it.hasNext();) {
					Map.Entry entry = (Map.Entryit.next();
					String beanName = (Stringentry.getKey();
					if (!result.containsKey(beanName) && !hbf.containsLocalBean(beanName)) {
						result.put(beanNameentry.getValue());
					}
				}
			}
		}
		return result;
	}

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 returned Map will only contain beans of this type.

Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).

Parameters:
lbf the bean factory
type type of bean to match
includeNonSingletons whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
allowEagerInit whether to initialize lazy-init singletons and objects created by FactoryBeans (or by factory methods with a "factory-bean" reference) for the type check. Note that FactoryBeans need to be eagerly initialized to determine their type: So be aware that passing in "true" for this flag will initialize FactoryBeans and "factory-bean" references.
Returns:
the Map of matching bean instances, or an empty Map if none
Throws:
org.springframework.beans.BeansException if a bean could not be created
			ListableBeanFactory lbfClass typeboolean includeNonSingletonsboolean allowEagerInit)
	    throws BeansException {
		Assert.notNull(lbf"ListableBeanFactory must not be null");
		Map result = new LinkedHashMap(4);
		result.putAll(lbf.getBeansOfType(typeincludeNonSingletonsallowEagerInit));
		if (lbf instanceof HierarchicalBeanFactory) {
			if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
						(ListableBeanFactoryhbf.getParentBeanFactory(), typeincludeNonSingletonsallowEagerInit);
				for (Iterator it = parentResult.entrySet().iterator(); it.hasNext();) {
					Map.Entry entry = (Map.Entryit.next();
					String beanName = (Stringentry.getKey();
					if (!result.containsKey(beanName) && !hbf.containsLocalBean(beanName)) {
						result.put(beanNameentry.getValue());
					}
				}
			}
		}
		return result;
	}


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.

Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.

This version of beanOfTypeIncludingAncestors automatically includes prototypes and FactoryBeans.

Parameters:
lbf the bean factory
type type of bean to match
Returns:
the matching bean instance
Throws:
NoSuchBeanDefinitionException if 0 or more than 1 beans of the given type were found
org.springframework.beans.BeansException if the bean could not be created
			throws BeansException {
		Map beansOfType = beansOfTypeIncludingAncestors(lbftype);
		if (beansOfType.size() == 1) {
			return beansOfType.values().iterator().next();
		}
		else {
			throw new NoSuchBeanDefinitionException(type"expected single bean but found " + beansOfType.size());
		}
	}

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.

Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).

Parameters:
lbf the bean factory
type type of bean to match
includeNonSingletons whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
allowEagerInit whether to initialize lazy-init singletons and objects created by FactoryBeans (or by factory methods with a "factory-bean" reference) for the type check. Note that FactoryBeans need to be eagerly initialized to determine their type: So be aware that passing in "true" for this flag will initialize FactoryBeans and "factory-bean" references.
Returns:
the matching bean instance
Throws:
NoSuchBeanDefinitionException if 0 or more than 1 beans of the given type were found
org.springframework.beans.BeansException if the bean could not be created
			ListableBeanFactory lbfClass typeboolean includeNonSingletonsboolean allowEagerInit)
	    throws BeansException {
		Map beansOfType = beansOfTypeIncludingAncestors(lbftypeincludeNonSingletonsallowEagerInit);
		if (beansOfType.size() == 1) {
			return beansOfType.values().iterator().next();
		}
		else {
			throw new NoSuchBeanDefinitionException(type"expected single bean but found " + beansOfType.size());
		}
	}

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.

Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.

This version of beanOfType automatically includes prototypes and FactoryBeans.

Parameters:
lbf the bean factory
type type of bean to match
Returns:
the matching bean instance
Throws:
NoSuchBeanDefinitionException if 0 or more than 1 beans of the given type were found
org.springframework.beans.BeansException if the bean could not be created
	public static Object beanOfType(ListableBeanFactory lbfClass typethrows BeansException {
		Assert.notNull(lbf"ListableBeanFactory must not be null");
		Map beansOfType = lbf.getBeansOfType(type);
		if (beansOfType.size() == 1) {
			return beansOfType.values().iterator().next();
		}
		else {
			throw new NoSuchBeanDefinitionException(type"expected single bean but found " + beansOfType.size());
		}
	}

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.

Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).

Parameters:
lbf the bean factory
type type of bean to match
includeNonSingletons whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
allowEagerInit whether to initialize lazy-init singletons and objects created by FactoryBeans (or by factory methods with a "factory-bean" reference) for the type check. Note that FactoryBeans need to be eagerly initialized to determine their type: So be aware that passing in "true" for this flag will initialize FactoryBeans and "factory-bean" references.
Returns:
the matching bean instance
Throws:
NoSuchBeanDefinitionException if 0 or more than 1 beans of the given type were found
org.springframework.beans.BeansException if the bean could not be created
	public static Object beanOfType(
			ListableBeanFactory lbfClass typeboolean includeNonSingletonsboolean allowEagerInit)
	    throws BeansException {
		Assert.notNull(lbf"ListableBeanFactory must not be null");
		Map beansOfType = lbf.getBeansOfType(typeincludeNonSingletonsallowEagerInit);
		if (beansOfType.size() == 1) {
			return beansOfType.values().iterator().next();
		}
		else {
			throw new NoSuchBeanDefinitionException(type"expected single bean but found " + beansOfType.size());
		}
	}
New to GrepCode? Check out our FAQ X