1. 字面意思
- BeanFactory:bean的工厂,也就是生产bean的工厂,它是工厂,不是bean。
- FactoryBean: 一个作为工厂的bean,首先它是一个bean,其次它才是一个工厂。
2. 源码解释
2.1 BeanFactory
1. 访问spring bean容器的根接口
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 org.springframework.beans.factory.config.ConfigurableBeanFactory are available for specific purposes.
2. bean的作用域
实现该接口的objects持有bean definitions(bean定义信息),bean根据bean的定义信息返回单例bean和原型bean。
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).
3. 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:
- BeanNameAware’s setBeanName // 设置bean的name
- BeanClassLoaderAware’s setBeanClassLoader // 设置加载bean的 class loader
- BeanFactoryAware’s setBeanFactory // 实现这个接口的bean想知道自己属于哪个BeanFactory
- EnvironmentAware’s setEnvironment // 获得Environment对象
- 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) // 获得ApplicationContext,常用
- 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:
15. postProcessBeforeDestruction methods of DestructionAwareBeanPostProcessors
16. DisposableBean’s destroy
17. a custom destroy-method definition
2.2 FactoryBean
- 对外暴露bean,而不是它自身
Interface to be implemented by objects used within a BeanFactory which are themselves factories for individual objects. If a bean implements this interface, it is used as a factory for an object to expose, not directly as a bean instance that will be exposed itself.
这个接口是在BeanFactory内使用的。一个实现这个接口的bean,可以作为工厂对外暴露创建好的对象。这个在spring整合其他框架时常用。
NB: A bean that implements this interface cannot be used as a normal bean. A FactoryBean is defined in a bean style, but the object exposed for bean references (getObject()) is always the object that it creates.
注意实现这个接口的bean不能当做一个正常的bean。一个FactoryBean只是作为bean的形态,但是对外暴露是它创建的对象,而不是它自身。
3. 总结
项目 | 相同点 | 不同点 |
BeanFactory | 都能生产bean | 这是一个factory,生产很多bean |
FactoryBean | 都能生产bean | 这是一个bean,生产特定的bean,FactoryBean被BeanFactory使用 |