BeanDefinition 用来描述 spring bean,是一个接口,它的具体实现类主要有 RootBeanDefinition、ChildBeanDefinition、GenericBeanDefinition 等,分别适用于不同的场景。
对于一个接口多个实现类来说,通常会有一些可以共用的部分,这些代码就可以提取到一个抽象的基础类里面,实现代码的复用。
BeanDefinition 接口的抽象基础类是 AbstractBeanDefinition,下面来看下它都实现了哪些功能
AbstractBeanDefinition
AbstractBeanDefinition 的类继承结构如下图所示:
AbstractBeanDefinition 并没有从零开始,它继承了 BeanMetadataAttributeAccessor 类,从而可以利用已有的功能对 bean 的 meta 数据进行管理。
1 2
| public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor implements BeanDefinition, Cloneable
|
AbstractBeanDefinition 的常量定义
AbstractBeanDefinition 定义了一些常量,主要是 autowire 和 依赖检查,具体见下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
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; 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;
public static final String INFER_METHOD = "(inferred)";
|
AbstractBeanDefinition 对象的属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| private volatile Object beanClass;
private String scope = SCOPE_DEFAULT;
private boolean abstractFlag = false;
private boolean lazyInit = false;
private int autowireMode = AUTOWIRE_NO;
private int dependencyCheck = DEPENDENCY_CHECK_NONE;
private String[] dependsOn;
private boolean autowireCandidate = true;
private boolean primary = false;
private final Map<String, AutowireCandidateQualifier> qualifiers = new LinkedHashMap<>();
private Supplier<?> instanceSupplier;
private boolean nonPublicAccessAllowed = true;
private boolean lenientConstructorResolution = true;
private String factoryBeanName;
private String factoryMethodName;
private ConstructorArgumentValues constructorArgumentValues;
private MutablePropertyValues propertyValues;
private MethodOverrides methodOverrides;
private String initMethodName;
private String destroyMethodName;
private boolean enforceInitMethod = true;
private boolean enforceDestroyMethod = true;
private boolean synthetic = false;
private int role = BeanDefinition.ROLE_APPLICATION;
private String description;
private Resource resource;
|
AbstractBeanDefinition 对象的方法
AbstractBeanDefinition 对象的方法与属性值对应,基本都很容易理解
getBeanClassName
获取 bean class 的名字,需要先检测下 beanClass 属性的类型,然后决定如何处理。
1 2 3 4 5 6 7 8 9 10 11 12
| public String getBeanClassName() { Object beanClassObject = this.beanClass; if (beanClassObject instanceof Class) { return ((Class<?>) beanClassObject).getName(); } else { return (String) beanClassObject; } }
|
resolveBeanClass
解析 bean class。先拿到类名,然后通过反射获取 class,当然这里是 ClassUtils 来解析,中间会使用缓存提高性能。
1 2 3 4 5 6 7 8 9 10 11
| public Class<?> resolveBeanClass(@Nullable ClassLoader classLoader) throws ClassNotFoundException { String className = getBeanClassName(); if (className == null) { return null; } Class<?> resolvedClass = ClassUtils.forName(className, classLoader); this.beanClass = resolvedClass; return resolvedClass; }
|
deep copy
深度拷贝时需要新创建一个 BeanDefinition,然后将原始的 BeanDefinition 的各个属性值全部复制过来,这在生成 MergedBeanDefinition 时会用到
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| protected AbstractBeanDefinition(BeanDefinition original) { setParentName(original.getParentName()); setBeanClassName(original.getBeanClassName()); setScope(original.getScope()); setAbstract(original.isAbstract()); setLazyInit(original.isLazyInit()); setFactoryBeanName(original.getFactoryBeanName()); setFactoryMethodName(original.getFactoryMethodName()); setRole(original.getRole()); setSource(original.getSource()); copyAttributesFrom(original);
if (original instanceof AbstractBeanDefinition) { AbstractBeanDefinition originalAbd = (AbstractBeanDefinition) original; if (originalAbd.hasBeanClass()) { setBeanClass(originalAbd.getBeanClass()); } if (originalAbd.hasConstructorArgumentValues()) { setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues())); } if (originalAbd.hasPropertyValues()) { setPropertyValues(new MutablePropertyValues(original.getPropertyValues())); } if (originalAbd.hasMethodOverrides()) { setMethodOverrides(new MethodOverrides(originalAbd.getMethodOverrides())); } setAutowireMode(originalAbd.getAutowireMode()); setDependencyCheck(originalAbd.getDependencyCheck()); setDependsOn(originalAbd.getDependsOn()); setAutowireCandidate(originalAbd.isAutowireCandidate()); setPrimary(originalAbd.isPrimary()); copyQualifiersFrom(originalAbd); setInstanceSupplier(originalAbd.getInstanceSupplier()); setNonPublicAccessAllowed(originalAbd.isNonPublicAccessAllowed()); setLenientConstructorResolution(originalAbd.isLenientConstructorResolution()); setInitMethodName(originalAbd.getInitMethodName()); setEnforceInitMethod(originalAbd.isEnforceInitMethod()); setDestroyMethodName(originalAbd.getDestroyMethodName()); setEnforceDestroyMethod(originalAbd.isEnforceDestroyMethod()); setSynthetic(originalAbd.isSynthetic()); setResource(originalAbd.getResource()); } else { setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues())); setPropertyValues(new MutablePropertyValues(original.getPropertyValues())); setResourceDescription(original.getResourceDescription()); } }
|
RootBeanDefinition
RootBeanDefinition 用于在 Spring BeanFactory 运行时表示合并的 bean definition,它可能是由多个存在继承关系的 bean definition 合并得到的,本质上 RootBeanDefinition 是 bean definition 在运行时的统一视图。
RootBeanDefinition 继承自 AbstractBeanDefinition,它也提供了很多属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| private BeanDefinitionHolder decoratedDefinition;
private AnnotatedElement qualifiedElement;
boolean allowCaching = true;
boolean isFactoryMethodUnique = false;
volatile ResolvableType targetType;
volatile Class<?> resolvedTargetType;
volatile ResolvableType factoryMethodReturnType;
volatile Method factoryMethodToIntrospect;
final Object constructorArgumentLock = new Object();
Executable resolvedConstructorOrFactoryMethod;
boolean constructorArgumentsResolved = false;
Object[] resolvedConstructorArguments;
Object[] preparedConstructorArguments;
final Object postProcessingLock = new Object();
boolean postProcessed = false;
volatile Boolean beforeInstantiationResolved;
private Set<Member> externallyManagedConfigMembers;
private Set<String> externallyManagedInitMethods;
private Set<String> externallyManagedDestroyMethods;
|
ChildBeanDefinition
ChildBeanDefinition 用于表示继承关系的 bean definition,它从父 bean definition 继承配置信息,例如构造函数参数值、属性值和方法重写(method overrides),并可选择添加新值。如果指定了 init 方法、destroy 方法或静态工厂方法,它们将覆盖相应的父 bean definition 设置。其余的设置将始终从子 definition 中获取:depends on、autowire 模式、dependency check、singleton、lazy init
GenericBeanDefinition
GenericBeanDefinition 是用于标准 bean definition 的一站式服务,与其他类型的 bean definition 一样,它允许指定一个类加上可选的构造函数参数值和属性值。此外,bean definition 继承关系可以通过 parentName 属性灵活地配置。
通常情况下,可以使用 GenericBeanDefinition 来注册用户自定义的 bean definition(后处理器可以对其进行操作,甚至可以重新配置 parentName)