Spring-boot 구성 파일로드 순서 및 사용자 지정 구성 파일 이름

머리말:

spring-boot 구성 파일은 기본적으로 application.properties 또는 application.yml 구성 파일을 사용하고 spring.profiles.active = dev를 지정합니다. 추가 application-dev.properties를로드 할 수 있습니다. 이것은 상식적인 지식입니다. want application.properties 이외의 파일을 구성 파일로로드하는 방법이 있습니까?

 

1. 구성 파일을로드하는 방법 사용자 지정

로드 구성 파일을 사용자 정의하는 세 가지 방법이 있습니다.

 

1. @PropertySource를 통해

시작 클래스에서 @PropertySource ( "test.properties")를 사용합니다. 참고 : 시작시 여러 시작 클래스를 사용하여 다른 구성 파일을로드하려는 경우이 메서드는 시작되지 않은 시작 클래스의 구성 파일을로드합니다. 기본 구성 파일이있는 경우 (기본 구성 파일은 application.properties 또는 프로필에 지정된 구성 파일 임) 속성 충돌시 기본 구성 파일이 우선합니다.

 

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의 주요 방법의 실행 또는 디버깅 구성에서 두 번째 탭을 찾아 프로그램 인수를 추가하십시오.

    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