Spring IOC and IOC container implementation principle

A. Spring IOC principle

1. Concept

Spring describes dependencies between Bean and Bean via a configuration file, using the Java language reflection function Bean instantiation and establishing dependencies between Bean. Spring IoC container Upon completion of these low-level work, but also provides a cache Bean instance, life cycle management, Bean instance agency, event publishing, resource loading and other advanced services.

2. Spring container top view

When reading the application provides Spring Bean boot configuration information and generate a corresponding Bean Spring container disposed in registry, and then according to this example of the registry Bean, assembled dependencies between Bean, for the upper application provide ready operating environment. Bean cache pool which is achieved HashMap

Spring IOC and IOC container implementation principle


3. IOC container to achieve

BeanFactory- framework for infrastructure

BeanFactory infrastructure Spring framework, Spring for itself; ApplicationContext for developers using the Spring framework, almost all of the applications we use BeanFactory ApplicationContext directly rather than the bottom.

Spring IOC and IOC container implementation principle


BeanDefinitionRegistry registry

(. 1). Spring configuration file in each node element represents all containers in the Spring, which is described by a configuration Bean BeanDefinition object. The interface provides methods BeanDefinitionRegistry BeanDefinition registration target to the vessel manually.

BeanFactory top level interface

(2) located at the top of the tree-based structure, which is the most important method getBean (String beanName), which returns the name of a particular Bean from the container, the BeanFactory functions obtained by expanding the other interface:

ListableBeanFactory

(3). 该接口定义了访问容器中 Bean 基本信息的若干方法,如查看 Bean 的个数、获取某一类型Bean 的配置名、查看容器中是否包括某一 Bean 等方法;

HierarchicalBeanFactory 父子级联

(4). 父子级联 IoC 容器的接口,子容器可以通过接口方法访问父容器; 通过HierarchicalBeanFactory 接口, Spring 的 IoC 容器可以建立父子层级关联的容器体系,子容器可以访问父容器中的 Bean,但父容器不能访问子容器的 Bean。Spring 使用父子容器实现了很多功能,比如在 Spring MVC 中,展现层 Bean 位于一个子容器中,而业务层和持久层的 Bean 位于父容器中。这样,展现层 Bean 就可以引用业务层和持久层的 Bean,而业务层和持久层的 Bean 则看不到展现层的 Bean。

ConfigurableBeanFactory

(5). 是一个重要的接口,增强了 IoC 容器的可定制性,它定义了设置类装载器、属性编辑器、容器初始化后置处理器等方法;

AutowireCapableBeanFactory 自动装配

(6). 定义了将容器中的 Bean 按某种规则(如按名字匹配、按类型匹配等)进行自动装配的方法;

SingletonBeanRegistry 运行期间注册单例 Bean

(7). 定义了允许在运行期间向容器注册单实例 Bean 的方法;对于单实例( singleton)的 Bean来说,BeanFactory 会缓存 Bean 实例,所以第二次使用 getBean() 获取 Bean 时将直接从IoC 容器的缓存中获取 Bean 实例。Spring 在 DefaultSingletonBeanRegistry 类中提供了一个用于缓存单实例 Bean 的缓存器,它是一个用 HashMap 实现的缓存器,单实例的 Bean 以beanName 为键保存在这个 HashMap 中。

依赖日志框框

(8). 在初始化 BeanFactory 时,必须为其提供一种日志框架,比如使用 Log4J, 即在类路径下提供 Log4J 配置文件,这样启动 Spring 容器才不会报错。

二.ApplicationContext 面向开发应用

ApplicationContext 由 BeanFactory 派生而来,提供了更多面向实际应用的功能。

ApplicationContext 继承了 HierarchicalBeanFactory 和 ListableBeanFactory 接口,在此基础上,还通过多个其他的接口扩展了 BeanFactory 的功能:

Spring IOC and IOC container implementation principle


1. ClassPathXmlApplicationContext:默认从类路径加载配置文件

2. FileSystemXmlApplicationContext:默认从文件系统中装载配置文件

3. ApplicationEventPublisher:让容器拥有发布应用上下文事件的功能,包括容器启动事件、关闭事件等。

4. MessageSource:为应用提供 i18n 国际化消息访问的功能;

5. ResourcePatternResolver : 所 有 ApplicationContext 实现类都实现了类似于PathMatchingResourcePatternResolver 的功能,可以通过带前缀的 Ant 风格的资源文件路径装载 Spring 的配置文件。

6. LifeCycle:该接口是 Spring 2.0 加入的,该接口提供了 start()和 stop()两个方法,主要用于控制异步处理过程。在具体使用时,该接口同时被 ApplicationContext 实现及具体Bean 实现, ApplicationContext 会将 start/stop 的信息传递给容器中所有实现了该接口的 Bean,以达到管理和控制 JMX、任务调度等目的。

7. ConfigurableApplicationContext extended to ApplicationContext, which adds two new major ways: refresh () and close (), let ApplicationContext has started, refresh, and the ability to close the application context. In the case of calls to close the application context refresh () to start the application context, it has been launched in the state, call the refresh () clears the cache and reload the configuration information, call the close () you can close the application context.

Three .WebApplication architecture

WebApplicationContext is designed for preparation of a Web application, which allows to load the configuration file from a Web root directory with respect to the path of the completion of initialization. WebApplicationContext ServletContext reference can be obtained from the entire Web application context object placed in the ServletContext as an attribute, to access the Web application environment may Spring application context.

Spring IOC and IOC container implementation principle


Guess you like

Origin blog.51cto.com/14599798/2449792