欢迎光临
我们一直在努力

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);  //加载配置文件
}
赞(0) 打赏
未经允许不得转载:hosteons中文网 » Spring ClassPathXmlApplicationContext类:创建容器

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册