• Spring源码分析(八)AbstractBeanDefinition属性


    摘要:本文结合《Spring源码深度解析》来分析Spring 5.0.6版本的源代码。若有描述错误之处,欢迎指正。

    上一篇中已经完成了XML文档到GenericBeanDefiniton的转化,也就是说,XML中所有的配置都在GenericBeanDefinition的实例类中找到了对应的位置。

    GenericBeanDefinition只是子类实现,大部分的属性都保存在了AbstractBeanDefinition中,下面我们看下这个类:

    public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor
            implements BeanDefinition, Cloneable {
            
        // 此处省略静态变量以及final变量
        
        @Nullable
        private volatile Object beanClass;
        /**
         * bean的作用范围,对应bean属性scope
         */
        @Nullable
        private String scope = SCOPE_DEFAULT;
        /**
         * 是否是抽象,对应bean属性abstract
         */
        private boolean abstractFlag = false;
        /**
         * 是否延迟加载,对应bean属性lazy-init
         */
        private boolean lazyInit = false;
        /**
         * 自动注入模式,对应bean属性autowire
         */
        private int autowireMode = AUTOWIRE_NO;
        /**
         * 依赖检查,Spring 3.0后弃用这个属性
         */
        private int dependencyCheck = DEPENDENCY_CHECK_NONE;
        /**
         * 用来表示一个bean的实例化依靠另一个bean先实例化,对应bean属性depend-on
         */
        @Nullable
        private String[] dependsOn;
        /**
         * autowire-candidate属性设置为false,这样容器在查找自动装配对象时,
         * 将不考虑该bean,即它不会被考虑作为其他bean自动装配的候选者,
         * 但是该bean本身还是可以使用自动装配来注入其他bean的
         */
        private boolean autowireCandidate = true;
        /**
         * 自动装配时出现多个bean候选者时,将作为首选者,对应bean属性primary
         */
        private boolean primary = false;
        /**
         * 用于记录Qualifier,对应子元素qualifier
         */
        private final Map<String, AutowireCandidateQualifier> qualifiers = new LinkedHashMap<>(0);
    
        @Nullable
        private Supplier<?> instanceSupplier;
        /**
         * 允许访问非公开的构造器和方法,程序设置
         */
        private boolean nonPublicAccessAllowed = true;
        /**
         * 是否以一种宽松的模式解析构造函数,默认为true,
         * 如果为false,则在以下情况
         * interface ITest{}
         * class ITestImpl implements ITest{};
         * class Main {
         *     Main(ITest i) {}
         *     Main(ITestImpl i) {}
         * }
         * 抛出异常,因为Spring无法准确定位哪个构造函数程序设置
         */
        private boolean lenientConstructorResolution = true;
        /**
         * 对应bean属性factory-bean,用法:
         * <bean id = "instanceFactoryBean" class = "example.chapter3.InstanceFactoryBean" />
         * <bean id = "currentTime" factory-bean = "instanceFactoryBean" factory-method = "createTime" />
         */
        @Nullable
        private String factoryBeanName;
        /**
         * 对应bean属性factory-method
         */
        @Nullable
        private String factoryMethodName;
        /**
         * 记录构造函数注入属性,对应bean属性constructor-arg
         */
        @Nullable
        private ConstructorArgumentValues constructorArgumentValues;
        /**
         * 普通属性集合
         */
        @Nullable
        private MutablePropertyValues propertyValues;
        /**
         * 方法重写的持有者,记录lookup-method、replaced-method元素
         */
        @Nullable
        private MethodOverrides methodOverrides;
        /**
         * 初始化方法,对应bean属性init-method
         */
        @Nullable
        private String initMethodName;
        /**
         * 销毁方法,对应bean属性destroy-method
         */
        @Nullable
        private String destroyMethodName;
        /**
         * 是否执行init-method,程序设置
         */
        private boolean enforceInitMethod = true;
        /**
         * 是否执行destroy-method,程序设置
         */
        private boolean enforceDestroyMethod = true;
        /**
         * 是否是用户定义的而不是应用程序本身定义的,创建AOP时候为true,程序设置
         */
        private boolean synthetic = false;
        /**
         * 定义这个bean的应用,APPLICATION:用户,INFRASTRUCTURE:完全内部使用,与用户无关,
         * SUPPORT:某些复杂配置的一部分
         * 程序设置
         */
        private int role = BeanDefinition.ROLE_APPLICATION;
        /**
         * bean的描述信息
         */
        @Nullable
        private String description;
        /**
         * 这个bean定义的资源
         */
        @Nullable
        private Resource resource;
    }
  • 相关阅读:
    如何让一个浮动垂直居中:两种方式!带来效果~~~~~~
    rgba()和opacity之间的区别(面试题)
    常用浏览器内核!IE,Chrome ,Firefox,Safari,Opera 等内核
    有关Option.inSamplSize 和 Compress 图片压缩
    Android App 启动 Activity 创建解析
     (转)windows一台电脑添加多个git账号
    Handler向子线程发送数据
    Android Touch事件分发
    int 转十六进制
    JVM client模式和Server模式
  • 原文地址:https://www.cnblogs.com/warehouse/p/9380375.html
Copyright © 2020-2023  润新知