這篇文章帶給大家的內容是關於Spring源碼之BeanDefinition類的詳細介紹,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
Spring版本為5.1.5
用過spring的人都知道,我們將物件注入到spring容器中,交給spring來幫我們管理。這種對像我們稱為bean對象。但是這些bean物件在spring容器中,到底是以什麼形式存在,具有哪些屬性、行為呢?今天我們進入到spring源碼來一探究竟。
bean的創建工廠BeanFactory有個預設實作類別DefaultListableBeanFactory,內部有個存放所有註入bean物件資訊的Map
/** Map of bean definition objects, keyed by bean name. */ private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
Map的value物件BeanDefinition就是spring中對bean的定義與描述,具體概述如下:
屬性 | 行為 | 解釋 |
---|---|---|
parentName | String getParentName(); void setParentName(@Nullable String parentName); |
bean定義物件的父類別定義物件名稱 |
#beanClassName | String getBeanClassName(); void setBeanClassName(@Nullable String beanClassName); |
bean物件的實際類別類別 |
scope | String getScope(); void setScope(@Nullable String scope); |
bean物件是否為單例 |
lazyInit | boolean isLazyInit(); void setLazyInit(boolean lazyInit); |
是否懶載入 |
dependsOn | String[] getDependsOn(); void setDependsOn(@Nullable String... dependsOn); |
設定依賴的bean對象,被依賴的bean物件總是會比目前bean物件先建立 |
autowireCandidate | boolean isAutowireCandidate(); void setAutowireCandidate(boolean autowireCandidate); |
設定是否可以自動注入。只對@Autowired註解有效,設定檔中可以透過property顯示注入 |
#primary | boolean isPrimary(); void setPrimary(boolean primary); |
配置bean為主要候選bean。當同一個介面的多個實作類別或一個類別多次注入spring容器時,透過該屬性來配置某個bean為主候選bean,透過型別來注入時,預設為使用主候選bean注入 |
factoryBeanName | String getFactoryBeanName(); void setFactoryBeanName(@Nullable String factoryBeanName); |
設定建立bean的工廠名稱 |
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(); }
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; } }
package 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>
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();
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
以上是Spring源碼之BeanDefinition類別的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!