【spring】FileSystemXmlApplicationContext 类学习

上一篇我们一起学习了

FileSystemXmlApplicationContext介绍AnnotationConfigApplicationContext 。

【spring】AnnotationConfigApplicationContext 类学习_annotationconfigapplicationcontext方法-CSDN博客

FileSystemXmlApplicationContext是一个用于加载和刷新基于XML配置文件的应用程序上下文的类。 FileSystemXmlApplicationContext类继承自AbstractApplicationContext,实现了ApplicationContext接口。它提供了一种在文件系统上查找和加载XML配置文件的方式,这使得开发者可以轻松地管理和更新应用程序的配置。 使用FileSystemXmlApplicationContext的步骤通常如下:

  1. 创建一个配置文件路径的列表,这些路径指向应用程序的XML配置文件。
  2. 创建FileSystemXmlApplicationContext实例,将配置文件路径列表作为构造函数参数传入。
  3. 调用refresh()方法来启动Spring容器,加载和处理配置文件,初始化和注册bean。
  4. 通过调用getBean()方法从应用程序上下文中获取bean,并使用它们构建应用程序。

源码截图

构造函数源代码

    /**
     * 单配置文件加载
     * configLocation 包含了 BeanDefinition 所在的文件路径
     *
     * @param configLocation
     * @throws BeansException
     */
    public FileSystemXmlApplicationContext (String configLocation) throws BeansException {
        this (new String[]{configLocation}, true, null);
    }

    /**
     * 多配置文件加载
     * 可以定义多个 BeanDefinition 所在的文件路径
     *
     * @param configLocations
     * @throws BeansException
     */
    public FileSystemXmlApplicationContext (String... configLocations) throws BeansException {
        this (configLocations, true, null);
    }

    /**
     * 类路径和文件系统混合加载
     * 在定义多个 BeanDefinition 所在的文件路径 的同时,还能指定自己的双亲 IoC 容器
     *
     * @param configLocations
     * @param parent
     * @throws BeansException
     */
    public FileSystemXmlApplicationContext (String[] configLocations, ApplicationContext parent) throws BeansException {
        this (configLocations, true, parent);
    }

    public FileSystemXmlApplicationContext (String[] configLocations, boolean refresh) throws BeansException {
        this (configLocations, refresh, null);
    }

    /**
     * 如果应用直接使用 FileSystemXmlApplicationContext 进行实例化,则都会进到这个构造方法中来
     */
    public FileSystemXmlApplicationContext (String[] configLocations, boolean refresh, ApplicationContext parent)
            throws BeansException {

        // 动态地确定用哪个加载器去加载我们的配置文件
        super (parent);
        // 告诉读取器 配置文件放在哪里,该方法继承于爷类 AbstractRefreshableConfigApplicationContext
        setConfigLocations (configLocations);
        if (refresh) {
            // 容器初始化
            refresh ();
        }
    }


    /**
     * 实例化一个 FileSystemResource 并返回,
     * 本方法是在其父类 DefaultResourceLoader的getResource 方法中被调用的,
     */
    @Override
    protected Resource getResourceByPath (String path) {
        if (path != null && path.startsWith ("/")) {
            path = path.substring (1);
        }
        return new FileSystemResource (path);
    }
 FileSystemXmlApplicationContext (String configLocation)

参数:configLocation(String类型),这是XML配置文件的路径。它可以根据提供的路径加载单个或多个XML配置文件。如果路径包含逗号分隔的字符串,那么它将加载所有指定的配置文件。

示例代码:
ApplicationContext context = new FileSystemXmlApplicationContext("path/spring-config.xml");
FileSystemXmlApplicationContext (String... configLocations)

参数:configLocations(String数组类型),这是一个包含多个XML配置文件路径的数组。它会加载数组中指定的所有配置文件。

示例代码:
ApplicationContext context = new FileSystemXmlApplicationContext(new String[] {"path/spring-config1.xml", "path/spring-config2.xml"});
 FileSystemXmlApplicationContext (String[] configLocations, ApplicationContext parent) 

