BeanFactory是Spring實現依賴注入的核心接口.提供應用的統一配置註冊功能,實現業務開發解偶.使用getBean能夠代替單例,原型設計模式.web
頂重要的BeanFactory裏註釋寫得太好了.因此我們先翻譯下注釋,後面再詳細分析.spring
重點直接看紅色標註吧.編程
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。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章.app
強烈推薦看這本書,就是國內很差買了.ide
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和其子接口實現的.post
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會從配置源(如XML文件)中加載bena 定義,並使用{@code org.springframework.beans}包解析bean。然而,實現能夠簡單地返回Java代碼直接新建的Java對象。這裏沒有限制bean 定義文件的格式:LDAP,RDBMS,XML.實現類歡迎支持應用而非bean(依賴注入)ui
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.this
對比{@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
1 package org.springframework.beans.factory; 2 public interface BeanFactory { 3 4 /** 5 * 用於區分是否直接獲取FactoryBean實例. 6 * bean以&開頭表示獲取FactoryBean實例.不然獲取created的實例.For example, if the bean named 7 * {@code myJndiObject} is a FactoryBean, getting {@code &myJndiObject} 8 * will return the factory, not the instance returned by the factory. 9 */ 10 String FACTORY_BEAN_PREFIX = "&"; 11 12 /** 13 * 返回一個原型或者單例實例. 14 * 搶單例,原型設計模式的飯碗 15 * 能夠根據別名查找,也能夠去父容器實例查找 16 */ 17 Object getBean(String name) throws BeansException; 18 19 /** 20 * 加個類型 21 */ 22 <T> T getBean(String name, Class<T> requiredType) throws BeansException; 23 24 /** 25 * 根據類型獲取bean實例.能夠是接口或子類,但不能是{@code null}. 26 * {@link ListableBeanFactory}也可使用類型轉化爲name進行查找.更多bean集合的操做能夠看 27 * ListableBeanFactory和BeanFactoryUtils 28 */ 29 <T> T getBean(Class<T> requiredType) throws BeansException; 30 31 /** 32 * 多了構造方法,工廠方法的參數 33 */ 34 Object getBean(String name, Object... args) throws BeansException; 35 36 /** 37 * 判斷是否包含bean(包括別名,父容器) 38 * 陷阱出現:這邊無論類是否抽象類,懶加載,是否在容器範圍內,只要符合都返回true,因此這邊true,不必定能從getBean獲取實例 39 */ 40 boolean containsBean(String name); 41 42 /** 43 * 是否單例 44 */ 45 boolean isSingleton(String name) throws NoSuchBeanDefinitionException; 46 47 /** 48 * 是否原型 49 */ 50 boolean isPrototype(String name) throws NoSuchBeanDefinitionException; 51 52 /** 53 * 是否有跟name匹配類型的bean 54 */ 55 boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException; 56 57 /** 58 * 根據bean name獲取類型 59 */ 60 Class<?> getType(String name) throws NoSuchBeanDefinitionException; 61 62 /** 63 * 獲取別名 64 */ 65 String[] getAliases(String name); 66 67 }