📄 abstractbeandefinition.java
字号:
/*
* Copyright 2002-2007 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.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Iterator;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.core.AttributeAccessorSupport;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
/**
* Base class for concrete, full-fledged
* {@link org.springframework.beans.factory.config.BeanDefinition} classes,
* factoring out common properties of {@link RootBeanDefinition} and
* {@link ChildBeanDefinition}.
*
* <p>The autowire constants match the ones defined in the
* {@link org.springframework.beans.factory.config.AutowireCapableBeanFactory}
* interface.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Rob Harrop
* @see RootBeanDefinition
* @see ChildBeanDefinition
*/
public abstract class AbstractBeanDefinition extends AttributeAccessorSupport implements BeanDefinition {
/**
* Constant that indicates no autowiring at all.
* @see #setAutowireMode
*/
public static final int AUTOWIRE_NO = AutowireCapableBeanFactory.AUTOWIRE_NO;
/**
* Constant that indicates autowiring bean properties by name.
* @see #setAutowireMode
*/
public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
/**
* Constant that indicates autowiring bean properties by type.
* @see #setAutowireMode
*/
public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
/**
* Constant that indicates autowiring a constructor.
* @see #setAutowireMode
*/
public static final int AUTOWIRE_CONSTRUCTOR = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
/**
* Constant that indicates determining an appropriate autowire strategy
* through introspection of the bean class.
* @see #setAutowireMode
*/
public static final int AUTOWIRE_AUTODETECT = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT;
/**
* Constant that indicates no dependency check at all.
* @see #setDependencyCheck
*/
public static final int DEPENDENCY_CHECK_NONE = 0;
/**
* Constant that indicates dependency checking for object references.
* @see #setDependencyCheck
*/
public static final int DEPENDENCY_CHECK_OBJECTS = 1;
/**
* Constant that indicates dependency checking for "simple" properties.
* @see #setDependencyCheck
* @see org.springframework.beans.BeanUtils#isSimpleProperty
*/
public static final int DEPENDENCY_CHECK_SIMPLE = 2;
/**
* Constant that indicates dependency checking for all properties
* (object references as well as "simple" properties).
* @see #setDependencyCheck
*/
public static final int DEPENDENCY_CHECK_ALL = 3;
private volatile Object beanClass;
private String scope = SCOPE_SINGLETON;
private boolean singleton = true;
private boolean prototype = false;
private boolean abstractFlag = false;
private boolean lazyInit = false;
private boolean autowireCandidate = true;
private int autowireMode = AUTOWIRE_NO;
private int dependencyCheck = DEPENDENCY_CHECK_NONE;
private String[] dependsOn;
private ConstructorArgumentValues constructorArgumentValues;
private MutablePropertyValues propertyValues;
private MethodOverrides methodOverrides = new MethodOverrides();
private String factoryBeanName;
private String factoryMethodName;
private String initMethodName;
private String destroyMethodName;
private boolean enforceInitMethod = true;
private boolean enforceDestroyMethod = true;
private boolean synthetic = false;
private String resourceDescription;
private Object source;
private int role = BeanDefinition.ROLE_APPLICATION;
/**
* Create a new AbstractBeanDefinition with default settings.
*/
protected AbstractBeanDefinition() {
this(null, null);
}
/**
* Create a new AbstractBeanDefinition with the given
* constructor argument values and property values.
*/
protected AbstractBeanDefinition(ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
setConstructorArgumentValues(cargs);
setPropertyValues(pvs);
}
/**
* Create a new AbstractBeanDefinition as deep copy of the given
* bean definition.
* @param original the original bean definition to copy from
*/
protected AbstractBeanDefinition(AbstractBeanDefinition original) {
this.beanClass = original.beanClass;
setScope(original.getScope());
setAbstract(original.isAbstract());
setLazyInit(original.isLazyInit());
setAutowireCandidate(original.isAutowireCandidate());
setAutowireMode(original.getAutowireMode());
setDependencyCheck(original.getDependencyCheck());
setDependsOn(original.getDependsOn());
setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues()));
setPropertyValues(new MutablePropertyValues(original.getPropertyValues()));
setMethodOverrides(new MethodOverrides(original.getMethodOverrides()));
setFactoryBeanName(original.getFactoryBeanName());
setFactoryMethodName(original.getFactoryMethodName());
setInitMethodName(original.getInitMethodName());
setEnforceInitMethod(original.isEnforceInitMethod());
setDestroyMethodName(original.getDestroyMethodName());
setEnforceDestroyMethod(original.isEnforceDestroyMethod());
setSynthetic(original.isSynthetic());
setResourceDescription(original.getResourceDescription());
setSource(original.getSource());
setRole(original.getRole());
copyAttributesFrom(original);
}
/**
* Override settings in this bean definition (assumably a copied parent
* from a parent-child inheritance relationship) from the given bean
* definition (assumably the child).
* <ul>
* <li>Will override beanClass if specified in the given bean definition.
* <li>Will always take <code>abstract</code>, <code>scope</code>,
* <code>lazyInit</code>, <code>autowireMode</code>, <code>dependencyCheck</code>,
* and <code>dependsOn</code> from the given bean definition.
* <li>Will add <code>constructorArgumentValues</code>, <code>propertyValues</code>,
* <code>methodOverrides</code> from the given bean definition to existing ones.
* <li>Will override <code>factoryBeanName</code>, <code>factoryMethodName</code>,
* <code>initMethodName</code>, and <code>destroyMethodName</code> if specified
* in the given bean definition.
* </ul>
*/
public void overrideFrom(AbstractBeanDefinition other) {
if (other.beanClass != null) {
this.beanClass = other.beanClass;
}
setScope(other.getScope());
setAbstract(other.isAbstract());
setLazyInit(other.isLazyInit());
setAutowireCandidate(other.isAutowireCandidate());
setAutowireMode(other.getAutowireMode());
setDependencyCheck(other.getDependencyCheck());
setDependsOn(other.getDependsOn());
getConstructorArgumentValues().addArgumentValues(other.getConstructorArgumentValues());
getPropertyValues().addPropertyValues(other.getPropertyValues());
getMethodOverrides().addOverrides(other.getMethodOverrides());
if (other.getFactoryBeanName() != null) {
setFactoryBeanName(other.getFactoryBeanName());
}
if (other.getFactoryMethodName() != null) {
setFactoryMethodName(other.getFactoryMethodName());
}
if (other.getInitMethodName() != null) {
setInitMethodName(other.getInitMethodName());
setEnforceInitMethod(other.isEnforceInitMethod());
}
if (other.getDestroyMethodName() != null) {
setDestroyMethodName(other.getDestroyMethodName());
setEnforceDestroyMethod(other.isEnforceDestroyMethod());
}
setSynthetic(other.isSynthetic());
setResourceDescription(other.getResourceDescription());
setSource(other.getSource());
setRole(other.getRole());
copyAttributesFrom(other);
}
/**
* Return whether this definition specifies a bean class.
*/
public boolean hasBeanClass() {
return (this.beanClass instanceof Class);
}
/**
* Specify the class for this bean.
*/
public void setBeanClass(Class beanClass) {
this.beanClass = beanClass;
}
/**
* Return the class of the wrapped bean.
* @throws IllegalStateException if the bean definition does not define a bean class,
* or a specified bean class name has not been resolved into an actual Class
*/
public Class getBeanClass() throws IllegalStateException {
if (this.beanClass == null) {
throw new IllegalStateException("No bean class specified on bean definition");
}
if (!(this.beanClass instanceof Class)) {
throw new IllegalStateException(
"Bean class name [" + this.beanClass + "] has not been resolved into an actual Class");
}
return (Class) this.beanClass;
}
/**
* Specify the class name for this bean.
*/
public void setBeanClassName(String beanClassName) {
this.beanClass = beanClassName;
}
/**
* Return the class name of the wrapped bean.
*/
public String getBeanClassName() {
if (this.beanClass instanceof Class) {
return ((Class) this.beanClass).getName();
}
else {
return (String) this.beanClass;
}
}
/**
* Determine the class of the wrapped bean, resolving it from a
* specified class name if necessary. Will also reload a specified
* Class from its name when called with the bean class already resolved.
* @param classLoader the ClassLoader to use for resolving a (potential) class name
* @return the resolved bean class
* @throws ClassNotFoundException if the class name could be resolved
*/
public Class resolveBeanClass(ClassLoader classLoader) throws ClassNotFoundException {
if (this.beanClass == null) {
return null;
}
Class resolvedClass = ClassUtils.forName(getBeanClassName(), classLoader);
this.beanClass = resolvedClass;
return resolvedClass;
}
/**
* Set the name of the target scope for the bean.
* <p>Default is "singleton"; the out-of-the-box alternative is "prototype".
* Extended bean factories might support further scopes.
* @see #SCOPE_SINGLETON
* @see #SCOPE_PROTOTYPE
*/
public void setScope(String scope) {
Assert.notNull(scope, "Scope must not be null");
this.scope = scope;
this.singleton = SCOPE_SINGLETON.equals(scope);
this.prototype = SCOPE_PROTOTYPE.equals(scope);
}
/**
* Return the name of the target scope for the bean.
*/
public String getScope() {
return this.scope;
}
/**
* Set if this a <b>Singleton</b>, with a single, shared instance returned
* on all calls. In case of "false", the BeanFactory will apply the <b>Prototype</b>
* design pattern, with each caller requesting an instance getting an independent
* instance. How this is exactly defined will depend on the BeanFactory.
* <p>"Singletons" are the commoner type, so the default is "true".
* Note that as of Spring 2.0, this flag is just an alternative way to
* specify scope="singleton" or scope="prototype".
* @see #setScope
* @see #SCOPE_SINGLETON
* @see #SCOPE_PROTOTYPE
*/
public void setSingleton(boolean singleton) {
this.scope = (singleton ? SCOPE_SINGLETON : SCOPE_PROTOTYPE);
this.singleton = singleton;
this.prototype = !singleton;
}
/**
* Return whether this a <b>Singleton</b>, with a single shared instance
* returned from all calls.
* @see #SCOPE_SINGLETON
*/
public boolean isSingleton() {
return this.singleton;
}
/**
* Return whether this a <b>Prototype</b>, with an independent instance
* returned for each call.
* @see #SCOPE_PROTOTYPE
*/
public boolean isPrototype() {
return this.prototype;
}
/**
* Set if this bean is "abstract", i.e. not meant to be instantiated itself but
* rather just serving as parent for concrete child bean definitions.
* <p>Default is "false". Specify true to tell the bean factory to not try to
* instantiate that particular bean in any case.
*/
public void setAbstract(boolean abstractFlag) {
this.abstractFlag = abstractFlag;
}
/**
* Return whether this bean is "abstract", i.e. not meant to be instantiated
* itself but rather just serving as parent for concrete child bean definitions.
*/
public boolean isAbstract() {
return this.abstractFlag;
}
/**
* Set whether this bean should be lazily initialized.
* <p>If <code>false</code>, the bean will get instantiated on startup by bean
* factories that perform eager initialization of singletons.
*/
public void setLazyInit(boolean lazyInit) {
this.lazyInit = lazyInit;
}
/**
* Return whether this bean should be lazily initialized, i.e. not
* eagerly instantiated on startup. Only applicable to a singleton bean.
*/
public boolean isLazyInit() {
return this.lazyInit;
}
/**
* Set whether this bean is a candidate for getting autowired into
* some other bean.
*/
public void setAutowireCandidate(boolean autowireCandidate) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -