歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring源碼解析 - BeanFactory

Spring源碼解析 - BeanFactory

日期:2017/3/1 9:18:10   编辑:Linux編程

BeanFactory是Spring實現依賴注入的核心接口,提供應用的統一配置注冊功能,實現業務開發解偶。使用getBean可以代替單例,原型設計模式。

頂重要的BeanFactory裡注釋寫得太好了.所以咱們先翻譯下注釋,後面再詳細分析.

重點直接看紅色標注吧.

The root interface for accessing a Spring bean container.
This is the basic client view of a bean container;
further interfaces such as {@link ListableBeanFactory} and
{@link org.springframework.beans.factory.config.ConfigurableBeanFactory}
are available for specific purposes.

訪問一個Spring bean容器的根接口。這是一個bean容器的基本客戶端視圖; 進一步的接口,如{@link ListableBeanFactory}和 {@link org.springframework.beans.factory.config.ConfigurableBeanFactory} 可用於特殊目的。

This interface is implemented by objects that hold a number of bean definitions,
each uniquely identified by a String name. Depending on the bean definition,
the factory will return either an independent instance of a contained object
(the Prototype design pattern), or a single shared instance (a superior
alternative to the Singleton design pattern, in which the instance is a
singleton in the scope of the factory). Which type of instance will be returned
depends on the bean factory configuration: the API is the same. Since Spring
2.0, further scopes are available depending on the concrete application
context (e.g. "request" and "session" scopes in a web environment).

此接口由持有一些bean定義的對象來實現,每個bean由String字符串唯一標識。根據bean定義, 工廠將返回一個獨立對象實例(原型設計模式),或者一個單個共享實例(Singleton設計模式的優雅代替實現,其中該實例是一個factory范圍內的單例)。實例的哪種類型將被返回依賴於bean工廠配置:即使API是一樣的。從Spring2.0開始,作用域擴展到根據具體的應用上下文,如web環境的request,session。

The point of this approach is that the BeanFactory is a central registry
of application components, and centralizes configuration of application
components (no more do individual objects need to read properties files,
for example). See chapters 4 and 11 of "Expert One-on-One J2EE Design and
Development" for a discussion of the benefits of this approach.

這種方案的關鍵是,BeanFactory的是應用程序組件注冊的中心,同時集中應用程序組件的配置(程序模塊不再需要讀取諸如properties的配置文件)。這種設計的更多好處討論詳見的<J2EE設計開發編程指南>第4和第11章.

強烈推薦看這本書 《J2EE設計開發編程指南》中文PDF掃描版+源碼 下載見http://www.linuxidc.com/Linux/2016-03/129120.htm

Note that it is generally better to rely on Dependency Injection
("push" configuration) to configure application objects through setters
or constructors, rather than use any form of "pull" configuration like a
BeanFactory lookup. Spring's Dependency Injection functionality is
implemented using this BeanFactory interface and its subinterfaces.

相比諸如 BeanFactory 中查找的pull配置方式,通過setters或者構造方法,依賴注入的方式配置應用對象更好.Spring的依賴注入功能就是通過實現BeanFactory和其子接口實現的.

Normally a BeanFactory will load bean definitions stored in a configuration
source (such as an XML document), and use the {@code org.springframework.beans}
package to configure the beans. However, an implementation could simply return
Java objects it creates as necessary directly in Java code. There are no
constraints on how the definitions could be stored: LDAP, RDBMS, XML,
properties file, etc. Implementations are encouraged to support references
amongst beans (Dependency Injection).

通常,一個BeanFactory會從配置源(如X​​ML文件)中加載bena 定義,並使用{@code org.springframework.beans}包解析bean。然而,實現可以簡單地返回Java代碼直接新建的Java對象。這裡沒有限制bean 定義文件的格式:LDAP,RDBMS,XML.實現類歡迎支持應用而非bean(依賴注入)


In contrast to the methods in {@link ListableBeanFactory}, all of the
operations in this interface will also check parent factories if this is a
{@link HierarchicalBeanFactory}. If a bean is not found in this factory instance,
the immediate parent factory will be asked. Beans in this factory instance
are supposed to override beans of the same name in any parent factory.

