原文
public interface BeanFactory
The root interface for accessing a Spring bean container.
This is the basic client view of a bean container; further interfaces such as ListableBeanFactory and ConfigurableBeanFactory are available for specific purposes.
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).
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.
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.
Normally a BeanFactory will load bean definitions stored in a configuration source (such as an XML document), and use the 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).
In contrast to the methods in ListableBeanFactory, all of the operations in this interface will also check parent factories if this is a 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.
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:
- BeanNameAware’s setBeanName
- BeanClassLoaderAware’s setBeanClassLoader
- BeanFactoryAware’s setBeanFactory
- EnvironmentAware’s setEnvironment
- EmbeddedValueResolverAware’s setEmbeddedValueResolver
- ResourceLoaderAware’s setResourceLoader (only applicable when running in an application context)
- ApplicationEventPublisherAware’s setApplicationEventPublisher (only applicable when running in an application context)
- MessageSourceAware’s setMessageSource (only applicable when running in an application context)
- ApplicationContextAware’s setApplicationContext (only applicable when running in an application context)
- ServletContextAware’s setServletContext (only applicable when running in a web application context)
- postProcessBeforeInitialization methods of BeanPostProcessors
- InitializingBean’s afterPropertiesSet
- a custom init-method definition
- postProcessAfterInitialization methods of BeanPostProcessors
On shutdown of a bean factory, the following lifecycle methods apply:
1. postProcessBeforeDestruction methods of DestructionAwareBeanPostProcessors
2. DisposableBean’s destroy
3. a custom destroy-method definition
大致翻译
用于访问Spring Bean容器的根接口。
这是bean容器的基本客户端视图;其他接口,如ListableBeanFactory和ConfigurableBeanFactory可用于特定目的。
这个接口由包含许多bean定义对象的实现,每个bean定义由一个字符串名作为唯一标识。根据bean定义,工厂将返回包含对象的独立实例(原型设计模式)或单个共享实例(单例设计模式的高级替代方案,其中实例是工厂范围内的单例)。返回哪种类型的实例取决于bean工厂配置:API是相同的。自Spring2.0以来,根据具体的应用程序上下文,还可以使用更多的作用域(例如web环境中的“request”和“session”作用域),。
这种方法的要点是BeanFactory是应用程序组件的中心注册表,并集中配置应用程序组件(例如,单个对象不再需要读取属性文件)。有关这种方法的好处的讨论,请参阅“专家一对一J2EE设计和开发”的第4章和第11章。。
请注意,通常最好依靠依赖项注入(“push”配置)通过setter或构造函数配置应用程序对象,而不是使用任何形式的“pull”配置,如BeanFactory查找。Spring的依赖项注入功能是使用这个BeanFactory接口及其子接口实现的。
通常,BeanFactory将加载存储在配置源中的bean定义(例如XML文档),并使用org.springframework.beans包来配置bean。但是,实现可以简单地返回它根据必要的Java对象直接在Java代码中创建。对于如何存储定义,没有约束:鼓励LDAP,RDBMS,XML,属性文件等实现。在Bean(依赖项注入)中支持引用。
与ListableBeanFactory中的方法不同,如果这是HierarchycalBeanFactory,则此接口中的所有操作也将检查父工厂。如果在此工厂实例中找不到bean,将直接访问父工厂。此工厂实例中的bean会覆盖任何父工厂中同名的bean.
Bean Factory实现应尽可能支持标准Bean 生命周期接口。全套初始化方法及其标准流程是:
- BeanNameAware’s setBeanName
- BeanClassLoaderAware’s setBeanClassLoader
- BeanFactoryAware’s setBeanFactory
- EnvironmentAware’s setEnvironment
- EmbeddedValueResolverAware’s setEmbeddedValueResolver
- ResourceLoaderAware’s setResourceLoader (仅适用于在应用程序上下文中运行时)
- ApplicationEventPublisherAware’s setApplicationEventPublisher (仅适用于在应用程序上下文中运行时)
- MessageSourceAware’s setMessageSource (仅适用于在应用程序上下文中运行时)
- ApplicationContextAware’s setApplicationContext (仅适用于在应用程序上下文中运行时)
- ServletContextAware’s setServletContext (仅适用于在应用程序上下文中运行时)
- postProcessBeforeInitialization methods of BeanPostProcessors
- InitializingBean’s afterPropertiesSet
- a custom init-method definition
- postProcessAfterInitialization methods of BeanPostProcessors
在关闭bean工厂时,将调用以下方法:
1. postProcessBeforeDestruction methods of DestructionAwareBeanPostProcessors
2. DisposableBean’s destroy
3. a custom destroy-method definition
附上链接:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/BeanFactory.html