Spring-boot configuration file loading order and custom configuration file name

Foreword:

The spring-boot configuration file uses the application.properties or application.yml configuration file by default, and specifies spring.profiles.active = dev, you can load additional application-dev.properties, these are common sense knowledge, then, if I want Is there any way to load a file other than application.properties as a configuration file?

 

1. Customize the way to load the configuration file

There are 3 ways to customize loading configuration files

 

1. Via @PropertySource

Use @PropertySource("test.properties") in the startup class. Note: If you want to use multiple startup classes to load different configuration files at startup, then this method will load the configuration file of the startup class that is not started, and if there is The main configuration file exists (the main configuration file is the configuration file specified by application.properties or profiles), and the main configuration file shall prevail in case of property conflicts.

 

2. Through springApplication.setDefaultProperties(prop)

The configuration weight of this method and method 1 is also the same, and it will also be overwritten by the main configuration. For the setting method, see the following code

		newSpringApplicationSetDefProps(Application.class, "test.properties").run()
	}
	
	/**
	 * 自定义配置文件方式二:优先级小于主配置文件 (需要在主配置文件不存在使用)
	 */
	public static SpringApplication newSpringApplicationSetDefProps(Class<?> clazz, String path) {
		SpringApplication springApplication = new SpringApplication(clazz);
		Properties prop = new Properties();
		InputStream in = clazz.getClassLoader().getResourceAsStream(path);
		try {
			prop.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		springApplication.setDefaultProperties(prop);
		return springApplication;
	}

 

3. Through the method of program independent variables

Argument value : --spring.config.name=test
Setting method:

    1) In the running or debugging configuration of the main method of eclipse, find the second tab and add the program arguments.

    2) Use the main method to set the args array

 args = new String[] {"--spring.config.name=test"};

 SpringApplication.run(Application.class, args);

 

Second, the order of priority

1. Priority test

/**
 * spring-boot资源文件加载方式和优先级等测试demo
 * 
 * 			ShowController代码
 * 
 *			 @Autowired private User user; // 通过@ConfigurationProperties加载配置属性
 * 
 * 			@Value("${server.port}") // 通过@value加载配置属性 private int port;
 * 
 * 			@GetMapping("/show") public String show() {
 *            System.out.println(user.getName());
 *            System.out.println(user.getTitle()); System.out.println(port);
 *            return "结果"; 
 *          }
 *
 *            1、测试方案1: 主资源文件端口设置server.port=8088, 附属资源文件也设置端口server.port=8090, 运行时查看启动端口值 
 *            结论: 主资源文件端口配置会覆盖掉附属增加的资源文件配置。
 * 
 *            2、测试方案2:去掉PropertySource,采用properties加载资源文件并且设置到SpringApplication,进行运行。
 *            结论:和测试方案1一致。 通过setDefaultProperties端口会被主配置覆盖。
 * 
 *            3、测试方案3:删除掉默认的application.properties,只从properties或@PropertySource进行加载资源文件 
 *            结论:端口设置会按照指定的properties文件加载
 *            
 *            ------1-3总结: 配置文件分为主配置文件,和附属配置文件,主配置文件优先级高于附属配置,另外setDefaultProperties加载的也是附属的配置(会被主配置覆盖)。
 *
 * 			  4、测试方案4:设置--spring.config.name=test2的程序自变量,并且改回默认的application.proerties文件。
 * 			   结论:设置--spring-config.name=test2的程序自变量,会重新设定主配置文件的名称为test2.properties的文件
 * 
 * 			 另外通过--server.port=9090程序自变量方式设置的配置,优先级高于配置文件。
 * 			
 */
@SpringBootApplication
// @PropertySource("test.properties")
public class Application {

	/**
	 * --spring.config.name=test2  指定配置文件的自变量 (可替换主配置文件名)
	 * --server.port=9090  指定启动端口的自变量 (优先级高于配置文件)
	 */
	public static void main(String[] args) {
		
		for (String string : args) {
			System.out.println(string);
		}
		
		SpringApplication.run(Application.class, args);
		 
//		runSetDefaultProperties("test.properties", args);
	}

	public static void runSetDefaultProperties(String path, String[] args) {
		Properties props = new Properties();
		InputStream inputStream = Application.class.getClassLoader().getResourceAsStream(path);
		try {
			props.load(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
		SpringApplication app = new SpringApplication(Application.class);
		app.setDefaultProperties(props);
		app.run(args);
	}

}

 

2. Conclusion of priority order

Program arguments> main configuration file (can be modified by --spring.config.name=test)> other settings configuration files

 

 

 

 

Guess you like

Origin blog.csdn.net/shuixiou1/article/details/114377317