對比{@link ListableBeanFactory}中的方法,如果這是一個{@link HierarchicalBeanFactory},這個接口的全部實現都會查找父工廠.如果在這個工廠實例找不到bean,去直接父工廠查找。factory實例中的bean會覆蓋父factory實例中的同名bean。

Bean factory implementations should support the standard bean lifecycle interfaces
as far as possible. The full set of initialization methods and their standard order is:
bean factory 實現類應該盡量支持標准bean的生命周期接口.全套的初始化方法,已經排序如下

感覺這坨概念得好好理理
1. BeanNameAware's {@code setBeanName}
2. BeanClassLoaderAware's {@code setBeanClassLoader}
3. BeanFactoryAware's {@code setBeanFactory}
4. ResourceLoaderAware's {@code setResourceLoader}
(only applicable when running in an application context)
5. ApplicationEventPublisherAware's {@code setApplicationEventPublisher}
(only applicable when running in an application context)
6. MessageSourceAware's {@code setMessageSource}
(only applicable when running in an application context)
7. ApplicationContextAware's {@code setApplicationContext}
(only applicable when running in an application context)
8. ServletContextAware's {@code setServletContext}
(only applicable when running in a web application context)
9. {@code postProcessBeforeInitialization} methods of BeanPostProcessors
10. InitializingBean's {@code afterPropertiesSet}
11. a custom init-method definition
12. {@code postProcessAfterInitialization} methods of BeanPostProcessors

On shutdown of a bean factory, the following lifecycle methods apply:
1. DisposableBean's {@code destroy}
2. a custom destroy-method definition

package org.springframework.beans.factory;
public interface BeanFactory {

/**
* 用於區分是否直接獲取FactoryBean實例.
* bean以&開頭表示獲取FactoryBean實例.否則獲取created的實例.For example, if the bean named
* {@code myJndiObject} is a FactoryBean, getting {@code &myJndiObject}
* will return the factory, not the instance returned by the factory.
*/
String FACTORY_BEAN_PREFIX = "&";

/**
* 返回一個原型或者單例實例.
* 搶單例,原型設計模式的飯碗
* 可以根據別名查找,也可以去父容器實例查找
*/
Object getBean(String name) throws BeansException;

/**
* 加個類型
*/
<T> T getBean(String name, Class<T> requiredType) throws BeansException;

/**
* 根據類型獲取bean實例.可以是接口或子類,但不能是{@code null}.
* {@link ListableBeanFactory}也可以使用類型轉化為name進行查找.更多bean集合的操作可以看
* ListableBeanFactory和BeanFactoryUtils
*/
<T> T getBean(Class<T> requiredType) throws BeansException;

/**
* 多了構造方法,工廠方法的參數
*/
Object getBean(String name, Object... args) throws BeansException;

/**
* 判斷是否包含bean(包括別名,父容器)
* 陷阱出現:這邊不管類是否抽象類,懶加載,是否在容器范圍內,只要符合都返回true,所以這邊true,不一定能從getBean獲取實例
*/
boolean containsBean(String name);

/**
* 是否單例
*/
boolean isSingleton(String name) throws NoSuchBeanDefinitionException;

/**
* 是否原型
*/
boolean isPrototype(String name) throws NoSuchBeanDefinitionException;

/**
* 是否有跟name匹配類型的bean
*/
boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;

/**
* 根據bean name獲取類型
*/
Class<?> getType(String name) throws NoSuchBeanDefinitionException;

/**
* 獲取別名
*/
String[] getAliases(String name);

}

Spring中如何配置Hibernate事務 http://www.linuxidc.com/Linux/2013-12/93681.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

基於 Spring 設計並實現 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm

Spring-3.2.4 + Quartz-2.2.0集成實例 http://www.linuxidc.com/Linux/2013-10/91524.htm

使用 Spring 進行單元測試 http://www.linuxidc.com/Linux/2013-09/89913.htm

運用Spring注解實現Netty服務器端UDP應用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm

Spring 3.x 企業應用開發實戰 PDF完整高清掃描版+源代碼 http://www.linuxidc.com/Linux/2013-10/91357.htm

Spring 的詳細介紹:請點這裡
Spring 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved