目录
承接上篇介绍BeanDefinition的博客:https://blog.csdn.net/dhaiuda/article/details/83183497
什么是AbstractBeanDefinition
AbstractBeanDefinition直接继承BeanDefiniton,实现了BeanDefinition定义的一系列操作,定义了描述Bean画像的一系列属性,在AbstractBeanDefinition的基础上,Spring衍生出了一系列具有特殊用途的BeanDefinition
源码解析
继承结构
public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor
implements BeanDefinition, Cloneable
定义的常量
这一些常量会直接影响到spring实例化Bean时的策略,个人理解,其实完全可以不定义这些常量,在使用的时候自行在代码中判断,也可以实现多策略实例化Bean,Spring定义这些常量的原因很简单,便于维护,让读代码的人知道每个值的意义,这点在自己写代码时可以注意一下
/**
* Constant for the default scope name: {@code ""}, equivalent to singleton
* status unless overridden from a parent bean definition (if applicable).
*/
默认的SCOPE,默认是单例
public static final String SCOPE_DEFAULT = "";
/**
* 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
*/
根据Bean的名字进行自动装配,即autowired属性的值为byname
public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
/**
* Constant that indicates autowiring bean properties by type.
* @see #setAutowireMode
*/
根据Bean的类型进行自动装配,对应调用setter函数装配属性,即autowired属性的值为byType
public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
/**
* Constant that indicates autowiring a constructor.
* @see #setAutowireMode
*/
自动装配构造函数的形参,完成对应属性的自动装配,即autowired属性的值为byConstructor
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
* @deprecated as of Spring 3.0: If you are using mixed autowiring strategies,
* use annotation-based autowiring for clearer demarcation of autowiring needs.
*/
通过Bean的class推断适当的自动装配策略(autowired=autodetect),如果Bean定义有有参构造函数,则通过自动装配构造函数形参,完成对应属性的自动装配(AUTOWIRE_CONSTRUCTOR),否则,使用setter函数(AUTOWIRE_BY_TYPE)
@Deprecated
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;
/**
* Constant that indicates the container should attempt to infer the
* {@link #setDestroyMethodName destroy method name} for a bean as opposed to
* explicit specification of a method name. The value {@value} is specifically
* designed to include characters otherwise illegal in a method name, ensuring
* no possibility of collisions with legitimately named methods having the same
* name.
* <p>Currently, the method names detected during destroy method inference
* are "close" and "shutdown", if present on the specific bean class.
*/
若Bean未指定销毁方法,容器应该尝试推断Bean的销毁方法的名字,目前来说,推断的销毁方法的名字一般为close或是shutdown
(即未指定Bean的销毁方法,但是内部定义了名为close或是shutdown的方法,则容器推断其为销毁方法)
public static final String INFER_METHOD = "(inferred)";
属性
AbstractBeanDefinition定义的属性基本囊括了Bean实例化需要的所有信息
Bean的class对象或是类的全限定名
@Nullable
private volatile Object beanClass;
默认的scope是单例
@Nullable
private String scope = SCOPE_DEFAULT;
默认不为抽象类
private boolean abstractFlag = false;
默认不是懒加载
private boolean lazyInit = false;
默认不进行自动装配
private int autowireMode = AUTOWIRE_NO;
默认不进行依赖检查
private int dependencyCheck = DEPENDENCY_CHECK_NONE;
存放依赖类的名字
@Nullable
private String[] dependsOn;
是自动装配的候选者,意味着可以自动装配到其他Bean的某个属性中
private boolean autowireCandidate = true;
包括本Bean在内,当自动装配有多个候选者时,是否优先注入,即@Primary注解
private boolean primary = false;
这个不是很清楚,查看了这个类的定义,AutowireCandidateQualifier用于解析自动装配的候选者
private final Map<String, AutowireCandidateQualifier> qualifiers = new LinkedHashMap<>();
用于初始化Bean的回调函数,一旦指定,这个方法会覆盖工厂方法以及构造函数中的元数据
我理解为通过这个函数的逻辑初始化Bean,而不是构造函数或是工厂方法
@Nullable
private Supplier<?> instanceSupplier;
是否允许访问非public方法和属性,应用于构造函数、工厂方法、init、destroy方法的解析,具体作用是什么我也不是很清楚
private boolean nonPublicAccessAllowed = true;
指定解析构造函数的模式,是宽松还是严格(什么是宽松、什么是严格,我没有找到解释)
private boolean lenientConstructorResolution = true;
工厂类名
@Nullable
private String factoryBeanName;
工厂方法名
@Nullable
private String factoryMethodName;
存储构造函数形参的值
@Nullable
private ConstructorArgumentValues constructorArgumentValues;
存储.property文件中的键值对
@Nullable
private MutablePropertyValues propertyValues;
存储被IOC容器覆盖的方法的相关信息(例如replace-method属性指定的函数)
@Nullable
private MethodOverrides methodOverrides;
init函数的名字
@Nullable
private String initMethodName;
destory函数的名字
@Nullable
private String destroyMethodName;
文档解释,不知道啥意思,Specify whether or not the configured init method is the default
private boolean enforceInitMethod = true;
文档解释,不知道啥意思,Specify whether or not the configured destroy method is the default
private boolean enforceDestroyMethod = true;
是否是合成类(是不是应用自定义的,例如生成AOP代理时,会用到某些辅助类,这些辅助类不是应用自定义的,这个就是合成类)
private boolean synthetic = false;
Bean的角色,为用户自定义Bean
private int role = BeanDefinition.ROLE_APPLICATION;
Bean的描述
@Nullable
private String description;
@Nullable
private Resource resource;
函数就不解析了,基本都是一些set、get操作
总结
AbstractBeanDefinition定义了一系列描述Bean画像的属性,同时实现了BeanDefinition定义的方法,通过这个类,可以窥见Bean的某些默认设置(例如默认为单例),这个类的属性基本可以在xml配置文件中找到相应的属性或是标签(例如该类的scope属性对应xml配置文件中的scope属性)
本文难免有错误,如有错误,欢迎指出