Spring ClassPathXmlApplicationContext类:创建容器

  • 内容
  • 评论
  • 相关

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);  //加载配置文件
}

本文标题:Spring ClassPathXmlApplicationContext类:创建容器

本文地址:https://www.hosteonscn.com/6532.html

评论

0条评论

发表评论

邮箱地址不会被公开。 必填项已用*标注