spring源码:BeanFactory和FactoryBean的区别

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:

  1. BeanNameAware’s setBeanName // 设置bean的name
  2. BeanClassLoaderAware’s setBeanClassLoader // 设置加载bean的 class loader
  3. BeanFactoryAware’s setBeanFactory // 实现这个接口的bean想知道自己属于哪个BeanFactory
  4. EnvironmentAware’s setEnvironment // 获得Environment对象
  5. EmbeddedValueResolverAware’s setEmbeddedValueResolver
  6. ResourceLoaderAware’s setResourceLoader (only applicable when running in an application context)
  7. ApplicationEventPublisherAware’s setApplicationEventPublisher (only applicable when running in an application context)
  8. MessageSourceAware’s setMessageSource (only applicable when running in an application context)
  9. ApplicationContextAware’s setApplicationContext (only applicable when running in an application context) // 获得ApplicationContext,常用
  10. ServletContextAware’s setServletContext (only applicable when running in a web application context)
  11. postProcessBeforeInitialization methods of BeanPostProcessors
  12. InitializingBean’s afterPropertiesSet
  13. a custom init-method definition
  14. 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

  1. 对外暴露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使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值