我们点开BeanDefiniton的继承关系可以发现,直接实现BeanDefiniton的子类有两个:
- 注解bean定义AnnotatedBeanDefinition
- 以及抽象类AbstractBeanDefinition
AbstractBeanDefinition
package org.springframework.beans.factory.support;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import org.springframework.beans.BeanMetadataAttributeAccessor;
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.ResolvableType;
import org.springframework.core.io.DescriptiveResource;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@SuppressWarnings("serial")
public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor
implements BeanDefinition, Cloneable {
//定义初始化范围
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;
//自动注入的通过构造器注入
public static final int AUTOWIRE_CONSTRUCTOR = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
//自动检测注入
@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;
//简单类型检查
public static final int DEPENDENCY_CHECK_SIMPLE = 2;
//检查依赖的所有属性
public static final int DEPENDENCY_CHECK_ALL = 3;
使用 Bean 注解的方法所注册的 Bean 对象,如果用户不设置
destroyMethod 属性,则其属性值为
AbstractBeanDefinition.INFER_METHOD。
此时 Spring 会检查当前 Bean 对象的原始类中是否有名为 shutdown 或者
close 的方法如果有,此方法会被 Spring 记录下来,并在容器被销毁时自动执行。
public static final String INFER_METHOD = "(inferred)";
//当前bean对应Class对象或者全限定类名称
@Nullable
private volatile Object beanClass;
//默认的bean类型为单例
@Nullable
private String scope = SCOPE_DEFAULT;
//是否为抽象类
private boolean abstractFlag = false;
//是否延迟加载
@Nullable
private Boolean lazyInit;
//自动注入类型
private int autowireMode = AUTOWIRE_NO;
//依赖检查类型
private int dependencyCheck = DEPENDENCY_CHECK_NONE;
//依赖对应的类数据
@Nullable
private String[] dependsOn;
//当自动注入时,当前类是否为候选项
private boolean autowireCandidate = true;
//当自动注入时,当前类是否为首选项
private boolean primary = false;
//<qualifier> 标签可以有多个,如果 type 类型相同,
后面的会覆盖前面的。每个 qualifier最终会解析成
AutowireCandidateQualifier,然后添加
AbstractBeanDefinition 中 Map<String,
AutowireCandidateQualifier>中,
需要理解qualifier可以定义再注入类和Bean本身来标识注入。
private final Map<String, AutowireCandidateQualifier> qualifiers = new LinkedHashMap<>();
//函数式变成创建Bean对象方式
@Nullable
private Supplier<?> instanceSupplier;
//是否允许访问私有属性
private boolean nonPublicAccessAllowed = true;
private boolean lenientConstructorResolution = true;
//工厂类名称
@Nullable
private String factoryBeanName;
//工厂方法名称
@Nullable
private String factoryMethodName;
//构造函数参数 思考:是否可以再Bean中通过构造函数参数给我们想要的Bean来进行赋值操作
@Nullable
private ConstructorArgumentValues constructorArgumentValues;
//属性值集合 思考:是否可以通过设置属性值来替换Bean的属性值。
@Nullable
private MutablePropertyValues propertyValues;
//拦截xml中配置lookup-method和replaced-method的方法
private MethodOverrides methodOverrides = new MethodOverrides();
//初始化方法名称
@Nullable
private String initMethodName;
//销毁方法名称
@Nullable
private String destroyMethodName;
//是否执行init初始化方法
private boolean enforceInitMethod = true;
//是否执行destory销毁方法
private boolean enforceDestroyMethod = true;
//设置此bean定义是否为“合成”,用户定义的bean均为false,
后续看spring加载的自己的bean和aop来解释
private boolean synthetic = false;
//Bean的角色,为用户自定义的Bean
private int role = BeanDefinition.ROLE_APPLICATION;
//类描述
@Nullable
private String description;
//bean拥有的资源
@Nullable
private Resource resource;
/**
* Create a new AbstractBeanDefinition with default settings.
*/
protected AbstractBeanDefinition() {
this(null, null);
}
//通过构造函数和属性参数来构造一个bean定义
protected AbstractBeanDefinition(@Nullable ConstructorArgumentValues cargs, @Nullable MutablePropertyValues pvs) {
this.constructorArgumentValues = cargs;
this.propertyValues = pvs;
}
}
通过AbstractBeanDefinition的类定义可以看到,BeanDefinition接口中全是对属性的获取和设置值,值在他的子类AbstractBeanDefinition中得到了定义,而AbstractBeanDefinition又继承了元数据属性访问接口BeanMetadataAttributeAccessor,有间接继承了属性访问支持类AttributeAccessorSupport和元数据配置源BeanMetadataElement,通过Map<String,BeanMetadataAttribute>实现了AttributeAccessor接口中的属性设置和获取。
BeanMetadataAttributeAccessor Bean元数据属性访问
package org.springframework.beans;
import org.springframework.core.AttributeAccessorSupport;
import org.springframework.lang.Nullable;
@SuppressWarnings("serial")
public class BeanMetadataAttributeAccessor extends AttributeAccessorSupport implements BeanMetadataElement {
//属性源
@Nullable
private Object source;
public void setSource(@Nullable Object source) {
this.source = source;
}
@Override
@Nullable
public Object getSource() {
return this.source;
}
//设置属性值
public void addMetadataAttribute(BeanMetadataAttribute attribute) {
super.setAttribute(attribute.getName(), attribute);
}
//获取属性值
@Nullable
public BeanMetadataAttribute getMetadataAttribute(String name) {
return (BeanMetadataAttribute) super.getAttribute(name);
}
@Override
public void setAttribute(String name, @Nullable Object value) {
super.setAttribute(name, new BeanMetadataAttribute(name, value));
}
@Override
@Nullable
public Object getAttribute(String name) {
BeanMetadataAttribute attribute = (BeanMetadataAttribute) super.getAttribute(name);
return (attribute != null ? attribute.getValue() : null);
}
@Override
@Nullable
public Object removeAttribute(String name) {
BeanMetadataAttribute attribute = (BeanMetadataAttribute) super.removeAttribute(name);
return (attribute != null ? attribute.getValue() : null);
}
}
此处贴出元数据属性访问BeanMetadataAttributeAccessor代码,可以看出定义了一个BeanMetadataAttribute元数据属性,通过map对其进行增删改查操作。我们就能得到AbstractBeanDefinition是一个集合了类定义和属性获取设置的功能类。
AnnotatedBeanDefinition
public interface AnnotatedBeanDefinition extends BeanDefinition {
//获取注解元数据
AnnotationMetadata getMetadata();
//获取方法元数据
@Nullable
MethodMetadata getFactoryMethodMetadata();
}
从中文翻译注解bean定义可以看到此类为注解解析的根类,而其也只有两个方法,分别为注解元数据接口和方法元数据接口,两个接口均继承了:ClassMetadata类元数据接口和AnnotatedTypeMetadata注解类型元数据接口。
ClassMetadata类元数据接口
package org.springframework.core.type;
import org.springframework.lang.Nullable;
public interface ClassMetadata {
/**
* Return the name of the underlying class.
*/
//返回基础类名称
String getClassName();
//返回标识基础类是否是接口
boolean isInterface();
//判断是否是注解
boolean isAnnotation();
//判断是否抽象类
boolean isAbstract();
//是否是非抽象类和接口,即实际定义的类
default boolean isConcrete() {
return !(isInterface() || isAbstract());
}
//是否被final修饰
boolean isFinal();
//是否独立 即内部类 静态内部类等等
boolean isIndependent();
//是否拥有内部类
default boolean hasEnclosingClass() {
return (getEnclosingClassName() != null);
}
//获取内部类名称
@Nullable
String getEnclosingClassName();
//是否有继承类
default boolean hasSuperClass() {
return (getSuperClassName() != null);
}
//获取继承类的名称
@Nullable
String getSuperClassName();
//获取所有实现的接口名称
String[] getInterfaceNames();
// 返回所有(继承、实现)该类的 成员类(内部类、接口除外)
String[] getMemberClassNames();
}
ClassMetadata雷元数据接口定义了一个类的完整参数,如全限定类名称,是否是接口,是否是注解,是否拥有内部类,是否有继承类,获取实现的所有接口和获取所有的内部类名称等等。
AnnotatedTypeMetadata注解类型元数据
package org.springframework.core.type;
import java.lang.annotation.Annotation;
import java.util.Map;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotation.Adapt;
import org.springframework.core.annotation.MergedAnnotationCollectors;
import org.springframework.core.annotation.MergedAnnotationPredicates;
import org.springframework.core.annotation.MergedAnnotationSelectors;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap;
public interface AnnotatedTypeMetadata {
/**
* Return annotation details based on the direct annotations of the
* underlying element.
* @return merged annotations based on the direct annotations
* @since 5.2
*/
MergedAnnotations getAnnotations();
//判断是否被该注解修饰
default boolean isAnnotated(String annotationName) {
return getAnnotations().isPresent(annotationName);
}
//根绝注解名称获取所有的注解属性 思考 后续实现是否是Map嵌套
@Nullable
default Map<String, Object> getAnnotationAttributes(String annotationName) {
return getAnnotationAttributes(annotationName, false);
}
//获取多有的注解属性
@Nullable
default Map<String, Object> getAnnotationAttributes(String annotationName,
boolean classValuesAsString) {
MergedAnnotation<Annotation> annotation = getAnnotations().get(annotationName,
null, MergedAnnotationSelectors.firstDirectlyDeclared());
if (!annotation.isPresent()) {
return null;
}
return annotation.asAnnotationAttributes(Adapt.values(classValuesAsString, true));
}
//根绝注解类名称获取对应的注解类型
@Nullable
default MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
return getAllAnnotationAttributes(annotationName, false);
}
//获取对应注解类型的所有注解 需要注意 component会扫描到@service等下一层注解
@Nullable
default MultiValueMap<String, Object> getAllAnnotationAttributes(
String annotationName, boolean classValuesAsString) {
Adapt[] adaptations = Adapt.values(classValuesAsString, true);
return getAnnotations().stream(annotationName)
.filter(MergedAnnotationPredicates.unique(MergedAnnotation::getMetaTypes))
.map(MergedAnnotation::withNonMergedAttributes)
.collect(MergedAnnotationCollectors.toMultiValueMap(map ->
map.isEmpty() ? null : map, adaptations));
}
}
AnnotatedTypeMetadata注解类型元数据,我们会发现该类的功能主要是判断类是否被注解修饰以及获取所有的直接注解和间接注解(元注解)的数据,由此我们基本可以推断AnnotatedBeanDefinition的作用是分析并获取注解类的属性和功能,将会加载成Bean定义数据,相对而言就是另外一种配置,即xml配置解析BeanDefinition,具体的类型解析我们放到后面StandardAnnotationMetadata实现类中来分析。