spring-boot 配置文件加载顺序以及自定义配置文件名称

前言:

spring-boot配置文件默认使用application.properties或application.yml配置文件,以及指定了spring.profiles.active = dev,可以额外加载application-dev.properties,这些都是常识性的知识,那么,如果我想加载不是application.properties的文件作为配置文件,有什么方式呢?

一、自定义加载配置文件的方式

自定义加载配置文件的方式一共有3种

1、通过@PropertySource的方式

在启动类使用@PropertySource("test.properties")  ,注意点:如果想用多个启动类启动时各自加载不同配置文件,那么这个方式会加载没有启动的启动类的这个配置文件,另外如果有主配置文件存在(主配置文件即application.properties或profiles指定的配置文件),属性出现冲突时以主配置文件为准。

2、通过springApplication.setDefaultProperties(prop)的方式

这个方式和方式1的配置权重也是一样的,也会被主配置覆盖,设置方式见下面代码

		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、通过程序自变量的方式

自变量值:  --spring.config.name=test
设置方式:

    1) eclipse在main方法的运行或调试配置,找到第二个tab,程序自变量进行添加即可。

    2) 采用main方法的args数组设置

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

 SpringApplication.run(Application.class, args);

二、优先级顺序

1、优先级顺序测试

/**
 * 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、优先级顺序结论

程序自变量 > 主配置文件 (可通过--spring.config.name=test进行修改)  > 其他设置配置文件

 

猜你喜欢

转载自blog.csdn.net/shuixiou1/article/details/114377317
今日推荐