BeanDefinition继承了AttributeAccessor,说明它具有处理属性的能力;
BeanDefinition继承了BeanMetadataElement,说明它可以持有Bean元数据元素,作用是可以持有XML文件的一个bean标签对应的Object。
Spring容器启动的过程中,会将Bean解析成Spring内部的BeanDefinition结构。
我们获取对象的方式一般有两种,一种是手动直接 new;另一种是交给 Spring 管理,Spring 将管理的对象称之为 Bean,容器会先实例化 Bean,然后自动注入,实例化的过程就需要依赖 BeanDefinition。
**BeanDefinition 用于保存 Bean 的相关信息,包括属性、构造方法参数、依赖的 Bean 名称及是否单例、延迟加载等,它是实例化 Bean 的原材料,Spring 就是根据 BeanDefinition 中的信息实例化 Bean。 **
BeanDefinition的继承体系
BeanDefinition 是一个接口,它有多个实现类,这些实现类分别描述不同类型的 Bean。
BeanDefinition
一个 BeanDefinition 描述了一个 Bean 实例,实例包含属性值、构造方法参数值以及更多实现信息。该 BeanDefinition 只是是一个最小的接口,主要目的是允许修改属性值和其他 Bean 元数据,这里列出几个核心方法。
直接看BeanDefinition源码,
//元数据操作接口
public interface AttributeAccessor {
//设置元数据
void setAttribute(String name, Object value);
//获取元数据
Object getAttribute(String name);
//删除元数据
Object removeAttribute(String name);
//是否含有元数据
boolean hasAttribute(String name);
//获取元数据的name数组
String[] attributeNames();
}
//接口:用于承载bean对象
public interface BeanMetadataElement {
//获取当前元素的配置源bean对象
Object getSource();
}
//用于描述一个具体bean实例
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
//scope值,单例
String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
//scope值,非单例
String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;
//Bean角色:
//用户
int ROLE_APPLICATION = 0;
//某些复杂的配置
int ROLE_SUPPORT = 1;
//完全内部使用
int ROLE_INFRASTRUCTURE = 2;
//返回此bean定义的父bean定义的名称,如果有的话
String getParentName();
void setParentName(String parentName);
//获取bean对象className
String getBeanClassName();
void setBeanClassName(String beanClassName);
//定义创建该Bean对象的工厂l类
String getFactoryBeanName();
void setFactoryBeanName(String factoryBeanName);
//定义创建该Bean对象的工厂方法
String getFactoryMethodName();
void setFactoryMethodName(String factoryMethodName);
//
String getScope();
void setScope(String scope);
//懒加载
boolean isLazyInit();
void setLazyInit(boolean lazyInit);
//依赖对象
String[] getDependsOn();
void setDependsOn(String[] dependsOn);
//是否为被自动装配
boolean isAutowireCandidate();
void setAutowireCandidate(boolean autowireCandidate);
//是否为主候选bean 使用注解:@Primary
boolean isPrimary();
void setPrimary(boolean primary);
//返回此bean的构造函数参数值。
ConstructorArgumentValues getConstructorArgumentValues();
//获取普通属性集合
MutablePropertyValues getPropertyValues();
//是否为单例
boolean isSingleton();
//是否为原型
boolean isPrototype();
//是否为抽象类
boolean isAbstract();
//获取这个bean的应用
int getRole();
//返回对bean定义的可读描述。
String getDescription();
//返回该bean定义来自的资源的描述(用于在出现错误时显示上下文)
String getResourceDescription();
BeanDefinition getOriginatingBeanDefinition();
}
可以看到 BeanDefinition 接口提供了一系列操作 Bean 元数据的方法,例如类名、scope、属性、构造函数参数列表、依赖的bean、是否是单例类、是否是懒加载等,其实就是将Bean的定义信息存储到这个BeanDefinition相应的属性中,后面对Bean的操作就直接对BeanDefinition进行,例如拿到这个BeanDefinition后,可以根据里面的类名、构造函数、构造函数参数,使用反射进行对象创建。
AnnotatedBeanDefinition
AnnotatedBeanDefinition 是 BeanDefinition 子接口之一,该接口扩展了 BeanDefinition 的功能,其用来操作注解元数据。一般情况下,通过注解方式得到的 Bean(@Component、@Bean),其 BeanDefinition 类型都是该接口的实现类。
//接口:支持注释元数据的bean定义
public interface AnnotatedBeanDefinition extends BeanDefinition {
//获取此bean定义bean类的注释元数据(以及基本类元数据)。
AnnotationMetadata getMetadata();
}
//实现类一:支持注释元数据的普通bean定义
public class AnnotatedGenericBeanDefinition extends GenericBeanDefinition implements AnnotatedBeanDefinition {
//当前bean的注解元数据
private final AnnotationMetadata metadata;
//通过给定beanClass 构造对象
public AnnotatedGenericBeanDefinition(Class> beanClass) {
setBeanClass(beanClass);
this.metadata = new StandardAnnotationMetadata(beanClass, true);
}
//给定注解元数据,构造对象
public AnnotatedGenericBeanDefinition(AnnotationMetadata metadata) {
Assert.notNull(metadata, "AnnotationMetadata must not be null");
if (metadata instanceof StandardAnnotationMetadata) {
setBeanClass(((StandardAnnotationMetadata) metadata).getIntrospectedClass());
}
else {
setBeanClassName(metadata.getClassName());
}
this.metadata = metadata;
}
//获取bean的注解元数据
public final AnnotationMetadata getMetadata() {
return this.metadata;
}
}
//实现类二:支持注释元数据bean定义
//通过 Annotation 配置方式定义的 Bean 属性经 Spring 框架解析后会封装成 ScannedGenericBeanDefinition
public class ScannedGenericBeanDefinition extends GenericBeanDefinition implements AnnotatedBeanDefinition {
//当前bean的注解元数据
private final AnnotationMetadata metadata;
//metadatareader:扫描指定类的注释元数据
public ScannedGenericBeanDefinition(MetadataReader metadataReader) {
Assert.notNull(metadataReader, "MetadataReader must not be null");
this.metadata = metadataReader.getAnnotationMetadata();
setBeanClassName(this.metadata.getClassName());
}
//获取bean的注解元数据
public final AnnotationMetadata getMetadata() {
return this.metadata;
}
}
该接口可以返回两个元数据的类:
AnnotationMetadata:主要对 Bean 的注解信息进行操作,如:获取当前 Bean 标注的所有注解、判断是否包含指定注解。
MethodMetadata:方法的元数据类。提供获取方法名称、此方法所属类的全类名、是否是抽象方法、判断是否是静态方法、判断是否是final方法等。
AbstractBeanDefinition
AbstractBeanDefinition 是 BeanDefinition 的子抽象类,也是其他 BeanDefinition 类型的基类,其实现了接口中定义的一系列操作方法,并定义了一系列的常量属性,这些常量会直接影响到 Spring 实例化 Bean 时的策略。核心属性如下。
//抽象类:基础bean,基石
@SuppressWarnings("serial")
public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor
implements BeanDefinition, Cloneable {
//第一部分:一些常量
//默认scope值,bean的作用范围
public static final String SCOPE_DEFAULT = "";
//自动装配方式常量
//不自动装配,需手动注入
public static final int AUTOWIRE_NO = AutowireCapableBeanFactory.AUTOWIRE_NO;
public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
//按构造器参数类型自动装配(constructor跟byType十分相似.)
public static final int AUTOWIRE_CONSTRUCTOR = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
//首先尝试使用constructor进行自动装配。如果失败,再尝试使用byType进行自动装配
@Deprecated
public static final int AUTOWIRE_AUTODETECT = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT;
//依赖检查类型常量
//依赖检查:无依赖
public static final int DEPENDENCY_CHECK_NONE = 0;
//依赖检查:对象间的引用
public static final int DEPENDENCY_CHECK_OBJECTS = 1;
///依赖检查:会核对所有的原始类型和String类型的属性。
public static final int DEPENDENCY_CHECK_SIMPLE = 2;
// 依赖检查:所有属性。(对象引用及原始类型和String类型的属性)。
public static final int DEPENDENCY_CHECK_ALL = 3;
public static final String INFER_METHOD = "(inferred)";
//第二部分,bean的属性描述
//存放bean的class对象
private volatile Object beanClass;
//bean的作用范围
private String scope = SCOPE_DEFAULT;
//是否单例
private boolean singleton = true;
//是否原型
private boolean prototype = false;
//是否抽象
private boolean abstractFlag = false;
//是否延迟加载
private boolean lazyInit = false;
//默认不自动装配
private int autowireMode = AUTOWIRE_NO;
//默认依赖检查:无依赖
private int dependencyCheck = DEPENDENCY_CHECK_NONE;
//依赖列表
private String[] dependsOn;
//当前bean时候做为自动装配的候选者
private boolean autowireCandidate = true;
//默认为非主候选
private boolean primary = false;
//用于纪录qualifier,对应子元素Qualifier
// @Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean 将会被装配来消除混乱
private final Map qualifiers =
new LinkedHashMap(0);
//允许访问非公开的构造器方法
private boolean nonPublicAccessAllowed = true;
/**
* 是否以一种宽松的模式解析构造函数,默认为 true
* 如果为false,则下面情况报错
* interface ITest{}
* class ITestImpl implements ITest{}
* class Main{
* Main(ITest){}
* Main(ITestImpl){}
* }
*/
private boolean lenientConstructorResolution = true;
/**
* 记录构造函数注入属性,如:
*
*
*
*
*
*/
private ConstructorArgumentValues constructorArgumentValues;
/**
* 普通属性的集合,如:
*
*
*
*
*
*
*
*/
private MutablePropertyValues propertyValues;
//方法重写的持有者,记录 lookup-method,replaced-method元素
private MethodOverrides methodOverrides = new MethodOverrides();
//构造当前实例工厂类名称
private String factoryBeanName;
//构造当前实例工厂类中方法名称
private String factoryMethodName;
//初始化方法
private String initMethodName;
//bean被销毁时,调用的方法
private String destroyMethodName;
//是否执行initMethod,程序设置
private boolean enforceInitMethod = true;
//是否执行destroyMethod,程序设置
private boolean enforceDestroyMethod = true;
private boolean synthetic = false;
//Bean角色
private int role = BeanDefinition.ROLE_APPLICATION;
//bean的描述信息
private String description;
//该bean定义来自的资源
private Resource resource;
//空构造方法
protected AbstractBeanDefinition() {
this(null, null);
}
//指定的构造函数的参数值和属性值
protected AbstractBeanDefinition(ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
setConstructorArgumentValues(cargs);
setPropertyValues(pvs);
}
//深拷贝构造
@Deprecated
protected AbstractBeanDefinition(AbstractBeanDefinition original) {
this((BeanDefinition) original);
}
//深拷贝构造
protected AbstractBeanDefinition(BeanDefinition original) {
setParentName(original.getParentName());
setBeanClassName(original.getBeanClassName());
setFactoryBeanName(original.getFactoryBeanName());
setFactoryMethodName(original.getFactoryMethodName());
setScope(original.getScope());
setAbstract(original.isAbstract());
setLazyInit(original.isLazyInit());
setRole(original.getRole());
setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues()));
setPropertyValues(new MutablePropertyValues(original.getPropertyValues()));
setSource(original.getSource());
copyAttributesFrom(original);
if (original instanceof AbstractBeanDefinition) {
AbstractBeanDefinition originalAbd = (AbstractBeanDefinition) original;
if (originalAbd.hasBeanClass()) {
setBeanClass(originalAbd.getBeanClass());
}
setAutowireMode(originalAbd.getAutowireMode());
setDependencyCheck(originalAbd.getDependencyCheck());
setDependsOn(originalAbd.getDependsOn());
setAutowireCandidate(originalAbd.isAutowireCandidate());
copyQualifiersFrom(originalAbd);
setPrimary(originalAbd.isPrimary());
setNonPublicAccessAllowed(originalAbd.isNonPublicAccessAllowed());
setLenientConstructorResolution(originalAbd.isLenientConstructorResolution());
setInitMethodName(originalAbd.getInitMethodName());
setEnforceInitMethod(originalAbd.isEnforceInitMethod());
setDestroyMethodName(originalAbd.getDestroyMethodName());
setEnforceDestroyMethod(originalAbd.isEnforceDestroyMethod());
setMethodOverrides(new MethodOverrides(originalAbd.getMethodOverrides()));
setSynthetic(originalAbd.isSynthetic());
setResource(originalAbd.getResource());
}
else {
setResourceDescription(original.getResourceDescription());
}
}
// 从给定的bean定义(大概是子)中覆盖此bean定义(可能是来自父子继承关系的复制父)的设置。
@Deprecated
public void overrideFrom(AbstractBeanDefinition other) {
overrideFrom((BeanDefinition) other);
}
// 从给定的bean定义(大概是子)中覆盖此bean定义(可能是来自父子继承关系的复制父)的设置。
public void overrideFrom(BeanDefinition other) {
//不为空时
if (StringUtils.hasLength(other.getBeanClassName())) {
setBeanClassName(other.getBeanClassName());
}
//不为空时
if (StringUtils.hasLength(other.getFactoryBeanName())) {
setFactoryBeanName(other.getFactoryBeanName());
}
//不为空时
if (StringUtils.hasLength(other.getFactoryMethodName())) {
setFactoryMethodName(other.getFactoryMethodName());
}
//不为空时
if (StringUtils.hasLength(other.getScope())) {
setScope(other.getScope());
}
setAbstract(other.isAbstract());
setLazyInit(other.isLazyInit());
setRole(other.getRole());
//更新已存在的,新增未拥有的
getConstructorArgumentValues().addArgumentValues(other.getConstructorArgumentValues());
getPropertyValues().addPropertyValues(other.getPropertyValues());
setSource(other.getSource());
copyAttributesFrom(other);
//更新AbstractBeanDefinition中特有的属性
if (other instanceof AbstractBeanDefinition) {
AbstractBeanDefinition otherAbd = (AbstractBeanDefinition) other;
if (otherAbd.hasBeanClass()) {
setBeanClass(otherAbd.getBeanClass());
}
setAutowireCandidate(otherAbd.isAutowireCandidate());
setAutowireMode(otherAbd.getAutowireMode());
copyQualifiersFrom(otherAbd);
setPrimary(otherAbd.isPrimary());
setDependencyCheck(otherAbd.getDependencyCheck());
setDependsOn(otherAbd.getDependsOn());
setNonPublicAccessAllowed(otherAbd.isNonPublicAccessAllowed());
setLenientConstructorResolution(otherAbd.isLenientConstructorResolution());
if (StringUtils.hasLength(otherAbd.getInitMethodName())) {
setInitMethodName(otherAbd.getInitMethodName());
setEnforceInitMethod(otherAbd.isEnforceInitMethod());
}
if (StringUtils.hasLength(otherAbd.getDestroyMethodName())) {
setDestroyMethodName(otherAbd.getDestroyMethodName());
setEnforceDestroyMethod(otherAbd.isEnforceDestroyMethod());
}
getMethodOverrides().addOverrides(otherAbd.getMethodOverrides());
setSynthetic(otherAbd.isSynthetic());
setResource(otherAbd.getResource());
}
else {
//更新资源描述
setResourceDescription(other.getResourceDescription());
}
}
//将所提供的默认值应用于此bean。
public void applyDefaults(BeanDefinitionDefaults defaults) {
setLazyInit(defaults.isLazyInit());
setAutowireMode(defaults.getAutowireMode());
setDependencyCheck(defaults.getDependencyCheck());
setInitMethodName(defaults.getInitMethodName());
setEnforceInitMethod(false);
setDestroyMethodName(defaults.getDestroyMethodName());
setEnforceDestroyMethod(false);
}
//操作beanclass对象
//是否已经设置
public boolean hasBeanClass() {
return (this.beanClass instanceof Class);
}
//设置class对象
public void setBeanClass(Class> beanClass) {
this.beanClass = beanClass;
}
//若已经解析就返回这个包装类。
public Class> getBeanClass() throws IllegalStateException {
Object beanClassObject = this.beanClass;
if (beanClassObject == null) {
throw new IllegalStateException("No bean class specified on bean definition");
}
if (!(beanClassObject instanceof Class)) {
throw new IllegalStateException(
"Bean class name [" + beanClassObject + "] has not been resolved into an actual Class");
}
return (Class>) beanClassObject;
}
//设置className
public void setBeanClassName(String beanClassName) {
this.beanClass = beanClassName;
}
//获取设置className
public String getBeanClassName() {
Object beanClassObject = this.beanClass;
if (beanClassObject instanceof Class) {
return ((Class>) beanClassObject).getName();
}
else {
return (String) beanClassObject;
}
}
//解析类
public Class> resolveBeanClass(ClassLoader classLoader) throws ClassNotFoundException {
String className = getBeanClassName();
if (className == null) {
return null;
}
Class> resolvedClass = ClassUtils.forName(className, classLoader);
this.beanClass = resolvedClass;
return resolvedClass;
}
//scope操作
public void setScope(String scope) {
this.scope = scope;
this.singleton = SCOPE_SINGLETON.equals(scope) || SCOPE_DEFAULT.equals(scope);
this.prototype = SCOPE_PROTOTYPE.equals(scope);
}
public String getScope() {
return this.scope;
}
//设置是否单例
@Deprecated
public void setSingleton(boolean singleton) {
this.scope = (singleton ? SCOPE_SINGLETON : SCOPE_PROTOTYPE);
this.singleton = singleton;
this.prototype = !singleton;
}
public boolean isSingleton() {
return this.singleton;
}
//是否为原型
public boolean isPrototype() {
return this.prototype;
}
//设置是否抽象
public void setAbstract(boolean abstractFlag) {
this.abstractFlag = abstractFlag;
}
public boolean isAbstract() {
return this.abstractFlag;
}
//设置是否懒加载
public void setLazyInit(boolean lazyInit) {
this.lazyInit = lazyInit;
}
public boolean isLazyInit() {
return this.lazyInit;
}
//设置自动装配方式
public void setAutowireMode(int autowireMode) {
this.autowireMode = autowireMode;
}
public int getAutowireMode() {
return this.autowireMode;
}
/**
* Return the resolved autowire code,
* (resolving AUTOWIRE_AUTODETECT to AUTOWIRE_CONSTRUCTOR or AUTOWIRE_BY_TYPE).
* @see #AUTOWIRE_AUTODETECT
* @see #AUTOWIRE_CONSTRUCTOR
* @see #AUTOWIRE_BY_TYPE
*/
public int getResolvedAutowireMode() {
if (this.autowireMode == AUTOWIRE_AUTODETECT) {
// Work out whether to apply setter autowiring or constructor autowiring.
// If it has a no-arg constructor it's deemed to be setter autowiring,
// otherwise we'll try constructor autowiring.
Constructor>[] constructors = getBeanClass().getConstructors();
for (Constructor> constructor : constructors) {
if (constructor.getParameterTypes().length == 0) {
return AUTOWIRE_BY_TYPE;
}
}
return AUTOWIRE_CONSTRUCTOR;
}
else {
return this.autowireMode;
}
}
//设置依赖检查
public void setDependencyCheck(int dependencyCheck) {
this.dependencyCheck = dependencyCheck;
}
public int getDependencyCheck() {
return this.dependencyCheck;
}
//设置依赖
public void setDependsOn(String[] dependsOn) {
this.dependsOn = dependsOn;
}
public String[] getDependsOn() {
return this.dependsOn;
}
//设置是否做为其他bean自动装配的候选者
public void setAutowireCandidate(boolean autowireCandidate) {
this.autowireCandidate = autowireCandidate;
}
public boolean isAutowireCandidate() {
return this.autowireCandidate;
}
//设置是否做为主候选
public void setPrimary(boolean primary) {
this.primary = primary;
}
public boolean isPrimary() {
return this.primary;
}
//设置Qualifier
public void addQualifier(AutowireCandidateQualifier qualifier) {
this.qualifiers.put(qualifier.getTypeName(), qualifier);
}
public boolean hasQualifier(String typeName) {
return this.qualifiers.keySet().contains(typeName);
}
public AutowireCandidateQualifier getQualifier(String typeName) {
return this.qualifiers.get(typeName);
}
/**
* Return all registered qualifiers.
* @return the Set of {@link AutowireCandidateQualifier} objects.
*/
public Set getQualifiers() {
return new LinkedHashSet(this.qualifiers.values());
}
//复制给定source的qualifiers到当前bean(覆盖已有的)
public void copyQualifiersFrom(AbstractBeanDefinition source) {
Assert.notNull(source, "Source must not be null");
this.qualifiers.putAll(source.qualifiers);
}
//设置是否允许访问非公开的构造方法
public void setNonPublicAccessAllowed(boolean nonPublicAccessAllowed) {
this.nonPublicAccessAllowed = nonPublicAccessAllowed;
}
public boolean isNonPublicAccessAllowed() {
return this.nonPublicAccessAllowed;
}
//设置是否以一种宽松的方式解析构造参数
public void setLenientConstructorResolution(boolean lenientConstructorResolution) {
this.lenientConstructorResolution = lenientConstructorResolution;
}
public boolean isLenientConstructorResolution() {
return this.lenientConstructorResolution;
}
//记录构造函数的注入属性
public void setConstructorArgumentValues(ConstructorArgumentValues constructorArgumentValues) {
this.constructorArgumentValues =
(constructorArgumentValues != null ? constructorArgumentValues : new ConstructorArgumentValues());
}
public ConstructorArgumentValues getConstructorArgumentValues() {
return this.constructorArgumentValues;
}
public boolean hasConstructorArgumentValues() {
return !this.constructorArgumentValues.isEmpty();
}
//记录普通的属性集合
public void setPropertyValues(MutablePropertyValues propertyValues) {
this.propertyValues = (propertyValues != null ? propertyValues : new MutablePropertyValues());
}
public MutablePropertyValues getPropertyValues() {
return this.propertyValues;
}
public void setMethodOverrides(MethodOverrides methodOverrides) {
this.methodOverrides = (methodOverrides != null ? methodOverrides : new MethodOverrides());
}
public MethodOverrides getMethodOverrides() {
return this.methodOverrides;
}
//工厂类操作
public void setFactoryBeanName(String factoryBeanName) {
this.factoryBeanName = factoryBeanName;
}
public String getFactoryBeanName() {
return this.factoryBeanName;
}
public void setFactoryMethodName(String factoryMethodName) {
this.factoryMethodName = factoryMethodName;
}
public String getFactoryMethodName() {
return this.factoryMethodName;
}
//初始化方法和销毁方法操作
public void setInitMethodName(String initMethodName) {
this.initMethodName = initMethodName;
}
public String getInitMethodName() {
return this.initMethodName;
}
public void setEnforceInitMethod(boolean enforceInitMethod) {
this.enforceInitMethod = enforceInitMethod;
}
public boolean isEnforceInitMethod() {
return this.enforceInitMethod;
}
public void setDestroyMethodName(String destroyMethodName) {
this.destroyMethodName = destroyMethodName;
}
public String getDestroyMethodName() {
return this.destroyMethodName;
}
public void setEnforceDestroyMethod(boolean enforceDestroyMethod) {
this.enforceDestroyMethod = enforceDestroyMethod;
}
public boolean isEnforceDestroyMethod() {
return this.enforceDestroyMethod;
}
/**
* 设置是否这个bean的定义是“合成”,不是由应用程序定义的
* Set whether this bean definition is 'synthetic', that is, not defined
* by the application itself (for example, an infrastructure bean such
* as a helper for auto-proxying, created through {@code <aop:config>}).
*/
public void setSynthetic(boolean synthetic) {
this.synthetic = synthetic;
}
public boolean isSynthetic() {
return this.synthetic;
}
//设置role
public void setRole(int role) {
this.role = role;
}
//获取role
public int getRole() {
return this.role;
}
//为这个bean定义一个可读的描述。
public void setDescription(String description) {
this.description = description;
}
//获取bean的描述
public String getDescription() {
return this.description;
}
//设置此bean定义的资源(为了在出错时显示上下文)。
public void setResource(Resource resource) {
this.resource = resource;
}
//获取此bean定义的资源(为了在出错时显示上下文)。
public Resource getResource() {
return this.resource;
}
//设置该bean定义的资源的描述(用于显示错误情况下的上下文)。
public void setResourceDescription(String resourceDescription) {
this.resource = new DescriptiveResource(resourceDescription);
}
public String getResourceDescription() {
return (this.resource != null ? this.resource.getDescription() : null);
}
//设置源(e.g. decorated)BeanDefinition,如果任何。
public void setOriginatingBeanDefinition(BeanDefinition originatingBd) {
this.resource = new BeanDefinitionResource(originatingBd);
}
public BeanDefinition getOriginatingBeanDefinition() {
return (this.resource instanceof BeanDefinitionResource ?
((BeanDefinitionResource) this.resource).getBeanDefinition() : null);
}
//验证当前bean的定义
public void validate() throws BeanDefinitionValidationException {
//无法将静态工厂方法与方法重写组合在一起
if (!getMethodOverrides().isEmpty() && getFactoryMethodName() != null) {
throw new BeanDefinitionValidationException(
"Cannot combine static factory method with method overrides: " +
"the static factory method must create the instance");
}
//已经设置beanClass属性,校验
if (hasBeanClass()) {
prepareMethodOverrides();
}
}
//开始准备验证这个bean定义的重写方法,检查指定名称的方法是否存在。
public void prepareMethodOverrides() throws BeanDefinitionValidationException {
//检测当前lookup methods是否存在
MethodOverrides methodOverrides = getMethodOverrides();
if (!methodOverrides.isEmpty()) {
for (MethodOverride mo : methodOverrides.getOverrides()) {
prepareMethodOverride(mo);
}
}
}
//准备验证给定的重写方法
//检查指定名称的方法是否存在,如果没有找到,则标记为未重载。
protected void prepareMethodOverride(MethodOverride mo) throws BeanDefinitionValidationException {
int count = ClassUtils.getMethodCountForName(getBeanClass(), mo.getMethodName());
//class对象中未找到当前方法,报错
if (count == 0) {
throw new BeanDefinitionValidationException(
"Invalid method override: no method with name '" + mo.getMethodName() +
"' on class [" + getBeanClassName() + "]");
}
else if (count == 1) {
//标记不是重载,以避免类型检查的开销。
mo.setOverloaded(false);
}
}
/**
* Public declaration of Object's {@code clone()} method.
* Delegates to {@link #cloneBeanDefinition()}.
* @see Object#clone()
*/
@Override
public Object clone() {
return cloneBeanDefinition();
}
/**
* Clone this bean definition.
* To be implemented by concrete subclasses.
* @return the cloned bean definition object
*/
public abstract AbstractBeanDefinition cloneBeanDefinition();
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof AbstractBeanDefinition)) {
return false;
}
AbstractBeanDefinition that = (AbstractBeanDefinition) other;
if (!ObjectUtils.nullSafeEquals(getBeanClassName(), that.getBeanClassName())) return false;
if (!ObjectUtils.nullSafeEquals(this.scope, that.scope)) return false;
if (this.abstractFlag != that.abstractFlag) return false;
if (this.lazyInit != that.lazyInit) return false;
if (this.autowireMode != that.autowireMode) return false;
if (this.dependencyCheck != that.dependencyCheck) return false;
if (!Arrays.equals(this.dependsOn, that.dependsOn)) return false;
if (this.autowireCandidate != that.autowireCandidate) return false;
if (!ObjectUtils.nullSafeEquals(this.qualifiers, that.qualifiers)) return false;
if (this.primary != that.primary) return false;
if (this.nonPublicAccessAllowed != that.nonPublicAccessAllowed) return false;
if (this.lenientConstructorResolution != that.lenientConstructorResolution) return false;
if (!ObjectUtils.nullSafeEquals(this.constructorArgumentValues, that.constructorArgumentValues)) return false;
if (!ObjectUtils.nullSafeEquals(this.propertyValues, that.propertyValues)) return false;
if (!ObjectUtils.nullSafeEquals(this.methodOverrides, that.methodOverrides)) return false;
if (!ObjectUtils.nullSafeEquals(this.factoryBeanName, that.factoryBeanName)) return false;
if (!ObjectUtils.nullSafeEquals(this.factoryMethodName, that.factoryMethodName)) return false;
if (!ObjectUtils.nullSafeEquals(this.initMethodName, that.initMethodName)) return false;
if (this.enforceInitMethod != that.enforceInitMethod) return false;
if (!ObjectUtils.nullSafeEquals(this.destroyMethodName, that.destroyMethodName)) return false;
if (this.enforceDestroyMethod != that.enforceDestroyMethod) return false;
if (this.synthetic != that.synthetic) return false;
if (this.role != that.role) return false;
return super.equals(other);
}
@Override
public int hashCode() {
int hashCode = ObjectUtils.nullSafeHashCode(getBeanClassName());
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.scope);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.constructorArgumentValues);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.propertyValues);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.factoryBeanName);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.factoryMethodName);
hashCode = 29 * hashCode + super.hashCode();
return hashCode;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("class [");
sb.append(getBeanClassName()).append("]");
sb.append("; scope=").append(this.scope);
sb.append("; abstract=").append(this.abstractFlag);
sb.append("; lazyInit=").append(this.lazyInit);
sb.append("; autowireMode=").append(this.autowireMode);
sb.append("; dependencyCheck=").append(this.dependencyCheck);
sb.append("; autowireCandidate=").append(this.autowireCandidate);
sb.append("; primary=").append(this.primary);
sb.append("; factoryBeanName=").append(this.factoryBeanName);
sb.append("; factoryMethodName=").append(this.factoryMethodName);
sb.append("; initMethodName=").append(this.initMethodName);
sb.append("; destroyMethodName=").append(this.destroyMethodName);
if (this.resource != null) {
sb.append("; defined in ").append(this.resource.getDescription());
}
return sb.toString();
}
}
以上是 AbstractBeanDefinition 中定义的一些常量和属性,该类中还有一部分是操作这些属性的 set 和 get 方法,这些方法都由子类来操作,且应用程序中真正使用的也是这些子类 BeanDefinition。
先来看 AbstractBeanDefinition 直接实现类:RootBeanDefinition、GenericBeanDefinition、ChildBeanDefinition。
RootBeanDefinition
该类继承自 AbstractBeanDefinition,它可以单独作为一个 BeanDefinition,也可以作为其他 BeanDefinition 的父类。
RootBeanDefinition 在 AbstractBeanDefinition 的基础上定义了更多属性。
public class RootBeanDefinition extends AbstractBeanDefinition {
// BeanDefinitionHolder 存储 Bean 的名称、别名、BeanDefinition
private BeanDefinitionHolder decoratedDefinition;
// AnnotatedElement 是java反射包的接口,通过它可以查看 Bean 的注解信息
private AnnotatedElement qualifiedElement;
// 允许缓存
boolean allowCaching = true;
// 工厂方法是否唯一
boolean isFactoryMethodUnique = false;
// 封装了 java.lang.reflect.Type,提供了泛型相关的操作
volatile ResolvableType targetType;
// 缓存 Class,表示 RootBeanDefinition 存储哪个类的信息
volatile Class> resolvedTargetType;
// 缓存工厂方法的返回类型
volatile ResolvableType factoryMethodReturnType;
// 这是以下四个构造方法字段的通用锁
final Object constructorArgumentLock = new Object();
// 用于缓存已解析的构造方法或工厂方法
Executable resolvedConstructorOrFactoryMethod;
// 将构造方法参数标记为已解析
boolean constructorArgumentsResolved = false;
// 用于缓存完全解析的构造方法参数
Object[] resolvedConstructorArguments;
// 缓存待解析的构造方法参数
Object[] preparedConstructorArguments;
// 这是以下两个后处理字段的通用锁
final Object postProcessingLock = new Object();
// 表明是否被 MergedBeanDefinitionPostProcessor 处理过
boolean postProcessed = false;
// 在生成代理的时候会使用,表明是否已经生成代理
volatile Boolean beforeInstantiationResolved;
// 实际缓存的类型是 Constructor、Field、Method 类型
private Set externallyManagedConfigMembers;
// InitializingBean中 的 init 回调函数名 afterPropertiesSet 会在这里记录,以便进行生命周期回调
private Set externallyManagedInitMethods;
// DisposableBean 的 destroy 回调函数名 destroy 会在这里记录,以便进生命周期回调
private Set externallyManagedDestroyMethods;
...
}
ChildBeanDefinition
该类继承自 AbstractBeanDefinition。其相当于一个子类,不可以单独存在,必须依赖一个父 BeanDetintion,构造 ChildBeanDefinition 时,通过构造方法传入父 BeanDetintion 的名称或通过 setParentName 设置父名称。它可以从父类继承方法参数、属性值,并可以重写父类的方法,同时也可以增加新的属性或者方法。若重新定义 init 方法,destroy 方法或者静态工厂方法,ChildBeanDefinition 会重写父类的设置。
从 Spring 2.5 开始,以编程方式注册 Bean 定义的首选方法是 GenericBeanDefinition,GenericBeanDefinition 可以有效替代 ChildBeanDefinition 的绝大分部使用场合。
在配置文件中可以定义父和子,父用RootBeanDefinition表示,而子用ChildBeanDefiniton表示,而没有父的就使用RootBeanDefinition表示
public class ChildBeanDefinition extends AbstractBeanDefinition {
//父bean名称
private String parentName;
//构造方法
public ChildBeanDefinition(String parentName) {
super();
this.parentName = parentName;
}
//构造方法
public ChildBeanDefinition(String parentName, MutablePropertyValues pvs) {
super(null, pvs);
this.parentName = parentName;
}
//构造方法
public ChildBeanDefinition(
String parentName, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
super(cargs, pvs);
this.parentName = parentName;
}
//构造方法
public ChildBeanDefinition(
String parentName, Class> beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
super(cargs, pvs);
this.parentName = parentName;
setBeanClass(beanClass);
}
//构造方法
public ChildBeanDefinition(
String parentName, String beanClassName, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
super(cargs, pvs);
this.parentName = parentName;
setBeanClassName(beanClassName);
}
//构造方法
public ChildBeanDefinition(ChildBeanDefinition original) {
super((BeanDefinition) original);
}
//设置parentName
public void setParentName(String parentName) {
this.parentName = parentName;
}
public String getParentName() {
return this.parentName;
}
//验证当前bean定义
@Override
public void validate() throws BeanDefinitionValidationException {
super.validate();
if (this.parentName == null) {
throw new BeanDefinitionValidationException("'parentName' must be set in ChildBeanDefinition");
}
}
//深度拷贝bean定义
@Override
public AbstractBeanDefinition cloneBeanDefinition() {
return new ChildBeanDefinition(this);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ChildBeanDefinition)) {
return false;
}
ChildBeanDefinition that = (ChildBeanDefinition) other;
return (ObjectUtils.nullSafeEquals(this.parentName, that.parentName) && super.equals(other));
}
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.parentName) * 29 + super.hashCode();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Child bean with parent '");
sb.append(this.parentName).append("': ").append(super.toString());
return sb.toString();
}
}
GenericBeanDefinition
GenericBeanDefinition 是 Spring 2.5 以后新引入的 BeanDefinition,是 ChildBeanDefinition 更好的替代者,它同样可以通过 setParentName 方法设置父 BeanDefinition。
最后三个 BeanDefinition 既实现了 AnnotatedBeanDefinition 接口,又间接继承 AbstractBeanDefinition 抽象类,这些 BeanDefinition 描述的都是注解形式的 Bean。
public class GenericBeanDefinition extends AbstractBeanDefinition {
//父bean名称
private String parentName;
//构造方法
public GenericBeanDefinition() {
super();
}
//构造方法
public GenericBeanDefinition(BeanDefinition original) {
super(original);
}
//设置parentName
public void setParentName(String parentName) {
this.parentName = parentName;
}
public String getParentName() {
return this.parentName;
}
//深度拷贝自身
@Override
public AbstractBeanDefinition cloneBeanDefinition() {
return new GenericBeanDefinition(this);
}
@Override
public boolean equals(Object other) {
return (this == other || (other instanceof GenericBeanDefinition && super.equals(other)));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Generic bean");
if (this.parentName != null) {
sb.append(" with parent '").append(this.parentName).append("'");
}
sb.append(": ").append(super.toString());
return sb.toString();
}
}
ConfigurationClassBeanDefinition
该类继承自 RootBeanDefinition ,并实现了 AnnotatedBeanDefinition 接口。这个 BeanDefinition 用来描述在标注 @Configuration 注解的类中,通过 @Bean 注解实例化的 Bean。
其功能特点如下:
- 1、如果 @Bean 注解没有指定 Bean 的名字,默认会用方法的名字命名 Bean。
- 2、标注 @Configuration 注解的类会成为一个工厂类,而标注 @Bean 注解的方法会成为工厂方法,通过工厂方法实例化 Bean,而不是直接通过构造方法初始化。
- 3、标注 @Bean 注解的类会使用构造方法自动装配
RootBeanDefinition
public class RootBeanDefinition extends AbstractBeanDefinition {
//默认允许缓存
boolean allowCaching = true;
//bean定义的持有者(修饰bean定义)
private BeanDefinitionHolder decoratedDefinition;
//bean指定的目标类型定义
private volatile Class> targetType;
//是否已经指定引用非重载方法的工厂方法名。
boolean isFactoryMethodUnique = false;
//用于构造函数注入属性的锁对象
final Object constructorArgumentLock = new Object();
/** Package-visible field for caching the resolved constructor or factory method */
//缓存已解析的构造函数或工厂方法
Object resolvedConstructorOrFactoryMethod;
/** Package-visible field that marks the constructor arguments as resolved */
//将构造函数参数标记为已解析。
boolean constructorArgumentsResolved = false;
/** Package-visible field for caching fully resolved constructor arguments */
//缓存完全解析的构造函数参数
Object[] resolvedConstructorArguments;
/** Package-visible field for caching partly prepared constructor arguments */
//缓存部分准备好的构造函数参数
Object[] preparedConstructorArguments;
//后处理锁对象
final Object postProcessingLock = new Object();
/** Package-visible field that indicates MergedBeanDefinitionPostProcessor having been applied */
//表明已应用mergedbeandefinitionpostprocessor
boolean postProcessed = false;
/** Package-visible field that indicates a before-instantiation post-processor having kicked in */
//指示已启动的一个实例化之前的后置处理器。
volatile Boolean beforeInstantiationResolved;
//1.
private Set externallyManagedConfigMembers;
//2.
private Set externallyManagedInitMethods;
//3.
private Set externallyManagedDestroyMethods;
//构造方法
public RootBeanDefinition() {
super();
}
//构造方法
public RootBeanDefinition(Class> beanClass) {
super();
setBeanClass(beanClass);
}
//构造方法
@Deprecated
public RootBeanDefinition(Class beanClass, boolean singleton) {
super();
setBeanClass(beanClass);
setSingleton(singleton);
}
//构造方法
@Deprecated
public RootBeanDefinition(Class beanClass, int autowireMode) {
super();
setBeanClass(beanClass);
setAutowireMode(autowireMode);
}
//构造方法
public RootBeanDefinition(Class> beanClass, int autowireMode, boolean dependencyCheck) {
super();
setBeanClass(beanClass);
setAutowireMode(autowireMode);
if (dependencyCheck && getResolvedAutowireMode() != AUTOWIRE_CONSTRUCTOR) {
setDependencyCheck(RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
}
}
//构造方法
@Deprecated
public RootBeanDefinition(Class beanClass, MutablePropertyValues pvs) {
super(null, pvs);
setBeanClass(beanClass);
}
//构造方法
@Deprecated
public RootBeanDefinition(Class beanClass, MutablePropertyValues pvs, boolean singleton) {
super(null, pvs);
setBeanClass(beanClass);
setSingleton(singleton);
}
//构造方法
public RootBeanDefinition(Class> beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
super(cargs, pvs);
setBeanClass(beanClass);
}
//构造方法
public RootBeanDefinition(String beanClassName) {
setBeanClassName(beanClassName);
}
//构造方法
public RootBeanDefinition(String beanClassName, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
super(cargs, pvs);
setBeanClassName(beanClassName);
}
//构造方法
public RootBeanDefinition(RootBeanDefinition original) {
super((BeanDefinition) original);
this.allowCaching = original.allowCaching;
this.decoratedDefinition = original.decoratedDefinition;
this.targetType = original.targetType;
this.isFactoryMethodUnique = original.isFactoryMethodUnique;
}
//构造方法
RootBeanDefinition(BeanDefinition original) {
super(original);
}
//不含有父bean定义
public String getParentName() {
return null;
}
//不含有父bean定义,否则抛错
public void setParentName(String parentName) {
if (parentName != null) {
throw new IllegalArgumentException("Root bean cannot be changed into a child bean with parent reference");
}
}
//设置bean定义的修饰者(对bean定义进行一层修饰,持有bean定义)
public void setDecoratedDefinition(BeanDefinitionHolder decoratedDefinition) {
this.decoratedDefinition = decoratedDefinition;
}
public BeanDefinitionHolder getDecoratedDefinition() {
return this.decoratedDefinition;
}
//设置这个bean指定的目标类型定义,如果已知的提前。
public void setTargetType(Class> targetType) {
this.targetType = targetType;
}
public Class> getTargetType() {
return this.targetType;
}
//指定引用非重载方法的工厂方法名。
public void setUniqueFactoryMethodName(String name) {
Assert.hasText(name, "Factory method name must not be empty");
setFactoryMethodName(name);
this.isFactoryMethodUnique = true;
}
/**
* Check whether the given candidate qualifies as a factory method.
* 检查给定的候选方法是否为工厂方法
*/
public boolean isFactoryMethod(Method candidate) {
return (candidate != null && candidate.getName().equals(getFactoryMethodName()));
}
//返回解析后的工厂方法作为Java对象方法,如果可用。
public Method getResolvedFactoryMethod() {
synchronized (this.constructorArgumentLock) {
Object candidate = this.resolvedConstructorOrFactoryMethod;
return (candidate instanceof Method ? (Method) candidate : null);
}
}
public void registerExternallyManagedConfigMember(Member configMember) {
synchronized (this.postProcessingLock) {
if (this.externallyManagedConfigMembers == null) {
this.externallyManagedConfigMembers = new HashSet(1);
}
this.externallyManagedConfigMembers.add(configMember);
}
}
public boolean isExternallyManagedConfigMember(Member configMember) {
synchronized (this.postProcessingLock) {
return (this.externallyManagedConfigMembers != null &&
this.externallyManagedConfigMembers.contains(configMember));
}
}
public void registerExternallyManagedInitMethod(String initMethod) {
synchronized (this.postProcessingLock) {
if (this.externallyManagedInitMethods == null) {
this.externallyManagedInitMethods = new HashSet(1);
}
this.externallyManagedInitMethods.add(initMethod);
}
}
public boolean isExternallyManagedInitMethod(String initMethod) {
synchronized (this.postProcessingLock) {
return (this.externallyManagedInitMethods != null &&
this.externallyManagedInitMethods.contains(initMethod));
}
}
public void registerExternallyManagedDestroyMethod(String destroyMethod) {
synchronized (this.postProcessingLock) {
if (this.externallyManagedDestroyMethods == null) {
this.externallyManagedDestroyMethods = new HashSet(1);
}
this.externallyManagedDestroyMethods.add(destroyMethod);
}
}
public boolean isExternallyManagedDestroyMethod(String destroyMethod) {
synchronized (this.postProcessingLock) {
return (this.externallyManagedDestroyMethods != null &&
this.externallyManagedDestroyMethods.contains(destroyMethod));
}
}
@Override
public RootBeanDefinition cloneBeanDefinition() {
return new RootBeanDefinition(this);
}
@Override
public boolean equals(Object other) {
return (this == other || (other instanceof RootBeanDefinition && super.equals(other)));
}
@Override
public String toString() {
return "Root bean: " + super.toString();
}
}
AnnotatedGenericBeanDefinition
该类继承自 GenericBeanDefinition ,并实现了 AnnotatedBeanDefinition 接口。这个 BeanDefinition 用来描述标注 @Configuration 注解的 Bean。
ScannedGenericBeanDefinition
该类继承自 GenericBeanDefinition ,并实现了 AnnotatedBeanDefinition 接口。这个 BeanDefinition 用来描述标注 @Component 注解的 Bean,其派生注解如 @Service、@Controller 也同理。
总结
最后,我们来做个总结。BeanDefinition 主要是用来描述 Bean,其存储了 Bean 的相关信息,Spring 实例化 Bean 时需读取该 Bean 对应的 BeanDefinition。BeanDefinition 整体可以分为两类,一类是描述通用的 Bean,还有一类是描述注解形式的 Bean。一般前者在 XML 时期定义