Spring IOC principle study notes

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.

Here Insert Picture Description

IOC container Introduction

pring 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.

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.

BeanFactory

BeanFactory architecture:
Here Insert Picture Description

BeanDefinitionRegistry: 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 interface class is located in the top of the tree, which is the most important method getBean (String beanName), which returns the name of a particular Bean from the container, BeanFactory functions obtained by expanding the other interface:

ListableBeanFactory: This interface defines a number of methods to access basic information Bean container, such as viewing the number Bean acquires a certain type configuration name Bean view comprises a vessel if the Bean or the like;

HierarchicalBeanFactory: Interface father and son cascade IoC containers, sub-containers can be accessed through the interface method of the parent container; HierarchicalBeanFactory through the interface, Spring IoC container vessel system can establish levels associated with father and son, the child can access the parent container vessel Bean, but the parent container Bean child can not access the container. Spring container using his son to achieve a lot of features, such as Spring MVC, the presentation layer is located in a sub-Bean container, while the business layer and persistence layer Bean located in the parent vessel. In this way, the presentation layer Bean Bean can reference the business layer and persistence layer, and the Bean business layer and persistence layer will not see the presentation layer of the Bean.

ConfigurableBeanFactory: is an important interface to enhance the customization IoC container, which defines the class loader is provided, the Attribute Editor, initialization container postprocessor or the like;

AutowireCapableBeanFactory: defines the container Bean (such as by name match, according to the type of matching, etc.) according to certain rules for automatic assembly methods;

SingletonBeanRegistry: Register defines the method allows the container to a single instance Bean during operation;

Spring loaded by Resource XmlBeanFactory configuration information and activate IoC container, and can obtain from the Bean IoC container by BeanFactory # getBean (beanName) method. BeanFactory through IoC container starts, will not initialize Bean defined in the configuration file, initializing operation occurs when the first call.

For single-instance (Singleton) for the Bean, the BeanFactory cached Bean instance, the second use of the getBean () acquired from the cache Bean instance IoC container getting Bean. Spring DefaultSingletonBeanRegistry provided a class for a single instance Bean cache buffer, it is implemented in a HashMap with buffer, in single instance Bean is a bond beanName stored in the HashMap.

It is worth mentioning that, during initialization the BeanFactory, must offer an logging framework, such as the use Log4J, Log4J configuration file providing classpath, such error will not start Spring container.

ApplicationContext

Derived from the ApplicationContext BeanFactory provides more features for practical application.

In BeanFactory, many functions need to be programmatically implemented, and can be realized by means arranged in the ApplicationContext.
Here Insert Picture Description
ApplicationContext inherited HierarchicalBeanFactory and ListableBeanFactory interfaces, on this basis, but also through several other interfaces extend the functionality of BeanFactory:

ClassPathXmlApplicationContext: The default configuration file is loaded from the classpath

FileSystemXmlApplicationContext: The default configuration is loaded from the file system

ApplicationEventPublisher:让容器拥有发布应用上下文事件的功能,包括容器启动事件、关闭事件等。实现了 ApplicationListener 事件监听接口的 Bean 可以接收到容器事件 , 并对事件进行响应处理 。 在 ApplicationContext 抽象实现类AbstractApplicationContext 中,我们可以发现存在一个 ApplicationEventMulticaster,它负责保存所有监听器,以便在容器产生上下文事件时通知这些事件监听者。

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

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

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

ConfigurableApplicationContext 扩展于 ApplicationContext,它新增加了两个主要的方法: refresh()和 close(),让 ApplicationContext 具有启动、刷新和关闭应用上下文的能力。在应用上下文关闭的情况下调用 refresh()即可启动应用上下文,在已经启动的状态下,调用 refresh()则清除缓存并重新装载配置信息,而调用close()则可关闭应用上下文。这些接口方法为容器的控制管理带来了便利,但作为开发者,我们并不需要过多关心这些方法。

使用:

如果配置文件放置在类路径下,用户可以优先使用 ClassPathXmlApplicationContext 实现类:

如果配置文件放置在文件系统的路径下,则可以优先考虑使用 FileSystemXmlApplicationContext 实现类:

Spring 3.0 支持基于类注解的配置方式,主要功能来自于 Spring 的一个名为 JavaConfig 子项目,目前 JavaConfig已经升级为 Spring核心框架的一部分。

ApplicationContext 在初始化应用上下文时就实例化所有单实例的 Bean。

WebApplicationContext

WebApplication体系架构:
Here Insert Picture Description

WebApplicationContext 是专门为 Web 应用准备的,它允许从相对于 Web 根目录的路径中装载配置文件完成初始化工作。从WebApplicationContext 中可以获得 ServletContext 的引用,整个 Web 应用上下文对象将作为属性放置到 ServletContext 中,以便 Web 应用环境可以访问 Spring 应用上下文。 WebApplicationContext 定义了一个常量ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,在上下文启动时, WebApplicationContext 实例即以此为键放置在 ServletContext 的属性列表中,因此我们可以直接通过以下语句从 Web 容器中获取WebApplicationContext:

总结

Spring IOC容器主要有继承体系底层的BeanFactory、高层的ApplicationContext和WebApplicationContext

Bean有自己的生命周期

容器启动原理:Spring应用的IOC容器通过tomcat的Servlet或Listener监听启动加载;Spring MVC的容器由DispatchServlet作为入口加载;Spring容器是Spring MVC容器的父容器

容器加载Bean原理:

BeanDefinitionReader读取Resource所指向的配置文件资源,然后解析配置文件。配置文件中每一个解析成一个BeanDefinition对象,并保存到BeanDefinitionRegistry中;

容器扫描BeanDefinitionRegistry中的BeanDefinition;调用InstantiationStrategy进行Bean实例化的工作;使用BeanWrapper完成Bean属性的设置工作;

Example Bean single buffer pool: Spring DefaultSingletonBeanRegistry class provides a cache for a single instance Bean buffer, which is implemented in a HashMap with buffer, in single instance Bean is a bond beanName stored in the HashMap.

Published 22 original articles · won praise 6 · views 9752

Guess you like

Origin blog.csdn.net/u012712556/article/details/104323185