• GenericBeanDefinition及其子类


    GenericBeanDefinition及其子类解析

    GenericBeanDefinition介绍

    GenericBeanDefinition是继承于抽象类AbstractBeanDefinition,相比AbstractBeanDefinition多了一个成员属性parentName。

    GenericBeanDefinition通用的bean实现,自2.5以后新加入的bean文件配置属性定义类。

    GenericBeanDefinition子类介绍

    GenericBeanDefinition有三个派生子类:

    • ScannedGenericBeanDefinition
    • ConfigurationPropertiesValueObjectBeanDefinition
    • AnnotatedGenericBeanDefinition

    请添加图片描述

    这三个派生子类的作用如下:

    ScannedGenericBeanDefinition:存储@Component、@Service、@Controller等注解注释的类

    ConfigurationPropertiesValueObjectBeanDefinition:存储@ConfigurationProperties等注解注释的类

    AnnotatedGenericBeanDefinition:存储@Configuration注解注释的类

    GenericBeanDefinition源码

    GenericBeanDefinition几乎所有实现都是依靠AbstractBeanDefinition,也就多了个成员属性parentName

    @SuppressWarnings("serial")
    public class GenericBeanDefinition extends AbstractBeanDefinition {
    
    	@Nullable
    	private String parentName;
    
    
    	/**
    	 * Create a new GenericBeanDefinition, to be configured through its bean
    	 * properties and configuration methods.
    	 * @see #setBeanClass
    	 * @see #setScope
    	 * @see #setConstructorArgumentValues
    	 * @see #setPropertyValues
    	 */
    	public GenericBeanDefinition() {
    		super();
    	}
    
    	/**
    	 * Create a new GenericBeanDefinition as deep copy of the given
    	 * bean definition.
    	 * @param original the original bean definition to copy from
    	 */
    	public GenericBeanDefinition(BeanDefinition original) {
    		super(original);
    	}
    
    
    	@Override
    	public void setParentName(@Nullable String parentName) {
    		this.parentName = parentName;
    	}
    
    	@Override
    	@Nullable
    	public String getParentName() {
    		return this.parentName;
    	}
    
    
    	@Override
    	public AbstractBeanDefinition cloneBeanDefinition() {
    		return new GenericBeanDefinition(this);
    	}
    
    	@Override
    	public boolean equals(@Nullable Object other) {
    		if (this == other) {
    			return true;
    		}
    		if (!(other instanceof GenericBeanDefinition)) {
    			return false;
    		}
    		GenericBeanDefinition that = (GenericBeanDefinition) other;
    		return (ObjectUtils.nullSafeEquals(this.parentName, that.parentName) && super.equals(other));
    	}
    
    	@Override
    	public String toString() {
    		if (this.parentName != null) {
    			return "Generic bean with parent '" + this.parentName + "': " + super.toString();
    		}
    		return "Generic bean: " + super.toString();
    	}
    
    }
    
    • 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
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    获取GenericBeanDefinition

    我们可以用XmlBeanFactory作为IOC容器,通过类路径中的application.xml文件去获取bean加载到IOC容器中,通过getBeanDefinition()方法去获取GenericBeanDefinition,并打印一下它有什么信息。

    	@Test
        public void testGenericBeanDefinition(){
            //创建IOC容器
            XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("application.xml"));
            //获取bean
            ParentBean parentBean = (ParentBean)xmlBeanFactory.getBean("parentBean");
            ChildBean childBean = (ChildBean)xmlBeanFactory.getBean("childBean");
            System.out.println(parentBean);
            System.out.println(childBean);
    		//获取BeanDefinition
            BeanDefinition parentbeanDefinition = xmlBeanFactory.getBeanDefinition("parentBean");
            BeanDefinition childbeanDefinition = xmlBeanFactory.getBeanDefinition("childBean");
            System.out.println(parentbeanDefinition);
            System.out.println(childbeanDefinition);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    结果

    17:28:00.114 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 3 bean definitions from class path resource [application.xml]
    17:28:00.118 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory - Creating shared instance of singleton bean ‘parentBean’
    17:28:00.132 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory - Creating shared instance of singleton bean ‘childBean’
    ParentBean()
    ChildBean()
    Generic bean: class [com.yaliyao.pojo.testBeanPojo.ParentBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [application.xml]
    ctoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [application.xml]
    Generic bean with parent ‘parentBean’: class [com.yaliyao.pojo.testBeanPojo.ChildBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [application.xml]

  • 相关阅读:
    Cesium添加自己的专属Logo(2023.10.22)
    Python:寇松数字(Curzon Number)
    Spring快速上手
    Vivado 2017.04版本安装教程
    MeterSphere v2.10.X-lts 双节点HA部署方案
    【Spring boot 集成 JSP】
    面试面经|Java面试基础题17道
    重绘、重排区别如何避免
    连接数据库时遇到的bug1号
    如何提升新闻公关发稿的效果?
  • 原文地址:https://blog.csdn.net/qq_43203949/article/details/125470748