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

Spring ClassPathXmlApplicationContext类:创建容器

发布时间:2020-12-15 02:38:52 所属栏目:Java 来源:网络整理
导读:ClassPathXmlApplicationContext 类位于 org.springframework.context.support 包中,它是 ApplicationContext 接口的实现类之一,从当前类路径中检索配置文件并装载它来创建容器的示例。 语法1: ClassPathXmlApplicationContext(String configLocation) 参
ClassPathXmlApplicationContext 类位于 org.springframework.context.support 包中,它是 ApplicationContext 接口的实现类之一,从当前类路径中检索配置文件并装载它来创建容器的示例。

语法1:

ClassPathXmlApplicationContext(String configLocation)

参数说明:
  • configLocation:是 Spring 配置文件的路径和名称。

示例

使用指定的配置文件名创建一个 ClassPathXmlApplicationContext 类的容器对象。
ClassPathXmlApplicationContext context;
context = new ClassPathXmlApplicationContext("appContext.xml");

语法2:

ClassPathXmlApplicationContext(String[] configLocations)

参数说明:
  • configLocations:是一个字符串数组,其中包含了多个 Spring 配置文件的名称和位置。

示例

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

(编辑:李大同)

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

    推荐文章
      热点阅读