上一主旨(查看)中,了然了IOC容器的基本概念,以及BeanFactory的筹划与完成方式,那里就来精晓一下ApplicationContext方式的达成。

 

ApplicationContext

 

   
在Spring的参考文档中,为何要推荐应用ApplicationContext?它能给我们的应用带来哪些利益吗?作为BeanFactory的贯彻之一,它又是怎么着统筹的?在SpringMVC中运用的WebApplictionContext\XmlApplicationContext与之有什么关联?

    ApplicationContext与BeanFactory:

 XML 1

 

从类图中,可以显明的观望ApplicationContext是作为BeanFactory的子接口突显的。不过它又在BeanFactory的基本功上添加了新的性状:MessageSource(新闻援助,例如国际化),Application伊芙ntPublisher(事件辅助),ResourcePatternResolver(解析资源)。

 

 

AbstractApplicationContext

在ApplicationContext的贯彻类AbstractApplicationContext已经大约完毕了ApplicationContext的效应。

 

XML 2
XML 3

/**

 * Abstract implementation of the {@link org.springframework.context.ApplicationContext}

 * interface. Doesn't mandate the type of storage used for configuration; simply

 * implements common context functionality. Uses the Template Method design pattern,

 * requiring concrete subclasses to implement abstract methods.

 *

 * <p>In contrast to a plain BeanFactory, an ApplicationContext is supposed

 * to detect special beans defined in its internal bean factory:

 * Therefore, this class automatically registers

 * {@link org.springframework.beans.factory.config.BeanFactoryPostProcessor BeanFactoryPostProcessors},

 * {@link org.springframework.beans.factory.config.BeanPostProcessor BeanPostProcessors}

 * and {@link org.springframework.context.ApplicationListener ApplicationListeners}

 * which are defined as beans in the context.

 *

 * <p>A {@link org.springframework.context.MessageSource} may also be supplied

 * as a bean in the context, with the name "messageSource"; otherwise, message

 * resolution is delegated to the parent context. Furthermore, a multicaster

 * for application events can be supplied as "applicationEventMulticaster" bean

 * of type {@link org.springframework.context.event.ApplicationEventMulticaster}

 * in the context; otherwise, a default multicaster of type

 * {@link org.springframework.context.event.SimpleApplicationEventMulticaster} will be used.

 *

 * <p>Implements resource loading through extending

 * {@link org.springframework.core.io.DefaultResourceLoader}.

 * Consequently treats non-URL resource paths as class path resources

 * (supporting full class path resource names that include the package path,

 * e.g. "mypackage/myresource.dat"), unless the {@link #getResourceByPath}

 * method is overwritten in a subclass.

 */

View Code

 

地点的叙说的几乎意思是: 

1、那些类是对ApplicationContext的抽象的贯彻,并不会指定使用哪一种bean
definition
配置策略。它做到了各样ApplicationContext的通用的落成。在统筹那么些类时,使用了模版方法设计格局,有的艺术须要在切实的子类中落到实处。

2、与BeanFactory相比,ApplicationContxt中可以把BeanFactoryPostProcessor、BeanPostProcessor、ApplicationListener作为一个司空见惯的Bean定义。

3、在ApplicationContext中也足以选择MessageSource,使用它时用的name是”messageSource”。同时也得以应用Application伊芙ntMulticaster(name是”application伊芙ntMulticaster”),如果不点名,私自认同使用的是:SimpleApplication伊芙ntMulticaster。

4、通过持续DefaultResourceLoader格局完成了资源文件的加载。

 

接下去看看那么些类的一而再关系:

 XML 4

 

从类图中得以看出AbstractApplicationContext主要分为两支:  
·AbstractRefreshWebApplicationContext(紧要用于Web环境下)

    ·AbstractXmlApplicationContext(首要用于非Web环境下)

 

对此AbstractRefreshWebApplicationContext,从布置项目上分为两类:使用Annotation配置或然接纳XML文件配置。

 

对于AbstractXmlApplicationContext,选择的是XML作为Bean定义的布署文件,它的多少个子类的区分是从何地去加载bean定义件。

 下一节,就足以看看ApplicationContext是何等早先化IOC容器的。

 

相关文章

网站地图xml地图