这个构造函数除了接受一组配置文件位置外,还接受一个父级ApplicationContext对象。这样可以创建一个具有父子上下文关系的层次结构,子上下文可以从父上下文中继承配置和共享Bean。

 FileSystemXmlApplicationContext (String[] configLocations, boolean refresh) 

它允许开发者指定一个配置文件路径数组和一个刷新标志来创建一个应用程序上下文实例。

  • String[] configLocations: 这是一个字符串数组,包含了要加载的Spring配置文件的路径。这些路径可以是文件系统中的绝对路径或相对路径,也可以是URL。数组中的每个元素都是一个有效的文件路径,上下文将会尝试加载这些路径指向的配置文件。
  • boolean refresh: 这是一个布尔值,指示是否在创建上下文实例后立即刷新上下文。如果设置为true,那么在构造函数执行完毕后,上下文会自动调用refresh()方法,这将触发上下文的初始化过程,包括加载配置文件、解析Bean定义、注册Bean以及执行任何初始化相关的回调。
示例代码:

    public static void main(String[] args) {
        try {
            // 定义Spring配置文件的路径数组
            String[] configLocations = {
                "file:/path/spring-config1.xml",
                "file:/path/spring-config2.xml"
            };
            
            // 创建FileSystemXmlApplicationContext实例
            // 这里设置refresh为true,表示在创建上下文后立即刷新上下文
            ApplicationContext context = new FileSystemXmlApplicationContext(configLocations, true);
            
            // 从上下文中获取Bean实例
            DemoBean demoBean= (DemoBean) context.getBean("demoBean");
            
            // 使用获取到的demoBean
            demoBean.test();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

FileSystemXmlApplicationContext (String[] configLocations, boolean refresh, ApplicationContext parent)

这个构造函数提供了最大的灵活性。它允许你指定是否在创建上下文后立即刷新上下文(这将加载所有的bean定义并启动上下文的生命周期),以及是否指定一个父上下文。

FileSystemXmlApplicationContext通过调用其父类AbstractApplicationContext的 refresh() 函数启动整个IOC容器对Bean定义的载入过程,下一篇我们好好聊聊。Spring IOC容器对Bean定义资源的载入是从refresh()函数开始的,refresh()是一个模板方法,refresh()方法的作用是:
在创建IOC容器前,如果已经有容器存在,则需要把已有的容器销毁和关闭,以保证在refresh之后使用的是新建立起来的IOC容器。
refresh的作用类似于对IOC容器的重启,在新建立好的容器中对容器进行初始化,对Bean定义资源进行载入。




相关推荐

  1. Python 学习

    2024-03-29 07:42:03       17 阅读
  2. DB学习

    2024-03-29 07:42:03       12 阅读
  3. [python学习]--

    2024-03-29 07:42:03       6 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-29 07:42:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-29 07:42:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-29 07:42:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-29 07:42:03       20 阅读

热门阅读

  1. admin端

    admin端

    2024-03-29 07:42:03      20 阅读
  2. 滑动模块-力扣-下一个排列

    2024-03-29 07:42:03       22 阅读
  3. 汇编开关atapi设备

    2024-03-29 07:42:03       16 阅读
  4. 实时数据库测试-汇编小程序

    2024-03-29 07:42:03       18 阅读
  5. openEuler 22.03 安装 .NET 8.0

    2024-03-29 07:42:03       22 阅读
  6. kotlin使用validation注解无效

    2024-03-29 07:42:03       21 阅读
  7. python中对象赋值、引用与拷贝的理解与应用

    2024-03-29 07:42:03       18 阅读
  8. 护网行动HW中的蓝队和红队的区别和关系?

    2024-03-29 07:42:03       20 阅读
  9. YTU 3379

    2024-03-29 07:42:03       16 阅读
  10. 在Vue中创建生产和开发环境

    2024-03-29 07:42:03       22 阅读