1. 程式人生 > >Spring原始碼學習之BeanFactory體系結構

Spring原始碼學習之BeanFactory體系結構

public interface AutowireCapableBeanFactory extends BeanFactory {

    /**
     * Constant that indicates no externally defined autowiring. Note that
     * BeanFactoryAware etc and annotation-driven injection will still be applied.
     */
    int AUTOWIRE_NO = 0;

    /**
     * Constant that indicates autowiring bean properties by name
     * (applying to all bean property setters).
     
*/ int AUTOWIRE_BY_NAME = 1; /** * Constant that indicates autowiring bean properties by type * (applying to all bean property setters). */ int AUTOWIRE_BY_TYPE = 2; /** * Constant that indicates autowiring the greediest constructor that * can be satisfied (involves resolving the appropriate constructor).
*/ int AUTOWIRE_CONSTRUCTOR = 3; /** * Constant that indicates determining an appropriate autowire strategy * through introspection of the bean class. */ @Deprecated int AUTOWIRE_AUTODETECT = 4; //-------------------------------------------------------------------------
// Typical methods for creating and populating external bean instances //------------------------------------------------------------------------- /** * Fully create a new bean instance of the given class. * <p>Performs full initialization of the bean, including all applicable * {@link BeanPostProcessor BeanPostProcessors}. * <p>Note: This is intended for creating a fresh instance, populating annotated * fields and methods as well as applying all standard bean initialiation callbacks. * It does <i>not</> imply traditional by-name or by-type autowiring of properties; * use {@link #createBean(Class, int, boolean)} for that purposes. */ //通過指定的class建立一個全新的Bean例項 <T> T createBean(Class<T> beanClass) throws BeansException; /** * Populate the given bean instance through applying after-instantiation callbacks * and bean property post-processing (e.g. for annotation-driven injection). * <p>Note: This is essentially intended for (re-)populating annotated fields and * methods, either for new instances or for deserialized instances. It does * <i>not</i> imply traditional by-name or by-type autowiring of properties; * use {@link #autowireBeanProperties} for that purposes. */ //給定物件,根據註釋,後處理器進行自動裝配 void autowireBean(Object existingBean) throws BeansException; /** * Configure the given raw bean: autowiring bean properties, applying * bean property values, applying factory callbacks such as {@code setBeanName} * and {@code setBeanFactory}, and also applying all bean post processors * (including ones which might wrap the given raw bean). * <p>This is effectively a superset of what {@link #initializeBean} provides, * fully applying the configuration specified by the corresponding bean definition. * <b>Note: This method requires a bean definition for the given name!</b> */ //自動裝配Bean的屬性,應用處理器等 Object configureBean(Object existingBean, String beanName) throws BeansException; /** * Resolve the specified dependency against the beans defined in this factory. */ Object resolveDependency(DependencyDescriptor descriptor, String beanName) throws BeansException; //------------------------------------------------------------------------- // Specialized methods for fine-grained control over the bean lifecycle //------------------------------------------------------------------------- /** * Fully create a new bean instance of the given class with the specified * autowire strategy. All constants defined in this interface are supported here. * <p>Performs full initialization of the bean, including all applicable * {@link BeanPostProcessor BeanPostProcessors}. This is effectively a superset * of what {@link #autowire} provides, adding {@link #initializeBean} behavior. * @see #AUTOWIRE_NO * @see #AUTOWIRE_BY_NAME * @see #AUTOWIRE_BY_TYPE * @see #AUTOWIRE_CONSTRUCTOR */根據給定的型別,指定的裝配策略,建立新的Bean例項 Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException; /** * Instantiate a new bean instance of the given class with the specified autowire * strategy. All constants defined in this interface are supported here. * Can also be invoked with {@code AUTOWIRE_NO} in order to just apply * before-instantiation callbacks (e.g. for annotation-driven injection). * <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors} * callbacks or perform any further initialization of the bean. This interface * offers distinct, fine-grained operations for those purposes, for example * {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor} * callbacks are applied, if applicable to the construction of the instance. * @param beanClass the class of the bean to instantiate * @param autowireMode by name or type, using the constants in this interface * @param dependencyCheck whether to perform a dependency check for object * references in the bean instance (not applicable to autowiring a constructor, * thus ignored there) * @return the new bean instance * @throws BeansException if instantiation or wiring failed * @see #AUTOWIRE_NO * @see #AUTOWIRE_BY_NAME * @see #AUTOWIRE_BY_TYPE * @see #AUTOWIRE_CONSTRUCTOR * @see #AUTOWIRE_AUTODETECT * @see #initializeBean * @see #applyBeanPostProcessorsBeforeInitialization * @see #applyBeanPostProcessorsAfterInitialization */ //根據給定的策略,型別,裝配Bean屬性 Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException; /** * Autowire the bean properties of the given bean instance by name or type. * Can also be invoked with {@code AUTOWIRE_NO} in order to just apply * after-instantiation callbacks (e.g. for annotation-driven injection). * <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors} * callbacks or perform any further initialization of the bean. This interface * offers distinct, fine-grained operations for those purposes, for example * {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor} * callbacks are applied, if applicable to the configuration of the instance. * @param existingBean the existing bean instance * @param autowireMode by name or type, using the constants in this interface * @param dependencyCheck whether to perform a dependency check for object * references in the bean instance * @throws BeansException if wiring failed * @see #AUTOWIRE_BY_NAME * @see #AUTOWIRE_BY_TYPE * @see #AUTOWIRE_NO */ void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck) throws BeansException; /** * Apply the property values of the bean definition with the given name to * the given bean instance. The bean definition can either define a fully * self-contained bean, reusing its property values, or just property values * meant to be used for existing bean instances. * <p>This method does <i>not</i> autowire bean properties; it just applies * explicitly defined property values. Use the {@link #autowireBeanProperties} * method to autowire an existing bean instance. * <b>Note: This method requires a bean definition for the given name!</b> * <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors} * callbacks or perform any further initialization of the bean. This interface * offers distinct, fine-grained operations for those purposes, for example * {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor} * callbacks are applied, if applicable to the configuration of the instance. * @param existingBean the existing bean instance * @param beanName the name of the bean definition in the bean factory * (a bean definition of that name has to be available) * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException * if there is no bean definition with the given name * @throws BeansException if applying the property values failed * @see #autowireBeanProperties */ void applyBeanPropertyValues(Object existingBean, String beanName) throws BeansException; /** * Initialize the given raw bean, applying factory callbacks * such as {@code setBeanName} and {@code setBeanFactory}, * also applying all bean post processors (including ones which * might wrap the given raw bean). * <p>Note that no bean definition of the given name has to exist * in the bean factory. The passed-in bean name will simply be used * for callbacks but not checked against the registered bean definitions. * @param existingBean the existing bean instance * @param beanName the name of the bean, to be passed to it if necessary * (only passed to {@link BeanPostProcessor BeanPostProcessors}) * @return the bean instance to use, either the original or a wrapped one * @throws BeansException if the initialization failed */ Object initializeBean(Object existingBean, String beanName) throws BeansException; /** * Apply {@link BeanPostProcessor BeanPostProcessors} to the given existing bean * instance, invoking their {@code postProcessBeforeInitialization} methods. * The returned bean instance may be a wrapper around the original. * @param existingBean the new bean instance * @param beanName the name of the bean * @return the bean instance to use, either the original or a wrapped one * @throws BeansException if any post-processing failed * @see BeanPostProcessor#postProcessBeforeInitialization */ Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException; /** * Apply {@link BeanPostProcessor BeanPostProcessors} to the given existing bean * instance, invoking their {@code postProcessAfterInitialization} methods. * The returned bean instance may be a wrapper around the original. * @param existingBean the new bean instance * @param beanName the name of the bean * @return the bean instance to use, either the original or a wrapped one * @throws BeansException if any post-processing failed * @see BeanPostProcessor#postProcessAfterInitialization */ Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException; /** * Destroy the given bean instance (typically coming from {@link #createBean}), * applying the {@link org.springframework.beans.factory.DisposableBean} contract as well as * registered {@link DestructionAwareBeanPostProcessor DestructionAwareBeanPostProcessors}. * <p>Any exception that arises during destruction should be caught * and logged instead of propagated to the caller of this method. * @param existingBean the bean instance to destroy */ void destroyBean(Object existingBean); /** * Resolve the specified dependency against the beans defined in this factory. * @param descriptor the descriptor for the dependency * @param beanName the name of the bean which declares the present dependency * @param autowiredBeanNames a Set that all names of autowired beans (used for * resolving the present dependency) are supposed to be added to * @param typeConverter the TypeConverter to use for populating arrays and * collections * @return the resolved object, or {@code null} if none found * @throws BeansException in dependency resolution failed */ Object resolveDependency(DependencyDescriptor descriptor, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException; }

相關推薦

Spring原始碼學習BeanFactory體系結構

public interface AutowireCapableBeanFactory extends BeanFactory { /** * Constant that indicates no externally defined autowiring. Note that

Spring原始碼學習BeanFactory和FactoryBean

今天在學習Spring原始碼的時候,發現了spring中不僅僅有BeanFactory,還有FactoryBean,突然覺得分不清這兩者之間有什麼不同,難道僅僅是名字嗎?但是從名字上我們也能看出一些端

Spring原始碼學習BeanFactory介面簡述

BeanFactory介面中定義如下: BeanFactory.class介面中定義的只是一下介面,通過實現這一系列介面,可以使用不同的bean的檢索方法(獲取不同的bean),很方便的從ioc容器中獲取需要的bean,從而忽略ioc的具體實現。 下面大致介紹一下這幾個介

spring原始碼學習路---IOC容器初始化要義bean定義載入(四)

上章說到要帶各位去看看bean定義載入的要義,其實就是loadBeanDefinitions這個方法的具體實現步驟,下面我們跟隨這個方法去看下它到底是如何載入bean定義的。 上面是我擷取的實現了loadBeanDefinitions的類級別截圖,loadBeanDefinit

spring原始碼學習路---深度分析IOC容器初始化過程(三)

分析FileSystemXmlApplicationContext的建構函式,到底都做了什麼,導致IOC容器初始化成功。 public FileSystemXmlApplicationContext(String[] configLocations, boolean ref

spring原始碼學習路---IOC實現原理(二)

上一章我們已經初步認識了BeanFactory和BeanDefinition,一個是IOC的核心工廠介面,一個是IOC的bean定義介面,上章提到說我們無法讓BeanFactory持有一個Map package org.springframework.beans.factory.supp

spring原始碼學習路---IOC初探(一)

首先把spring原始碼匯入,怎麼匯入百度下。 首先我們來說一下IOC,IOC是spring最核心的理念,包括AOP也要屈居第二,那麼IOC到底是什麼呢,四個字,控制反轉。 網上有不少是這麼解釋IOC的,說IOC是將物件的建立和依賴關係交給容器,這句話我相信不少人都知道,在我個人的理解

Jmeter初級學習Jmeter體系結構

jmeter基本原理 建立一個多執行緒,多執行緒執行取樣器產生大量負載,在執行過程中通過斷言來驗證結果的正確性,通過監聽器來記錄測試結果。若取樣器有引數化的需求,可以通過配置元件或前置處理器來完成;若有關聯需求,可以通過後置處理器來完成。 jmeter執行原理

Spring原始碼學習容器的功能擴充套件

我們都站在巨人的肩膀上 宣告:參考《Spring原始碼深度解析》 ApplicationContext和BeanFactory區別: Application提供了更多的擴充套件功能,簡單來說,就是:Application包含了BeanFactory的所

SPRING原始碼學習路(一)

結合《Spring技術內幕:深入解析SPRING架構與設計原理》這本書開啟Spring學習之路。 ps:之前其實已經看過一部分了,但是也就是看過,一看而過了。o(╯□╰)o 結合FileSystemXmlApplicationContext來分析  具體實現如下: /

Spring原始碼學習IOC實現原理(二)-ApplicationContext

一.Spring核心元件結構      總的來說Spring共有三個核心元件,分別為Core,Context,Bean.三大核心元件的協同工作主要表現在 :Bean是包裝我們應用程式自定義物件Object的,Object中存有資料,而Context就是為了這些資料存放提供一個生存環境,儲存各個 bean之間的

Spring原始碼學習IOC容器實現原理(一)-DefaultListableBeanFactory

從這個繼承體系結構圖來看,我們可以發現DefaultListableBeanFactory是第一個非抽象類,非介面類。實際IOC容器。所以這篇部落格以DefaultListableBeanFactoryIOC容器為基準進行IOC原理解析。 一.兩個重要介面 前面已經分析了BeanFactor,它的三個直接子

Spring原始碼學習AOP

我們都站在巨人的肩膀上 宣告:參考《spring原始碼深度解析》 1.Spring AOP主要採用動態代理實現,而動態代理分為兩種: JDK動態代理:其代理物件必須是某個介面的實現,它是通過在執行時期建立一個介面的實現類來完成對目標物件的代理。 CG

Spring原始碼學習路---深入AOP(終)

原文地址:https://blog.csdn.net/zuoxiaolong8810/article/details/8962353    上一章和各位一起看了一下springAOP的工作流程,當我們給出AOP相關的配置以後,直接從IOC容器中拿出來的就是已經加強過的bean

SPRING原始碼學習路(二)

     上一篇,已經對IOC容器的初始化過程有個大體認識,接著看IOC容器的依賴注入。       依賴注入的觸發是在使用者第一次向容器索要Bean時才觸發,當然也可以設定lazy-init讓容器提前完成Bean的預例項化,預例項化是在初始化過程中完成      我

Spring原始碼學習路---IOC實現原理(三)

原文地址:https://blog.csdn.net/zuoxiaolong8810/article/details/8548478          上一章我們已經初步認識了BeanFactory和BeanDefinition,一個是IOC的核心工廠介面,一個是IOC的be

spring原始碼學習六 元素處理過程

概述: </property-placeholder> 主要用來讀取配置資訊替換Bean中的${}佔位符, 常用配置如下: <context:property-placeholder ignore-resource-not-found="

spring原始碼學習整合Mybatis原理分析

開發十年,就只剩下這套架構體系了! >>>   

Spring原始碼學習【八】SpringMVCDispatcherServlet

目錄 一、前言 三、總結 一、前言 Web環境是Spring框架的重要應用場景,而SpringMVC又是Web開發中一個常用的框架,因此我們有必要學習一下SpringMVC的實現原理。 回到Web專案的配置檔案web.xml中,在使用SpringMV

spring原始碼學習筆記-初始化(三)-BeanFactory

轉自http://www.sandzhang.com/blog/2011/04/05/spring-study-notes-initialization-3/ refresh()方法中在上篇obtainFreshBeanFactory()方法建立了beanfactory物