天天看點

springboot源碼解析-管中窺豹系列之BeanDefinition(八)一、前言二、BeanDefinition三、源碼分析

一、前言

  • Springboot源碼解析是一件大工程,逐行逐句的去研究代碼,會很枯燥,也不容易堅持下去。
  • 我們不追求大而全,而是試着每次去研究一個小知識點,最終聚沙成塔,這就是我們的springboot源碼管中窺豹系列。
springboot源碼解析-管中窺豹系列之BeanDefinition(八)一、前言二、BeanDefinition三、源碼分析

二、BeanDefinition

  • spring幫我們管理bean,就是通過BeanDefinition實作的,要深研源碼,就繞不開BeanDefinition
  • 我們通過源碼來看看BeanDefinition到底做了什麼

三、源碼分析

我們先看看這個類:BeanDefinition

public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {

	String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;

	String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;

	int ROLE_APPLICATION = 0;

	int ROLE_SUPPORT = 1;

	int ROLE_INFRASTRUCTURE = 2;

	void setParentName(@Nullable String parentName);

	@Nullable
	String getParentName();

	void setBeanClassName(@Nullable String beanClassName);

	@Nullable
	String getBeanClassName();

	void setScope(@Nullable String scope);

	@Nullable
	String getScope();

	void setLazyInit(boolean lazyInit);

	boolean isLazyInit();

	void setDependsOn(@Nullable String... dependsOn);

	@Nullable
	String[] getDependsOn();

	void setAutowireCandidate(boolean autowireCandidate);

	boolean isAutowireCandidate();

	void setPrimary(boolean primary);

	boolean isPrimary();

	void setFactoryBeanName(@Nullable String factoryBeanName);

	@Nullable
	String getFactoryBeanName();

	void setFactoryMethodName(@Nullable String factoryMethodName);

	@Nullable
	String getFactoryMethodName();

	ConstructorArgumentValues getConstructorArgumentValues();


	default boolean hasConstructorArgumentValues() {
		return !getConstructorArgumentValues().isEmpty();
	}

	MutablePropertyValues getPropertyValues();


	default boolean hasPropertyValues() {
		return !getPropertyValues().isEmpty();
	}


	boolean isSingleton();


	boolean isPrototype();


	boolean isAbstract();


	int getRole();


	@Nullable
	String getDescription();

	@Nullable
	String getResourceDescription();

	@Nullable
	BeanDefinition getOriginatingBeanDefinition();

}
           

定義了一個bean的各種屬性:

  • 父類:setParentName
  • class名稱:setBeanClassName
  • 作用域:setScope
  • 懶加載:setLazyInit
  • 依賴: setDependsOn
  • 被依賴: setAutowireCandidate
  • 自動加載:setPrimary
  • 工廠bean: setFactoryBeanName
  • 工廠方法:setFactoryMethodName
  • 單例:isSingleton
  • 多例:isPrototype
  • 靜态:isAbstract

我們用idea打開圖屬性看看:

springboot源碼解析-管中窺豹系列之BeanDefinition(八)一、前言二、BeanDefinition三、源碼分析
  • 繼承了AttributeAccessor,擁有擷取屬性的能力
  • 繼承了BeanMetadataElement,擁有了擷取源的能力
  • 定義了各種接口,用來描述一個bean

我們在看看它的實作類:

springboot源碼解析-管中窺豹系列之BeanDefinition(八)一、前言二、BeanDefinition三、源碼分析
  • spring的常用方式:定義接口,寫抽象類實作接口(核心功能寫在這裡),各種實作
  • 接口:BeanDefinition
  • 抽象類: AbstractBeanDefinition
  • 實作類:RootBeanDefinition,ChildBeanDefinition,GenericBeanDefinition,AnnotatedGenericBeanDefinition,ScannedGenericBeanDefinition
  • Root和Child是早期使用的,從spring2.5以後統一用Geniric

此外還有一個封裝類:BeanDefinitionHolder

public class BeanDefinitionHolder implements BeanMetadataElement {

	private final BeanDefinition beanDefinition;

	private final String beanName;

	@Nullable
	private final String[] aliases;

    ...

}
           
  • 擴充别名的時候會用到
  • 這一節,我們先對整體的beanDefinition家族有個了解,後面講源碼的時候會用到。
springboot源碼解析-管中窺豹系列之BeanDefinition(八)一、前言二、BeanDefinition三、源碼分析

歡迎關注微信公衆号:豐極,更多技術學習分享。