加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Spring FileSystemXmlApplicationContext类:指定配置文件的位置

发布时间:2020-12-15 02:38:45 所属栏目:Java 来源:网络整理
导读:FileSystemXmlApplicationContext 类位于 org.springframework.context.support 包中,它同样是 ApplicationContext 接口的实现类,与 ClassPathXmlApplicationContext 类的区别在于读取 Spring 配置文件的方式。它不再从类路径中获取配置文件,而是通过参数
FileSystemXmlApplicationContext 类位于 org.springframework.context.support 包中,它同样是 ApplicationContext 接口的实现类,与 ClassPathXmlApplicationContext 类的区别在于读取 Spring 配置文件的方式。它不再从类路径中获取配置文件,而是通过参数指定配置文件的位置,可以获取类路径之外的资源。

语法1:

FileSystemXmlApplicationContext(String configLocation)

参数说明:
  • configLocation:是 Spring 配置文件的位置和名称的字符串表现形式。

示例

使用指定的配置文件路径和名称创建 FileSystemXmlApplicationContext 类的容器对象,关键代码如下:
public static void main(String[] args){
  FileSystemXmlApplicationContext context = 
  new FileSystemXmlApplicationContext("d:appContext.xml");  //加载配置文件
}

语法2:

FileSystemXmlApplicationContext(String[]configLocations)

参数说明:
  • configLocations:是Spring配置文件的位置和名称的字符串表现形式。

示例

使用多个指定的配置文件的名称和路径创建一个字符串数组,然后使用该数组创建 FileSystemXmlApplicationContext 类的容器对象,关键代码如下:
public static void main(String[] args){
  String[] locations = {"appContext.xml","daoConfig.xml","viewConfig"};  //定义保存配置文件的字符串数组
  FileSystemXmlApplicationContext context = 
  new FileSystemXmlApplicationContext(locations);  //加载配置文件
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读