Spring中Bean註入源碼示例解析

BeanDefinition和Bean

在Spring中Bean的註入涉及兩部分:

  • BeanDefinition
  • Bean

兩個對象存在先後順序,先註入BeanDefinition之後才執行Bean對象的註入。

那麼兩者有什麼關聯呢?

BeanDefinition和Bean的關系:可以理解為BeanDefinition是Bean的包裝類,類似Java中類和屬性的關系。

BeanDefinition是對Bean對象的包裝,BeanDefinition中封裝瞭Bean相關的描述,比如bean對象,bean的單例還是原型、bean的父級、懶加載方式等等。

public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
	void setScope(@Nullable String scope);
	@Nullable
	String getScope();
	void setLazyInit(boolean lazyInit);
	boolean isLazyInit();
}
//bean的原數據
public interface BeanMetadataElement {
	@Nullable
	Object getSource();
}

在BeanDefinition中繼承瞭BeanMetadataElement,該類是Bean的原數據,該類中實例化瞭Bean對象為Object。因此在BeanDefinition中調用getSource就可以獲取到Bean對象。

分清楚BeanDefinition和Bean之後再看BeanDefinition的註入,因為隻有註入瞭BeanDefinition才會註入後續的Bean。

BeanDefinition的註入

BeanDefinitionRegistry接口

BeanDefinitin的註入通過接口BeanDefinitionRegistry抽象瞭各種對BeanDefinition的操作,例如

  • BeanDefinitionRegistry#registerBeanDefinition(註入到beanDefinition中)
  • BeanDefinitionRegistry#removeBeanDefinition(從beanDefinition容器中移除)
  • BeanDefinitionRegistry#getBeanDefinition(獲取BeanDefinition)
public interface BeanDefinitionRegistry extends AliasRegistry {
	void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
			throws BeanDefinitionStoreException;
	void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
	BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
	boolean containsBeanDefinition(String beanName);
	String[] getBeanDefinitionNames();
	int getBeanDefinitionCount();
	boolean isBeanNameInUse(String beanName);
}

該接口是操作BeanDefinition的基礎接口,該類是一個接口類型並不是一個抽象類,因此該接口中不會定義任何BeanDefinition的容器,BeanDefinition的容器隻在各實現類中定義並使用。任何需要操作BeanDefinition的實現都需要實現該接口。

BeanDefinitionRegistry的實現類

找到操作BeanDefinition的接口就可以通過接口查看接口的實現類,進而找到BeanDefinition的應用。

BeanDefinition常見的幾個實現類有:

  • DefaultListableBeanFactory
  • SimpleBeanDefinitionRegistry

SimpleBeanDefinitionRegistry

public class SimpleBeanDefinitionRegistry extends SimpleAliasRegistry implements BeanDefinitionRegistry {
	/**beanDefinition的容器,通過ConcurrentHashMap實現. */
	private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(64);
	//該類的實現方法隻是實現瞭最簡單的beanDefinition的註入,在Java開發環境中並不會使用該實現類
	@Override
	public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
			throws BeanDefinitionStoreException {
		Assert.hasText(beanName, "'beanName' must not be empty");
		Assert.notNull(beanDefinition, "BeanDefinition must not be null");
		this.beanDefinitionMap.put(beanName, beanDefinition);
	}
}

該實現類中隻做瞭最簡單的註入功能,沒有任何的邏輯處理,因此在實際開發過程中Spring並不會使用該類。

