Maven multi module에서 yml(yaml) 오버라이딩 설정 (spring boot 2.x)
@PropertySources({ @PropertySource("application-share.yml"), @PropertySource("application.yml") })
@PropertySource(value = { "classpath:/application.yml", "classpath:/application-share.yml" })
위 방식은 확장자가 properties의 경우, 사용 가능하다. yml(yaml)은 사용 불가능하다.
방법1. 메인이 되는 yml과 서브 yml를 가질 경우의 설정(서브 yml이 2개 이상이면 문제 발생하므로 비추천)
질문 :
I have a Spring boot application that is divided in several modules. The main module runs the application and has an application.properties file in the resources folder. I'm wondering if I can add another properties file in a submodule to separate properties that are belonging to that module and how to make this working (because it is not).
---------------------------------------------------
+main_module
+src
+main
+java
+my/package/Application.java
+resources/application.properties
+support_module
+src
+main
+java
+resources/application.properties
---------------------------------------------------
So, this is the current situation. Clearly the properties file in the module support_module is not read causing a NoSuchBeanDefinitionException, while if I put the content in the other properties file everything works fine.
해답 :
Spring Boot reads the property files in the following order. (from Spring Boot in Action)
- Externally, in a /config subdirectory of the directory from which the application is run
- Externally, in the directory from which the application is run
- Internally, in a package named “config”
- Internally, at the root of the classpath
So placing application.properties in a config sub-directory will give it a higher priority. In the following configuration, the application.properties from module_a will take precedence. You can add common defaults in application.properties and override them in individual modules by placing the configuration file in config/application.properties.
---------------------------------------------------
+common_module
+src
+main
+java
+resources/application.properties
+module_a
+src
+main
+java
+my/package/Application.java
+resources/config/application.properties
---------------------------------------------------
방법2. 빌드 시에 주입하는 방법(간단하게 구성가능)
1 2 3 4 5 6 7 8 9 10 11 12 13 | @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class) .properties(new StringBuilder().append("spring.config.location=") .append("classpath:/application-dev.yml").append(", classpath:/application.yml").toString()) .run(args); // SpringApplication.run(Application.class, args); } } | cs |
가장 편리하다.