2、spring之定义Bean的抽象BeanDefinition

xiaoxiao2021-02-28  13

一、BeanDefinition

1.1 什么是BeanDefinition

BeanDefinition作为定义springBean文件中bean的接口,可以说是bean的抽象数据结构,它包括属性参数,构造器参数,以及其他具体的参数。

1.2 BeanDefinition类结构图

BeanDefinition继承了AttributeAccessor和BeanMetaDataElement接口,拥有了对于元数据访问的功能。

1.3 BeanDefinition接口

public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { String getParentName(); void setParentName(String parentName); /** * 返回当前bean的名字(并不是准确的名字,有些ChildBeanDefinition是继承自父bean的名字), * 所以不能依靠该名字来确定Class的类型 */ String getBeanClassName(); void setBeanClassName(String beanClassName); // 工厂Bean String getFactoryBeanName(); void setFactoryBeanName(String factoryBeanName); // 工厂方法名字 String getFactoryMethodName(); void setFactoryMethodName(String factoryMethodName); // 作用域 String getScope(); void setScope(String scope); boolean isSingleton(); boolean isPrototype(); // 懒加载 boolean isLazyInit(); void setLazyInit(boolean lazyInit); // 依赖的Bean String[] getDependsOn(); void setDependsOn(String... dependsOn); /** * Return whether this bean is a candidate for getting autowired into some other bean. */ boolean isAutowireCandidate(); void setAutowireCandidate(boolean autowireCandidate); boolean isPrimary(); void setPrimary(boolean primary); // 获取构造函数的值 ConstructorArgumentValues getConstructorArgumentValues(); // 获取参数的值 MutablePropertyValues getPropertyValues(); // 是否是抽象Bean boolean isAbstract(); /** * Get the role hint for this {@code BeanDefinition}. The role hint * provides the frameworks as well as tools with an indication of * the role and importance of a particular {@code BeanDefinition}. * @see #ROLE_APPLICATION * @see #ROLE_SUPPORT * @see #ROLE_INFRASTRUCTURE */ int getRole(); // 获取描述 String getDescription(); String getResourceDescription(); /** * 有些BeanDefinition会使用BeanDefinitionResource进行包装,将 * BeanDefinition描述为一个资源 */ BeanDefinition getOriginatingBeanDefinition(); }

作用域:

String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON; 单例 String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE; 非单例

角色 ROLE_APPLICATION 用户 ROLE_SUPPORT 某些复杂的配置 ROLE_INFRASTRUCTURE 完全内部使用

二、AbstractBeanDefinition

AbstractBeanDefinition是对BeanDefinition的抽象实现类,我们可以看一下它的值域和方法

public static final String INFER_METHOD = "(inferred)"; // Bean所抽象的class 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<String, AutowireCandidateQualifier>(0); private boolean nonPublicAccessAllowed = true; private boolean lenientConstructorResolution = true; // 构造函数参数抽象 private ConstructorArgumentValues constructorArgumentValues; // 值域的抽象 private MutablePropertyValues propertyValues; // 方法重写的抽象 private MethodOverrides methodOverrides = new MethodOverrides(); private String factoryBeanName; private String factoryMethodName; 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;

可以看到值域基本上都是用来存储BeanDefinition接口所需要的内容,只是比BeanDefinition多出了initMethodName 和 destroyMethodName。

2.1 ConstructorArgumentValues

用来存储定义bean的时候使用的构造函数传入的参数信息

public class ConstructorArgumentValues { // 用来存储带有index的值 private final Map<Integer, ValueHolder> indexedArgumentValues = new LinkedHashMap<Integer, ValueHolder>(0); // 用来存储不带有index的值 private final List<ValueHolder> genericArgumentValues = new LinkedList<ValueHolder>(); } // 用来存储值的信息 public static class ValueHolder implements BeanMetadataElement { // 对应的值 private Object value; // 对应的类型 private String type; // 对应的名称 private String name; private Object source; // 是否已经转换 private boolean converted = false; // 是转换后的值 private Object convertedValue; }

2.2 MutablePropertyValues

2.3 MethodOverrides

三、BeanDefinition的具体实现

(1)RootBeanDefinition、GenericBeanDefinition、ChildBeanDefinition RootBeanDefinition是最常用的实现类,它对应一般性的元素标签,GenericBeanDefinition是自2.5以后新加入的bean文件配置属性定义类,是一站式服务类。在配置文件中可以定义父和子,父用RootBeanDefinition表示,而子用ChildBeanDefiniton表示,而没有父的就使用RootBeanDefinition表示 (2)AnnotatedGenericBeanDefinition 以@Configuration注解标记的会解析为AnnotatedGenericBeanDefinition (3)ConfigurationClassBeanDefinition 以@Bean注解标记的会解析为ConfigurationClassBeanDefinition

(4)ScannedGenericBeanDefinition 以@Component注解标记的会解析为ScannedGenericBeanDefinition

接下来可以了解一下spring如何将xml或者注解类转换为BeanDefinition https://blog.csdn.net/liu20111590/article/details/89892342 https://blog.csdn.net/liu20111590/article/details/89857660

转载请注明原文地址: https://www.6miu.com/read-1450118.html

最新回复(0)