DefaultListableBeanFactory

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
		//beanDefinition的容器
		private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
		//向BeanFactory中註入BeanDefinition
		public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException {
			Assert.hasText(beanName, "Bean name must not be empty");
			Assert.notNull(beanDefinition, "BeanDefinition must not be null");
			if (beanDefinition instanceof AbstractBeanDefinition) {
				try {
					((AbstractBeanDefinition) beanDefinition).validate();
				}
				catch (BeanDefinitionValidationException ex) {
					throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
							"Validation of bean definition failed", ex);
				}
			}
			BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName);
			//判斷beanDefinition容器中是否已存在該beanDefinition
			if (existingDefinition != null) {
				//是否可以覆蓋
				if (!isAllowBeanDefinitionOverriding()) {
					throw new BeanDefinitionOverrideException(beanName, beanDefinition, existingDefinition);
				} else if (existingDefinition.getRole() < beanDefinition.getRole()) {
					// e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
					if (logger.isInfoEnabled()) {
						logger.info("Overriding user-defined bean definition for bean '" + beanName +
								"' with a framework-generated bean definition: replacing [" +
								existingDefinition + "] with [" + beanDefinition + "]");
					}
				} else if (!beanDefinition.equals(existingDefinition)) {
					if (logger.isDebugEnabled()) {
						logger.debug("Overriding bean definition for bean '" + beanName +
								"' with a different definition: replacing [" + existingDefinition +
								"] with [" + beanDefinition + "]");
					}
				} else {
					if (logger.isTraceEnabled()) {
						logger.trace("Overriding bean definition for bean '" + beanName +
								"' with an equivalent definition: replacing [" + existingDefinition +
								"] with [" + beanDefinition + "]");
					}
				}
				//覆蓋beanDefinition
				this.beanDefinitionMap.put(beanName, beanDefinition);
			} else { //容器中不存在該beanDefinition的處理
				if (hasBeanCreationStarted()) {
					synchronized (this.beanDefinitionMap) { //對beanDefinition加鎖
						//註入該beanDefinition
						this.beanDefinitionMap.put(beanName, beanDefinition);
						List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
						updatedDefinitions.addAll(this.beanDefinitionNames);
						updatedDefinitions.add(beanName);
						this.beanDefinitionNames = updatedDefinitions;
						if (this.manualSingletonNames.contains(beanName)) {
							Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames);
							updatedSingletons.remove(beanName);
							this.manualSingletonNames = updatedSingletons;
						}
					}
				}
				else {
					// Still in startup registration phase
					this.beanDefinitionMap.put(beanName, beanDefinition);
					this.beanDefinitionNames.add(beanName);
					this.manualSingletonNames.remove(beanName);
				}
				this.frozenBeanDefinitionNames = null;
			}
		if (existingDefinition != null || containsSingleton(beanName)) {
			resetBeanDefinition(beanName);
		}
	}
}

DefaultListableBeanFactory是默認使用的註入BeanDefinition的實現類,可以看到該實現類中對註入BeanDefinition做瞭很多的邏輯判斷,日常開發啟動容器過程中都會使用該實現類註入BeanDefinition進行後續的Bean註入。

Bean的註入

BeanDefinition註入後後續執行Bean的註入,bean註入的方式也是將bean註入到bean的容器中,因此在Spring中BeanDefinition和Bean都是通過容器化的方式操作的,那麼在Bean的註入中也通過對應的接口定義對Bean的操作,該接口就是SingletonBeanRegistry

SingletonBeanRegistry接口

該接口實現瞭對Bean的操作:

  • SingletonBeanRegistry#registerSingleton(將bean註入到容器中)
  • SingletonBeanRegistry#getSingleton(從容器中獲取bean)
public interface SingletonBeanRegistry {
	//註入bean
	void registerSingleton(String beanName, Object singletonObject);
	//獲取bean
	@Nullable
	Object getSingleton(String beanName);
	//是否存在bean
	boolean containsSingleton(String beanName);
	String[] getSingletonNames();
	int getSingletonCount();
	Object getSingletonMutex();
}

SingletonBeanRegistry的實現類

SingletonBeanRegistry的實現類有多個,分別為:

  • AbstractBeanFactory
  • DefaultListableBeanFactory
  • DefaultSingletonBeanRegistry
  • FactoryBeanRegistrySupport
  • AbstractAutowireCapableBeanFactory

下面說兩個有代表性的:AbstractBeanFactory和DefaultSingletonBeanRegistry

AbstractBeanFactory

該實現類是一個抽象類,即該類是bean的工廠類,其中主要實現的是可以復用的具體邏輯,該抽象類中包含獲取bean、beanPostProcessor、初始化bean、銷毀bean等對bean的操作抽象類。

