How to spring load multiple configuration profiles

Multiple profiles for specific applications:

More relational configuration file:

  1. Juxtaposed
  2. contain

Constellation

That there are multiple configuration files, which need to load multiple configuration files at the same time;

The variable parameters can be used, and an array of wildcards loaded;

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

Inclusion relation

First, load the master configuration file, and then using wildcards load another profile in the main configuration file.

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

Guess you like

Origin www.cnblogs.com/gbetter/p/11827252.html