zoukankan      html  css  js  c++  java
  • ApplicationContext容器的设计原理

    1.在ApplicationContext容器中,我们以常用的FileSystemXmlApplicationContext实现为例来说明ApplicationContext容器的设计原理

    2.在FileSystemXmlApplicationContext的设计中,我们看到ApplicationContext应用上下文的主要功能已经在FileSystemXmlApplicationContext的基类AbstractXmlApplication中实现了,在FileSystemXmlApplicationContext中,作为一个具体的应用上下文,只需要实现和它自身设计相关两个功能

    3.一个功能是,如果应用直接使用FileSystemXmlApplicationContext,对于实例化这个应用上下文支持同时启动IoC容器的refresh()过程。这在FileSystemXmlApplicationContext的代码实现中可以看到,代码如下:

     1 /**
     2      * Create a new FileSystemXmlApplicationContext with the given parent,
     3      * loading the definitions from the given XML files.
     4      * @param configLocations array of file paths
     5      * @param refresh whether to automatically refresh the context,
     6      * loading all bean definitions and creating all singletons.
     7      * Alternatively, call refresh manually after further configuring the context.
     8      * @param parent the parent context
     9      * @throws BeansException if context creation failed
    10      * @see #refresh()
    11      */
    12     public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
    13             throws BeansException {
    14 
    15         super(parent);
    16         setConfigLocations(configLocations);
    17         if (refresh) {
    18             refresh();
    19         }
    20     }
    FileSystemXmlApplicationContext(String[] configLocations,boolean,ApplicationContext)

    4.这个refresh()过程会牵涉IoC容器启动一系列复杂操作,同时,对于不同的容器实现,这些操作都是类似的,因此在基类中将它们封装好。所以,我们在FileSystemXmlApplicationContext的设计中看到的只是一个简单的调用。

    5.另一功能是与FileSystemXmlApplicationContext设计具体相关的功能,这部分与怎样从文件系统加载XML的Bean定义资源有关。

    6.通过这个实现,可以在文件系统中读取以XML形式存在的BeanDefinition做准备,因为不同的应用上下文实现对应着不同的读取BeanDefinition的方式,在FileSystemXmlApplicationContext中的实现代码如下:

     1 /**
     2      * Create a new FileSystemXmlApplicationContext with the given parent,
     3      * loading the definitions from the given XML files.
     4      * @param configLocations array of file paths
     5      * @param refresh whether to automatically refresh the context,
     6      * loading all bean definitions and creating all singletons.
     7      * Alternatively, call refresh manually after further configuring the context.
     8      * @param parent the parent context
     9      * @throws BeansException if context creation failed
    10      * @see #refresh()
    11      */
    12     public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
    13             throws BeansException {
    14 
    15         super(parent);
    16         setConfigLocations(configLocations);
    17         if (refresh) {
    18             refresh();
    19         }
    20     }
    getResourceByPath

     可以看到,调用这个方法,可以得到FileSystemResource的资源定位。

  • 相关阅读:
    CSS之旅——第二站 如何更深入的理解各种选择器
    CSS之旅——第一站 为什么要用CSS
    记录一些在用wcf的过程中走过的泥巴路 【第一篇】
    asp.net mvc 之旅—— 第二站 窥探Controller下的各种Result
    asp.net mvc 之旅—— 第一站 从简单的razor入手
    Sql Server之旅——终点站 nolock引发的三级事件的一些思考
    Sql Server之旅——第十四站 深入的探讨锁机制
    Sql Server之旅——第十三站 对锁的初步认识
    Sql Server之旅——第十二站 sqltext的参数化处理
    Sql Server之旅——第十一站 简单说说sqlserver的执行计划
  • 原文地址:https://www.cnblogs.com/duffy/p/3953712.html
Copyright © 2011-2022 走看看