public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {
	//獲取bean的方式平時使用ApplicationContext就是最終調用該方法
	public Object getBean(String name) throws BeansException {
		return doGetBean(name, null, null, false);
	}
	@Override
	public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
		return doGetBean(name, requiredType, null, false);
	}
	@Override
	public Object getBean(String name, Object... args) throws BeansException {
		return doGetBean(name, null, args, false);
	}
	public <T> T getBean(String name, @Nullable Class<T> requiredType, @Nullable Object... args)
			throws BeansException {
		return doGetBean(name, requiredType, args, false);
	}
	/**
	* 從容器中獲取bean的主要邏輯
	*/
	protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
			@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
		final String beanName = transformedBeanName(name);
		Object bean;
		// Eagerly check singleton cache for manually registered singletons.
		Object sharedInstance = getSingleton(beanName);
		if (sharedInstance != null && args == null) {
			if (logger.isTraceEnabled()) {
				if (isSingletonCurrentlyInCreation(beanName)) {
					logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
							"' that is not fully initialized yet - a consequence of a circular reference");
				}
				else {
					logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
				}
			}
			bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
		}
		else {
			//省略部分代碼
			try {
				//先獲取beanDefinition
				final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
				checkMergedBeanDefinition(mbd, beanName, args);
				// Guarantee initialization of beans that the current bean depends on.
				String[] dependsOn = mbd.getDependsOn();
				if (dependsOn != null) {
					for (String dep : dependsOn) {
						if (isDependent(beanName, dep)) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
									"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
						}
						registerDependentBean(dep, beanName);
						try {
							getBean(dep);
						}
						catch (NoSuchBeanDefinitionException ex) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
									"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
						}
					}
				}
				// 單例模式下獲取bean的方式
				if (mbd.isSingleton()) {
					sharedInstance = getSingleton(beanName, () -> {
						try {
							return createBean(beanName, mbd, args);
						}
						catch (BeansException ex) {
							// Explicitly remove instance from singleton cache: It might have been put there
							// eagerly by the creation process, to allow for circular reference resolution.
							// Also remove any beans that received a temporary reference to the bean.
							destroySingleton(beanName);
							throw ex;
						}
					});
					bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
				}
				else if (mbd.isPrototype()) {//原型模式下的bean的獲取
					// It's a prototype -> create a new instance.
					Object prototypeInstance = null;
					try {
						beforePrototypeCreation(beanName);
						//原型模式每次都需要創建bean
						prototypeInstance = createBean(beanName, mbd, args);
					}
					finally {
						afterPrototypeCreation(beanName);
					}
					bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
				}
				else { 
					String scopeName = mbd.getScope();
					final Scope scope = this.scopes.get(scopeName);
					if (scope == null) {
						throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
					}
					try {
						Object scopedInstance = scope.get(beanName, () -> {
							beforePrototypeCreation(beanName);
							try {
								return createBean(beanName, mbd, args);
							}
							finally {
								afterPrototypeCreation(beanName);
							}
						});
						bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
					}
					catch (IllegalStateException ex) {
						throw new BeanCreationException(beanName,
								"Scope '" + scopeName + "' is not active for the current thread; consider " +
								"defining a scoped proxy for this bean if you intend to refer to it from a singleton",
								ex);
					}
				}
			}
			catch (BeansException ex) {
				cleanupAfterBeanCreationFailure(beanName);
				throw ex;
			}
		}
		//省略部分代碼
		return (T) bean;
	}
	//將實現beanPostProcessor的實現類加入到鏈表中用於對bean的前置或者後置處理
	@Override
	public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
		Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
		// Remove from old position, if any
		this.beanPostProcessors.remove(beanPostProcessor);
		// Track whether it is instantiation/destruction aware
		if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
			this.hasInstantiationAwareBeanPostProcessors = true;
		}
		if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
			this.hasDestructionAwareBeanPostProcessors = true;
		}
		// Add to end of list
		this.beanPostProcessors.add(beanPostProcessor);
	}
	//銷毀bean的方法
	@Override
	public void destroyBean(String beanName, Object beanInstance) {
		destroyBean(beanName, beanInstance, getMergedLocalBeanDefinition(beanName));
	}
	protected void destroyBean(String beanName, Object bean, RootBeanDefinition mbd) {
		new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), getAccessControlContext()).destroy();
	}
}

DefaultSingletonBeanRegistry

