前言:看完了Spring BeanDefinition与AbstractBeanDefinition后,现在开始学习RootBeanDefinition
简介 类图
RootBeanDefinition继承AbstractBeanDefinition抽象类。
一个RootBeanDefinition定义表明它是一个可合并的bean definition:即在spring beanFactory运行期间,可以返回一个特定的bean。RootBeanDefinition可以作为一个重要的通用的bean definition 视图。
RootBeanDefinition用来在配置阶段进行注册bean definition。然后,从spring 2.5后,编写注册bean definition有了更好的的方法:GenericBeanDefinition。GenericBeanDefinition支持动态定义父类依 赖,而非硬编码作为root bean definition。其中RootBeanDefinition是最常用的实现类,它对应一般性的元素标签
在 配置文件中可以定义父和子,父用RootBeanDefinition表示, 而子用ChildBeanDefiniton表示,而没有父的就使用 RootBeanDefinition表示。
方法
字段
字段 下面是我对该类字段的理解
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 53 54 55 boolean allowCaching = true ;volatile Boolean beforeInstantiationResolved;final Object constructorArgumentLock = new Object();boolean constructorArgumentsResolved = false ;private BeanDefinitionHolder decoratedDefinition;private Set<Member> externallyManagedConfigMembers;private Set<String> externallyManagedDestroyMethods;private Set<String> externallyManagedInitMethods;volatile ResolvableType factoryMethodReturnType;boolean isFactoryMethodUnique = false ;boolean postProcessed = false ;final Object postProcessingLock = new Object();Object[] preparedConstructorArguments; private AnnotatedElement qualifiedElement;Object[] resolvedConstructorArguments; Object resolvedConstructorOrFactoryMethod; volatile Class<?> resolvedTargetType;volatile ResolvableType targetType;
构造方法 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 53 public RootBeanDefinition () { super (); } RootBeanDefinition(BeanDefinition original) { super (original); } public RootBeanDefinition (Class<?> beanClass) { super (); setBeanClass(beanClass); } public RootBeanDefinition (Class<?> beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) { super (cargs, pvs); setBeanClass(beanClass); } public RootBeanDefinition (Class<?> beanClass, int autowireMode, boolean dependencyCheck) { super (); setBeanClass(beanClass); setAutowireMode(autowireMode); if (dependencyCheck && getResolvedAutowireMode() != AUTOWIRE_CONSTRUCTOR) { setDependencyCheck(DEPENDENCY_CHECK_OBJECTS); } } public RootBeanDefinition (RootBeanDefinition original) { super (original); this .decoratedDefinition = original.decoratedDefinition; this .qualifiedElement = original.qualifiedElement; this .allowCaching = original.allowCaching; this .isFactoryMethodUnique = original.isFactoryMethodUnique; this .targetType = original.targetType; } public RootBeanDefinition (String beanClassName) { setBeanClassName(beanClassName); } public RootBeanDefinition (String beanClassName, ConstructorArgumentValues cargs, MutablePropertyValues pvs) { super (cargs, pvs); setBeanClassName(beanClassName); }
cloneBeanDefinition 深拷贝一个RootBeanDefinition
1 2 3 4 @Override public RootBeanDefinition cloneBeanDefinition () { return new RootBeanDefinition(this ); }
equals方法 重写equals方法
1 2 3 4 @Override public boolean equals (Object other) { return (this == other || (other instanceof RootBeanDefinition && super .equals(other))); }
get方法 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 public BeanDefinitionHolder getDecoratedDefinition () { return this .decoratedDefinition; } public String getParentName () { return null ; } public AnnotatedElement getQualifiedElement () { return this .qualifiedElement; } public Method getResolvedFactoryMethod () { synchronized (this .constructorArgumentLock) { Object candidate = this .resolvedConstructorOrFactoryMethod; return (candidate instanceof Method ? (Method) candidate : null ); } } public Class<?> getTargetType() { if (this .resolvedTargetType != null ) { return this .resolvedTargetType; } return (this .targetType != null ? this .targetType.resolve() : null ); }
is方法 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 public boolean isExternallyManagedConfigMember (Member configMember) { synchronized (this .postProcessingLock) { return (this .externallyManagedConfigMembers != null && this .externallyManagedConfigMembers.contains(configMember)); } } public boolean isExternallyManagedDestroyMethod (String destroyMethod) { synchronized (this .postProcessingLock) { return (this .externallyManagedDestroyMethods != null && this .externallyManagedDestroyMethods.contains(destroyMethod)); } } public boolean isExternallyManagedInitMethod (String initMethod) { synchronized (this .postProcessingLock) { return (this .externallyManagedInitMethods != null && this .externallyManagedInitMethods.contains(initMethod)); } } public boolean isFactoryMethod (Method candidate) { return (candidate != null && candidate.getName().equals(getFactoryMethodName())); }
registerExternallyManaged方法 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 public void registerExternallyManagedConfigMember (Member configMember) { synchronized (this .postProcessingLock) { if (this .externallyManagedConfigMembers == null ) { this .externallyManagedConfigMembers = new HashSet<Member>(1 ); } this .externallyManagedConfigMembers.add(configMember); } } public void registerExternallyManagedDestroyMethod (String destroyMethod) { synchronized (this .postProcessingLock) { if (this .externallyManagedDestroyMethods == null ) { this .externallyManagedDestroyMethods = new HashSet<String>(1 ); } this .externallyManagedDestroyMethods.add(destroyMethod); } } public void registerExternallyManagedInitMethod (String initMethod) { synchronized (this .postProcessingLock) { if (this .externallyManagedInitMethods == null ) { this .externallyManagedInitMethods = new HashSet<String>(1 ); } this .externallyManagedDestroyMethods.add(initMethod); } }
set方法 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 public void setDecoratedDefinition (BeanDefinitionHolder decoratedDefinition) { this .decoratedDefinition = decoratedDefinition; } public void setParentName (String parentName) { if (parentName != null ) { throw new IllegalArgumentException("Root bean cannot be changed into a child bean with parent reference" ); } } public void setQualifiedElement (AnnotatedElement qualifiedElement) { this .qualifiedElement = qualifiedElement; } public void setTargetType (Class<?> targetType) { this .targetType = (targetType != null ? ResolvableType.forClass(targetType) : null ); } public void setTargetType (ResolvableType targetType) { this .targetType = targetType; } public void setUniqueFactoryMethodName (String name) { Assert.hasText(name, "Factory method name must not be empty" ); setFactoryMethodName(name); this .isFactoryMethodUnique = true ; }
总结一下,RootBeanDefiniiton保存了以下信息:
定义了id、别名与Bean的对应关系(BeanDefinitionHolder)
Bean的注解(AnnotatedElement)
具体的工厂方法(Class类型),包括工厂方法的返回类型,工厂方法的Method对象
构造函数、构造函数形参类型
Bean的class对象
RootBeanDefinition对AbstractBeanDefinition抽象类进一步补全,在其基础上定义了更多属性,初始化Bean需要的信息基本完善
参考
【小家Spring】Spring IoC容器中核心定义之——BeanDefinition深入分析(RootBeanDefinition、ChildBeanDefinition…)