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:
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 |
String[] getDependsOn(); | void setDependsOn(@Nullable String... dependsOn); | Set the dependent bean object. The dependent bean object will always be larger than the current one. The bean object is first created|
boolean isAutowireCandidate(); | void setAutowireCandidate(boolean autowireCandidate); | Set whether automatic injection can be performed. Only valid for @Autowired annotation, the configuration file can be injected through property display|
boolean isPrimary(); | void setPrimary(boolean primary); | 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 injection|
String getFactoryBeanName(); | void setFactoryBeanName(@Nullable String factoryBeanName); | Set the factory name of the created bean|
String getFactoryMethodName(); | void setFactoryMethodName(@Nullable String factoryMethodName); | Set the specific method of creating the bean in the factory where the bean is created|
String getInitMethodName(); | void setInitMethodName(@Nullable String initMethodName); | Set the default initialization method when creating a bean|
String getDestroyMethodName(); | void setDestroyMethodName(@Nullable String destroyMethodName); | Set the method name called when destroying the bean. Note that you need to call the close() method of context to call |
int getRole(); | void setRole(int role); | Set the bean classification|
String getDescription(); | void setDescription(@Nullable String description); | For beans Description of the object
Actual useAssume there are the following two beans, the Java code is as followsMyTestBeanpublic 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(); }
AutowireCandidateBeanpackage 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; } }
Let’s see how to configure it in the configuration file applicationContext.xmlpackage com.yuanweiquan.learn.bean; public class AutowireCandidateBean { public void initBean() { System.out.println("init AutowireCandidateBean"); } public void destroyBean() { System.out.println("destroy AutowireCandidateBean"); } }
<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 AutowireCandidateBeanSo 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!