該實現類通過名字也可以推斷出該實現類是對單例bean的註入,該類中實現瞭bean的所有容器,平時開發過程中Spring容器啟動就是使用瞭該實現類中的容器,該實現類是對bean最終註入的實現,涉及的四個容器主要有:

  • singletonObjects
  • singletonFactories
  • earlySingletonObjects
  • registeredSingletons

四個容器其中三個的核心作用是解決循環依賴,這裡不展開說。

找一條主線梳理下Bean註入的過程,當BeanDefinition註入成功後,後續執行bean的註入,以AbstractAutowireCapableBeanFactory的實現為例,該實現類中會對bean的註入,整個註入的鏈路如下:

  • 獲取資源數據:CommonAnnotationBeanPostProcessor#autowireResource
  • 通過beanName解析bean:AbstractAutowireCapableBeanFactory#resolveBeanByName
  • 調用抽象類中獲取bean:AbstractBeanFactory#getBean
  • 執行抽象類中獲取bean的核心邏輯:AbstractBeanFactory#doGetBean
  • bean不存在時創建bean:AbstractAutowireCapableBeanFactory#createBean
  • 獲取單例bean:efaultSingletonBeanRegistry#getSingleton
  • 單例bean不存在將類註入到容器中:DefaultSingletonBeanRegistry#addSingleton
public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {
	//單例bean的容器,開發過程中獲取的bean都是從該容器中獲取的:即所謂的一級緩存
	private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
	//單例bean的beanFactory的容器:即所謂的三級緩存
	private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
	//提前初始化的bean的容器,該容器的bean並沒有任何屬性值隻是提前將bean註入到該容器中用於後續的判斷
	//即所謂的二級緩存
	private final Map<String, Object> earlySingletonObjects = new HashMap<>(16);
	//已註冊的單例bean的beanName列表
	private final Set<String> registeredSingletons = new LinkedHashSet<>(256);
	//註入bean的方法
	public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException {
		Assert.notNull(beanName, "Bean name must not be null");
		Assert.notNull(singletonObject, "Singleton object must not be null");
		synchronized (this.singletonObjects) {
			Object oldObject = this.singletonObjects.get(beanName);
			if (oldObject != null) {
				throw new IllegalStateException("Could not register object [" + singletonObject +
												"] under bean name '" + beanName + "': there is already object [" + oldObject + "] bound");
			}
			addSingleton(beanName, singletonObject);
		}
	}
	/**
	* 將bean註入容器中,操作四個容器主要用於bean的循環依賴問題
	*/
	protected void addSingleton(String beanName, Object singletonObject) {
		synchronized (this.singletonObjects) {
			this.singletonObjects.put(beanName, singletonObject);
			this.singletonFactories.remove(beanName);
			this.earlySingletonObjects.remove(beanName);
			this.registeredSingletons.add(beanName);
		}
	}
}

總結

以上就是對bean註入從源碼的梳理,該梳理主要從bean註入前的準備(beanDefinition的註入)和bean註入的實現來完成,從整個梳理中學到最多的主要有兩點:

  • 抽象的思想:在BeanDefinition的實現和Bean的實現邏輯中,都完成高度抽象並且遵循單一原則和高度抽象,代碼的可擴展性和復用性很高,我們作為開發者也可以通過Spring暴露的各種Registry接口實現對Bean的操作。
  • 容器化思想:從BeanDefinition和Bean的註入來看,整個的實現思路都是基於容器化思想來實現的,將開發中需要關心的對象都實例化到容器中,開發者在使用時隻要從容器中獲取即可,並且獲取bean的方式也很靈活,開發者在使用和擴展性上更便利。

對於源碼的學習自己理解來說並不是背其中的代碼,而是學習大佬實現代碼的思想,為什麼可以抽象出擴展性這麼高的代碼,並且從源頭梳理下來都能很輕松的按照主線一步一步梳理到目的地,這也是每個開發者需要學習的地方,提高自己代碼的可讀性和可擴展性,能夠掌握面向對象的核心:封裝、繼承、多態

以上就是Spring中Bean註入源碼示例解析的詳細內容,更多關於Spring Bean註入的資料請關註WalkonNet其它相關文章!

推薦閱讀: