【Spring源码深度解析学习系列】复杂标签属性解析(四)

时间:2023-03-09 03:26:22
【Spring源码深度解析学习系列】复杂标签属性解析(四)

一、创建用于属性承载的BeanDefinition

BeanDefiniton是一个接口,在Spring中存在三种实现:RootBeanDefinition、ChildBeanDefinition、GenericBeanDefiniton.三种实现均继承了AbstractBeanFactory,其中BeanDefinition是配置文件<bean>元素标签在容器中的内部表示形式。<bean>元素拥有class、scope、lazy-init等配置属性,BeanDefinition则提供了相应的beanClass、scope、lazyInit属性,BeanDefinition和<bean>中的属性是一一对应的。其中RootBeanDefinition是最常用的实现类,它对应一般性的<bean>元素标签,GenericBeanDefiniton是自2.5版本以后新加入的bean文件配置属性定义类,是一站式服务类。

在配置文件中可以定义父<bean>和子<bean>。父<bean>用RootBeanDefinition表示,而子<bean>用CgildBeanDefiniton表示,而没有父<bean>的,bea>就使用RootBeanDefinition表示。AbstractBeanDefinition对两者共同的类信息进行抽象。

Spring通过BeanDefinition将配置文件中的<bean>配置信息转换为容器的内部表示,并将这些BeanDefiniton注册到BeanDefinitonRegistry中。Spring容器的BeanDefinitonRegistry就像是Spring配置信息的内存数据库,主要是以map的形式保存,后续操作直接从BeanDefinitionRegistry中读取配置信息。

【Spring源码深度解析学习系列】复杂标签属性解析(四)

1、要解析属性首先要创建用于承载属性的实例,也就是创建GenericBeanDefiniton类型的实例。

【Spring源码深度解析学习系列】复杂标签属性解析(四)

2、解析各种属性

当我们创建了bean信息的承载实例后,变可以进行bean信息的各种属性解析了,首先我们进入parseBeanDefinitionAttribute方法。该方法是对element所有元素属性进行解析。

【Spring源码深度解析学习系列】复杂标签属性解析(四)

二、AbstractBeanDefinition属性

public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor
implements BeanDefinition, Cloneable { /**
* Constant for the default scope name: {@code ""}, equivalent to singleton
* status unless overridden from a parent bean definition (if applicable).
* bean的作用范围,对应bean属性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
*/
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
* @deprecated as of Spring 3.0: If you are using mixed autowiring strategies,
* use annotation-based autowiring for clearer demarcation of autowiring needs.
*/
@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.
*/
public static final String INFER_METHOD = "(inferred)"; private volatile Object beanClass; private String scope = SCOPE_DEFAULT; private boolean abstractFlag = false;
//是否延迟加载,对应bean属性lazy-init
private boolean lazyInit = false;
//自动注入模式,对应bean属性autowire
private int autowireMode = AUTOWIRE_NO;
//依赖检查
private int dependencyCheck = DEPENDENCY_CHECK_NONE;
//用来表示一个bean的实例化依靠另一个bean先实例化,对应bean属性的depend-on
private String[] dependsOn;
//autowire-candicate属性设置为false,这样容器在查找自动装配对象时将不考虑bean,则它不会被考虑作为其他bean自动装配的候选者,但是该bean本身还是可以使用自动装配来注入其他bean的
//对应bean属性的autowire-candidate
private boolean autowireCandidate = true;
//自动装配时当出现多个bean候选者时,将作为首选者,对应bean属性primary
private boolean primary = false;
//用于记录Qualifier,对应子元素qualifier
private final Map<String, AutowireCandidateQualifier> qualifiers =
new LinkedHashMap<String, AutowireCandidateQualifier>(0);
//允许访问非公开的构造器和方法,程序设置
private boolean nonPublicAccessAllowed = true;
//是否以一种宽松的模式解析构造函数,默认为true
private boolean lenientConstructorResolution = true;
//对应bean属性的factory-bean
private String factoryBeanName;
//对应bean属性的factory-method
private String factoryMethodName;
//记录构造函数注入属性,对应bean属性constructor-arg
private ConstructorArgumentValues constructorArgumentValues;
//普通属性集合
private MutablePropertyValues propertyValues;
//方法重写的持有者,记录lookup-method replaced-method元素
private MethodOverrides methodOverrides = new MethodOverrides();
//初始化方法,对应bean属性的init-method
private String initMethodName;
//销毁方法,对应bean属性的deatory-method
private String destroyMethodName;
//是否执行init-method程序设置
private boolean enforceInitMethod = true;
//是否执行deatory-method程序设置
private boolean enforceDestroyMethod = true;
//是否是用户定义的而不是应用程序本身定义的,创建AOP时候为true,程序设置
private boolean synthetic = false;
//定义这个bean的应用,APPLICATION:用户;INFRASTRUCTURE:完全内部使用,与用户无关,SUPPORT:某些复杂配置的一部分
private int role = BeanDefinition.ROLE_APPLICATION;
//bean的描述信息
private String description;
//bean定义的资源
private Resource resource;