spring怎样加载配置多个配置文件

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

spring怎样加载配置多个配置文件

  2021-04-02 我要评论

这篇文章主要介绍了spring如何加载配置多个配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

为应用指定多个配置文件:

多个配置文件的关系:

  • 并列
  • 包含

并列关系

即有多个配置文件,需要同时加载这多个配置文件;

可以使用可变参数,数组和统配符进行加载;

可变参数

String config1 = "com/abc/di08/spring-student.xml";
String config2 = "com/abc/di08/spring-school.xml";
//加载配置文件,生成spring容器对象(多个字符串参数加载多个配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(config1,config2);

数组加载

String config1 = "com/abc/di08/spring-student.xml";
String config2 = "com/abc/di08/spring-school.xml";
String[] configs = {config1,config2};
//加载配置文件,生成spring容器对象(数组加载多个配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(configs);

通配符加载

  String config = "com/abc/di08/spring-*.xml";
  //加载配置文件,生成spring容器对象(通配符加载多个配置文件)
  ApplicationContext ac = new ClassPathXmlApplicationContext(config);

包含关系

首先加载主配置文件,然后在主配置文件中使用通配符加载另外的配置文件。

String config = "com/abc/di09/applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
<!--import标签加载包含配置文件-->
<import resource="spring-*.xml"/>
您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们