spring加载多个配置文件如何配置

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

多个配置文件的关系:

  1. 并列
  2. 包含

并列关系

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

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

  1. 可变参数
String config1 = "com/abc/di08/spring-student.xml";
String config2 = "com/abc/di08/spring-school.xml";
//加载配置文件,生成spring容器对象(多个字符串参数加载多个配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(config1,config2);
  1. 数组加载
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);
  1. 通配符加载
    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"/>

猜你喜欢

转载自www.cnblogs.com/gbetter/p/11827252.html