abstractbeanfactory.java
来自「一个关于Spring框架的示例应用程序,简单使用,可以参考.」· Java 代码 · 共 1,016 行 · 第 1/3 页
JAVA
1,016 行
/*
* Copyright 2002-2004 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.support;
import java.beans.PropertyEditor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.BeanIsAbstractException;
import org.springframework.beans.factory.BeanIsNotAFactoryException;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.FactoryBeanNotInitializedException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.util.Assert;
/**
* Abstract superclass for BeanFactory implementations, implementing the
* ConfigurableBeanFactory SPI interface. Does <i>not</i> assume a listable
* bean factory: can therefore also be used as base class for bean factory
* implementations which fetch bean definitions from a variety of backend
* resources (where bean definition access is an expensive operation).
*
* <p>This class provides singleton/prototype determination, singleton cache,
* aliases, FactoryBean handling, bean definition merging for child bean definitions,
* and bean destruction (DisposableBean interface, custom destroy methods).
* Furthermore, it can manage a bean factory hierarchy, through implementing the
* HierarchicalBeanFactory interface (superinterface of ConfigurableBeanFactory).
*
* <p>The main template methods to be implemented by subclasses are
* <code>getBeanDefinition</code> and <code>createBean</code>, retrieving a
* bean definition for a given bean name or creating a bean instance for a
* given bean definition. Default implementations for those can be found in
* DefaultListableBeanFactory or AbstractAutowireCapableBeanFactory, respectively.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @since 15 April 2001
* @see #getBeanDefinition
* @see #createBean
* @see org.springframework.beans.factory.HierarchicalBeanFactory
* @see org.springframework.beans.factory.DisposableBean
* @see RootBeanDefinition
* @see ChildBeanDefinition
* @see AbstractAutowireCapableBeanFactory#createBean
* @see DefaultListableBeanFactory#getBeanDefinition
*/
public abstract class AbstractBeanFactory implements ConfigurableBeanFactory {
/**
* Marker object to be temporarily registered in the singleton cache
* while instantiating a bean, to be able to detect circular references.
*/
private static final Object CURRENTLY_IN_CREATION = new Object();
/** Logger available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());
/** Parent bean factory, for bean inheritance support */
private BeanFactory parentBeanFactory;
/** Custom PropertyEditors to apply to the beans of this factory */
private Map customEditors = new HashMap();
/** BeanPostProcessors to apply in createBean */
private final List beanPostProcessors = new ArrayList();
private boolean hasDestructionAwareBeanPostProcessors;
/** Map from alias to canonical bean name */
private final Map aliasMap = Collections.synchronizedMap(new HashMap());
/** Cache of singletons: bean name --> bean instance */
private final Map singletonCache = Collections.synchronizedMap(new HashMap());
/** Disposable bean instances: bean name --> disposable instance */
private final Map disposableBeans = Collections.synchronizedMap(new HashMap());
/** Map between dependent bean names: bean name --> dependent bean name */
private final Map dependentBeanMap = Collections.synchronizedMap(new HashMap());
/**
* Create a new AbstractBeanFactory.
*/
public AbstractBeanFactory() {
}
/**
* Create a new AbstractBeanFactory with the given parent.
* @param parentBeanFactory parent bean factory, or null if none
* @see #getBean
*/
public AbstractBeanFactory(BeanFactory parentBeanFactory) {
this.parentBeanFactory = parentBeanFactory;
}
//---------------------------------------------------------------------
// Implementation of BeanFactory interface
//---------------------------------------------------------------------
public Object getBean(String name) throws BeansException {
return getBean(name, null, null);
}
public Object getBean(String name, Class requiredType) throws BeansException {
return getBean(name, requiredType, null);
}
/**
* Return the bean with the given name,
* checking the parent bean factory if not found.
* @param name the name of the bean to retrieve
* @param args arguments to use if creating a prototype using explicit arguments to a
* static factory method. It is invalid to use a non-null args value in any other case.
*/
public Object getBean(String name, Object[] args) throws BeansException {
return getBean(name, null, args);
}
/**
* Return the bean with the given name,
* checking the parent bean factory if not found.
* @param name the name of the bean to retrieve
* @param requiredType the required type of the bean to retrieve
* @param args arguments to use if creating a prototype using explicit arguments to a
* static factory method. It is invalid to use a non-null args value in any other case.
* TODO: We could consider supporting this for constructor args also, but it's really a
* corner case required for AspectJ integration.
*/
public Object getBean(String name, Class requiredType, Object[] args) throws BeansException {
String beanName = transformedBeanName(name);
Object bean = null;
// Eagerly check singleton cache for manually registered singletons.
Object sharedInstance = this.singletonCache.get(beanName);
if (sharedInstance != null) {
if (sharedInstance == CURRENTLY_IN_CREATION) {
throw new BeanCurrentlyInCreationException(beanName);
}
if (logger.isDebugEnabled()) {
logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
}
bean = getObjectForSharedInstance(name, sharedInstance);
}
else {
// Check if bean definition exists in this factory.
RootBeanDefinition mergedBeanDefinition = null;
try {
mergedBeanDefinition = getMergedBeanDefinition(beanName, false);
}
catch (NoSuchBeanDefinitionException ex) {
// Not found -> check parent.
if (this.parentBeanFactory instanceof AbstractBeanFactory) {
// Delegation to parent with args only possible for AbstractBeanFactory.
return ((AbstractBeanFactory) this.parentBeanFactory).getBean(name, requiredType, args);
}
else if (this.parentBeanFactory != null && args == null) {
// No args -> delegate to standard getBean method.
return this.parentBeanFactory.getBean(name, requiredType);
}
throw ex;
}
checkMergedBeanDefinition(mergedBeanDefinition, beanName, requiredType, args);
// Create bean instance.
if (mergedBeanDefinition.isSingleton()) {
synchronized (this.singletonCache) {
// re-check singleton cache within synchronized block
sharedInstance = this.singletonCache.get(beanName);
if (sharedInstance == null) {
if (logger.isInfoEnabled()) {
logger.info("Creating shared instance of singleton bean '" + beanName + "'");
}
this.singletonCache.put(beanName, CURRENTLY_IN_CREATION);
try {
sharedInstance = createBean(beanName, mergedBeanDefinition, args);
this.singletonCache.put(beanName, sharedInstance);
}
catch (BeansException ex) {
this.singletonCache.remove(beanName);
throw ex;
}
}
}
bean = getObjectForSharedInstance(name, sharedInstance);
}
else {
// It's a prototype -> create a new instance.
bean = createBean(name, mergedBeanDefinition, args);
}
}
// Check if required type matches the type of the actual bean instance.
if (requiredType != null && !requiredType.isAssignableFrom(bean.getClass())) {
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
return bean;
}
public boolean containsBean(String name) {
String beanName = transformedBeanName(name);
if (this.singletonCache.containsKey(beanName)) {
return true;
}
if (containsBeanDefinition(beanName)) {
return true;
}
else {
// Not found -> check parent.
if (this.parentBeanFactory != null) {
return this.parentBeanFactory.containsBean(name);
}
else {
return false;
}
}
}
public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
String beanName = transformedBeanName(name);
try {
Class beanClass = null;
boolean singleton = true;
Object beanInstance = this.singletonCache.get(beanName);
if (beanInstance == CURRENTLY_IN_CREATION) {
throw new BeanCurrentlyInCreationException(beanName);
}
if (beanInstance != null) {
beanClass = beanInstance.getClass();
singleton = true;
}
else {
RootBeanDefinition bd = getMergedBeanDefinition(beanName, false);
if (bd.hasBeanClass()) {
beanClass = bd.getBeanClass();
}
singleton = bd.isSingleton();
}
// In case of FactoryBean, return singleton status of created object if not a dereference.
if (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass) &&
!isFactoryDereference(name)) {
FactoryBean factoryBean = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName);
return factoryBean.isSingleton();
}
return singleton;
}
catch (NoSuchBeanDefinitionException ex) {
// Not found -> check parent.
if (this.parentBeanFactory != null) {
return this.parentBeanFactory.isSingleton(name);
}
throw ex;
}
}
public Class getType(String name) throws NoSuchBeanDefinitionException {
String beanName = transformedBeanName(name);
try {
Class beanClass = null;
// Check manually registered singletons.
Object beanInstance = this.singletonCache.get(beanName);
if (beanInstance == CURRENTLY_IN_CREATION) {
throw new BeanCurrentlyInCreationException(beanName);
}
if (beanInstance != null) {
beanClass = beanInstance.getClass();
}
else {
// OK, let's assume it's a bean definition.
RootBeanDefinition mergedBeanDefinition = getMergedBeanDefinition(beanName, false);
// Return "undeterminable" for beans without class or with factory method.
if (!mergedBeanDefinition.hasBeanClass() || mergedBeanDefinition.getFactoryMethodName() != null) {
return null;
}
beanClass = mergedBeanDefinition.getBeanClass();
}
// Check bean class whether we're dealing with a FactoryBean.
if (FactoryBean.class.isAssignableFrom(beanClass) && !isFactoryDereference(name)) {
// If it's a FactoryBean, we want to look at what it creates, not the factory class.
FactoryBean factoryBean = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName);
return factoryBean.getObjectType();
}
return beanClass;
}
catch (NoSuchBeanDefinitionException ex) {
// Not found -> check parent.
if (this.parentBeanFactory != null) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?