Home  >  Article  >  Java  >  Detailed introduction to the BeanDefinition class of Spring source code

Detailed introduction to the BeanDefinition class of Spring source code

不言
不言forward
2019-03-22 16:38:563718browse

This article brings you a detailed introduction to the BeanDefinition class in the Spring source code. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Spring version is 5.1.5

Everyone who has used spring knows that we inject objects into the spring container and let spring manage it for us. This kind of object is called a bean object. But in what form do these bean objects exist in the spring container, and what attributes and behaviors do they have? Today we enter the spring source code to find out.

The bean creation factory BeanFactory has a default implementation class DefaultListableBeanFactory, and there is a Map inside that stores all the injected bean object information

/** Map of bean definition objects, keyed by bean name. */
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);

The value object BeanDefinition of the Map is the definition and description of the bean in spring , the specific overview is as follows:

##dependsOn String[] getDependsOn();Set the dependent bean object. The dependent bean object will always be larger than the current one. The bean object is first createdautowireCandidateboolean isAutowireCandidate();Set whether automatic injection can be performed. Only valid for @Autowired annotation, the configuration file can be injected through property displayprimaryboolean isPrimary();Configure the bean as the main candidate bean. When multiple implementation classes of the same interface or a class are injected into the spring container multiple times, use this attribute to configure a bean as the main candidate bean. When injecting by type, the default is to use the main candidate bean for injectionfactoryBeanNameString getFactoryBeanName();Set the factory name of the created beanfactoryMethodNameString getFactoryMethodName();Set the specific method of creating the bean in the factory where the bean is createdinitMethodNameString getInitMethodName();Set the default initialization method when creating a beandestroyMethodNameString getDestroyMethodName();Set the method name called when destroying the bean. Note that you need to call the close() method of context to call roleint getRole();Set the bean classificationdescriptionString getDescription();For beans Description of the object
Attributes Behavior Explanation
parentName String getParentName();
void setParentName(@Nullable String parentName);
The parent class definition object name of the bean definition object
beanClassName String getBeanClassName();
void setBeanClassName(@Nullable String beanClassName);
The actual class class of the bean object
scope String getScope();
void setScope(@Nullable String scope);
Whether the bean object is a singleton
lazyInit boolean isLazyInit();
void setLazyInit(boolean lazyInit);
Whether lazy loading
void setDependsOn(@Nullable String... dependsOn);
void setAutowireCandidate(boolean autowireCandidate);
void setPrimary(boolean primary);
void setFactoryBeanName(@Nullable String factoryBeanName);
void setFactoryMethodName(@Nullable String factoryMethodName);
void setInitMethodName(@Nullable String initMethodName);
void setDestroyMethodName(@Nullable String destroyMethodName);
void setRole(int role);
void setDescription(@Nullable String description);
#Note: BeanDefinition is an interface, which only defines some basic behaviors of the bean object internally. The properties in the above table do not actually exist in the BeanDefinition, they are only set and obtained through the set and get methods. The following extracts part of the BeanDefinition source code for everyone's perception
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
    /**
     * Override the target scope of this bean, specifying a new scope name.
     * @see #SCOPE_SINGLETON
     * @see #SCOPE_PROTOTYPE
     */
    void setScope(@Nullable String scope);

    /**
     * Return the name of the current target scope for this bean,
     * or {@code null} if not known yet.
     */
    @Nullable
    String getScope();
}
Actual use

Assume there are the following two beans, the Java code is as follows

MyTestBean
package com.yuanweiquan.learn.bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;

public class MyTestBean {
    @Autowired
    private AutowireCandidateBean autowireCandidateBean;

    public void init() {
        System.out.println("inti MyTestBean");
    }

    public AutowireCandidateBean getAutowireCandidateBean() {
        return autowireCandidateBean;
    }
    public void setAutowireCandidateBean(AutowireCandidateBean bean) {
        this.autowireCandidateBean = bean;
    }
}
AutowireCandidateBean
package com.yuanweiquan.learn.bean;

public class AutowireCandidateBean {
    public void initBean() {
        System.out.println("init AutowireCandidateBean");
    }

    public void destroyBean() {
        System.out.println("destroy AutowireCandidateBean");
    }
}
Let’s see how to configure it in the configuration file applicationContext.xml

   <bean id="myTestBean" class="com.yuanweiquan.learn.bean.MyTestBean" 
         depends-on="autowireCandidateBean" 
         init-method="init"/>
         
   <bean id="autowireCandidateBean" 
         class="com.yuanweiquan.learn.bean.AutowireCandidateBean"
         init-method="initBean"
         autowire-candidate="true"
         destroy-method="destroyBean"
         scope="singleton"
         parent="myTestBean"
         lazy-init="default"
         primary="true">
      <description>autowireCandidateBean description</description>
   </bean>
The next step is the test code

FileSystemXmlApplicationContext factory = 
        new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
BeanDefinition myTestBeanDefinition = 
        factory.getBeanFactory().getBeanDefinition("autowireCandidateBean");
//输出
System.out.println("bean description:" + myTestBeanDefinition.getDescription());
System.out.println("bean class name:" + myTestBeanDefinition.getBeanClassName());
System.out.println("parent name:" + myTestBeanDefinition.getParentName());
System.out.println("scope:" + myTestBeanDefinition.getScope());
System.out.println("is lazyinit:" + myTestBeanDefinition.isLazyInit());
System.out.println("depends On:" + myTestBeanDefinition.getDependsOn());
System.out.println("is autowireCandidate:" + myTestBeanDefinition.isAutowireCandidate());
System.out.println("is primary:" + myTestBeanDefinition.isPrimary());
System.out.println("factory bean name:"+myTestBeanDefinition.getFactoryBeanName());
System.out.println("factory bean method name:" + myTestBeanDefinition.getFactoryMethodName());
System.out.println("init method name:" + myTestBeanDefinition.getInitMethodName());
System.out.println("destory method name:" + myTestBeanDefinition.getDestroyMethodName());
System.out.println("role:" + myTestBeanDefinition.getRole());
//关闭context,否则不会调用bean的销毁方法
factory.close();
The console output is as follows

init AutowireCandidateBean
inti MyTestBean
bean description:autowireCandidateBean description
bean class name:com.yuanweiquan.learn.bean.AutowireCandidateBean
parent name:myTestBean
scope:singleton
is lazyinit:false
depends On:null
is autowireCandidate:true
is primary:true
factory bean name:null
factory bean method name:null
init method name:initBean
destory method name:destroyBean
role:0
destroy AutowireCandidateBean
So far, through the above information, we can clearly see the values ​​corresponding to each attribute. The purpose of the above test code is to allow us all to see what form the bean exists in the spring container, what specific attributes it has, the value of the attribute and the default value. It can be considered as a preliminary reveal of the beans in the spring container. In fact, it is not as mysterious as we imagined.

The above is the detailed content of Detailed introduction to the BeanDefinition class of Spring source code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete