你的位置:首页 > 软件开发 > Java > Spring源码学习之BeanFactory体系结构

Spring源码学习之BeanFactory体系结构

发布时间:2016-02-16 16:00:28
一.BeanFactory BeanFactory是Spring IOC容器的鼻祖,是IOC容器的基础接口,所有的容器都是从它这里继承实现而来。可见其地位。BeanFactory提供了最基本的IOC容器的功能,即所有的容器至少需要实现的标准。 ...

Spring源码学习之BeanFactory体系结构

一.BeanFactory

BeanFactory是Spring IOC容器的鼻祖,是IOC容器的基础接口,所有的容器都是从它这里继承实现而来。可见其地位。BeanFactory提供了最基本的IOC容器的功能,即所有的容器至少需要实现的标准。

 

      BeanFactory体系结构是典型的工厂方法模式,即什么样的工厂生产什么样的产品。BeanFactory是最基本的抽象工厂,而其他的IOC容器只不过是具体的工厂,对应着各自的Bean定义方法。但同时,其他容器也针对具体场景不同,进行了扩充,提供具体的服务。

二.BeanFacory源码

1.java doc

首先是Java doc,现在阅读源码的习惯是一定要理解了java doc的描述,因为这里是作者的描述,没有人比作者更明白这个类,这个接口要干什么。

/** * 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. * * <p>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). * * <p>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. * * <p>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. * * <p>Normally a BeanFactory will load bean definitions stored in a configuration * source (such as an @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, @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. * * <p>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:<br> * 1. BeanNameAware's {@code setBeanName}<br> * 2. BeanClassLoaderAware's {@code setBeanClassLoader}<br> * 3. BeanFactoryAware's {@code setBeanFactory}<br> * 4. ResourceLoaderAware's {@code setResourceLoader} * (only applicable when running in an application context)<br> * 5. ApplicationEventPublisherAware's {@code setApplicationEventPublisher} * (only applicable when running in an application context)<br> * 6. MessageSourceAware's {@code setMessageSource} * (only applicable when running in an application context)<br> * 7. ApplicationContextAware's {@code setApplicationContext} * (only applicable when running in an application context)<br> * 8. ServletContextAware's {@code setServletContext} * (only applicable when running in a web application context)<br> * 9. {@code postProcessBeforeInitialization} methods of BeanPostProcessors<br> * 10. InitializingBean's {@code afterPropertiesSet}<br> * 11. a custom init-method definition<br> * 12. {@code postProcessAfterInitialization} methods of BeanPostProcessors * * <p>On shutdown of a bean factory, the following lifecycle methods apply:<br> * 1. DisposableBean's {@code destroy}<br> * 2. a custom destroy-method definition *

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:Spring源码学习之BeanFactory体系结构

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录