• Spring 创建 IOC 容器时加载配置文件的几种方式


    一、ClassPathXmlApplicationContext 类路径加载

    1. 使用 classpath 路径,classpath 前缀加不加都可以。

    ApplicationContext act = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    // 使用ClassPath路径可以不加前缀
    ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");

    2. 使用磁盘绝对路径,必须要加前缀file。

    ApplicationContext act = new ClassPathXmlApplicationContext("file:F:/stud/src/main/webapp/WEB-INF/applicationContext.xml");

    3. 当有多个配置文件时

    ApplicationContext act = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","spring.xml"});

    4. 使用通配符

    ApplicationContext act = new ClassPathXmlApplicationContext("application-*.xml");

    二、FileSystemXmlApplicationContext 文件系统加载

    该类参数抓取规则与 ClassPathXmlApplicationContext 相比,多了一项:

    1. 使用项目的根目录

    ApplicationContext act = new FileSystemXmlApplicationContext("src/main/resources/applicationContext.xml");

    相同的两种抓取规则:

    2. 也可以使用classpath路径,但不同的是:必须有前缀

    ApplicationContext act = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");

    3. 使用磁盘的绝对路径,但不同的是:前缀可以不加

    ApplicationContext act = new FileSystemXmlApplicationContext("file:F:/stud/kaptcha/src/main/webapp/WEB-INF/applicationContext.xml");
    // 使用磁盘绝对路径可以不加前缀 ApplicationContext act
    = new FileSystemXmlApplicationContext("F:/stud/kaptcha/src/main/webapp/WEB-INF/applicationContext.xml");
  • 相关阅读:
    [NOI2009]管道取珠 DP + 递推
    poj3207 Ikki's Story IV
    NOIP2016Day1T2天天爱跑步(LCA+桶)
    NOIP2016Day2T3愤怒的小鸟(状压dp) O(2^n*n^2)再优化
    NOIP2016Day1T3换教室(floyd+期望dp)
    bzoj1854: [Scoi2010]游戏(匈牙利) / GDKOI Day2 T2(最大流)
    [CodeVs4927]线段树练习5
    基数排序的奇技淫巧
    bzoj2724: [Violet 6]蒲公英(离散化+分块)
    bzoj1483: [HNOI2009]梦幻布丁(链表+启发式合并)
  • 原文地址:https://www.cnblogs.com/libra0920/p/6165499.html
Copyright © 2020-2023  润新知