Spring容器启动的过程中,会将Bean解析成Spring内部的BeanDefinition结构,结构图如下:
这个图中所有的类或接口的源码,我们一步步看吧!
## 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定义的名称,如果有的话 <bean parent="">
String getParentName();
void setParentName(String parentName);
//获取bean对象className <bean class="">
String getBeanClassName();
void setBeanClassName(String beanClassName);
//定义创建该Bean对象的工厂l类 <bean factory-bean="">
String getFactoryBeanName();
void setFactoryBeanName(String factoryBeanName);
//定义创建该Bean对象的工厂方法 <bean factory-method="">
String getFactoryMethodName();
void setFactoryMethodName(String factoryMethodName);
//<bean scope="singleton/prototype">
String getScope();
void setScope(String scope);
//懒加载 <bean lazy-init="true/false">
boolean isLazyInit();
void setLazyInit(boolean lazyInit);
//依赖对象 <bean depends-on="">
String[] getDependsOn();
void setDependsOn(String[] dependsOn);
//是否为被自动装配 <bean autowire-candidate="true/false">
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();
}
## AbstractBeanDefinition##
//抽象类:基础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<String, AutowireCandidateQualifier> qualifiers =
new LinkedHashMap<String, AutowireCandidateQualifier>(0);
//允许访问非公开的构造器方法
private boolean nonPublicAccessAllowed = true;
/**
* 是否以一种宽松的模式解析构造函数,默认为 true
* 如果为false,则下面情况报错
* interface ITest{}
* class ITestImpl implements ITest{}
* class Main{
* Main(ITest){}
* Main(ITestImpl){}
* }
*/
private boolean lenientConstructorResolution = true;
/**
* 记录构造函数注入属性,如:
*<bean id="student" class="com.rc.sp.Student">
* <constructor-arg name="id" value="1"/>
* <constructor-arg name="score">
* <map>
* <entry key="math" value="90"/>
* <entry key="english" value="85"/>
* </map>
* </constructor-arg>
*/</bean>
private ConstructorArgumentValues constructorArgumentValues;
/**
* 普通属性的集合,如:
* <bean id="student" class="com.rc.sp.Student">
* <property name="id" value="1"/>
* <property name="dao" ref="dao" />
* <property name="map">
* <map>
* <entry key="math" value="90"/>
* <entry key="english" value="85"/>
* </map>
* </property>
* </bean>
*/
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(