티스토리 뷰

Spring-boot

[Spring boot]yml configuration

개발자-김씨 2021. 4. 27. 23:05
반응형

spring boot 학습 & 세팅 2탄 - 2.3.9 RELEASE  docs.spring.io/spring-boot/docs/2.3.9.RELEASE/reference/html/

 

역시 기본적인 내용 정리는 .... 잘 되어있는 블로그 참고 ~

갓대희 블로그

코딩 덕후님 블로그

YAML 문법 위키 문서 : https://ko.wikipedia.org/wiki/YAML

YAML 문법 확인 : http://www.yamllint.com/

Spring boot 2.3.9 -Externalized Configuration

spring boot 2.3.9 - Common Application properties

 

 

 

1. application.yml 통합 버전

spring.profiles와 --- 구분자로 application.yml파일 하나에 local,dev, prod 모든 phase설정을 다 넣을 수 있다.

server:
  port : 8080
  undertow:
    threads:
      io: 5
      worker: 100

spring:
  profiles:
    active: local

  datasource:
    hikari:
      driver-class-name: com.mysql.cj.jdbc.Driver


# local
---
spring:
  profiles: local

  datasource:
    hikari:
      jdbc-url: #local - jdbc url
      username: #local - username
      password: #local - password
      minimum-idle: 10
      maximum-pool-size: 10

logging:
  level:
    root: debug
    org.kimcci.boot: debug


# dev
---
spring:
  profiles: dev

  datasource:
    hikari:
      jdbc-url: #dev - jdbc url
      username: #dev - username
      password: #dev - password
      minimum-idle: 10
      maximum-pool-size: 10

logging:
  level:
    root: info
    org.kimcci.boot: debug
   @Bean(destroyMethod = "close")
   @ConfigurationProperties(prefix = "spring.datasource.hikari")
   public HikariDataSource mysqlHikariCp() {
      return new HikariDataSource();
   }

 

 

2. @ConfigurationProperties 

@ConfigurationProperties와 @EnableConfigurationProperties 사용하여 프로퍼티 바인딩

server:
  port : 8080
  undertow:
    threads:
      io: 5
      worker: 100

spring:
  profiles:
    active: local

  datasource:
    hikari:
      driver-class-name: com.mysql.cj.jdbc.Driver
      
common:
  prop:
    num: 2
    managers:
      - kim   // -리스트 표현방식 1
      - lee
    users:
      [choi, park]   //리스트 표현방식 2
    blacks: 		//List<Map or bean> 매핑
      - name: kang
        value: 1
      - name : kong
        value: 2

# local
---
spring:
@ConfigurationProperties("common.prop")
@Data
public class CommonProp {
	
    Integer num;
    List<String> managers;
    List<String> users;
    List<BlackUser> blacks; //List<Map<String, Object>> blacks;

    @Data
    public static class BlackUser {
        private String name;
        private Integer value;
    }
}
단, setter메소드를 제공하고 변경 가능하도록 해야 합니다.

List<String> managers = new ArrayList<>();   --> 가능

final List<String> managers = new ArrayList<>();   -->  가능

final List<String> managers = null;  --> 불가

final Integer num = 1;   --> 불가

 

@SpringBootApplication
@EnableConfigurationProperties({CommonProp.class})
public class ApiApplication {

	public static void main(String[] args) {
		SpringApplication.run(ApiApplication.class, args);
	}

 

 

3. phase(profile)별로 yml파일 나누기

phase에 종속적이지 않은 프로퍼티는 application.yml에

phase에 종속적인 프로퍼티는 application-{phase}.yml에 

application.yml

server:
  port : 8080
  undertow:
    threads:
      io: 5
      worker: 100

spring:
  profiles:
    active: local

  datasource:
    hikari:
      driver-class-name: com.mysql.cj.jdbc.Driver

common:
  prop:
    managers:
      - kim
      - lee
    users:
      [choi, park]
    blacks:
      - name: kang
        value: 1
      - name : kong
        value: 2

 

application-dev.yml

spring:
  datasource:
    hikari:
      jdbc-url: #dev - jdbc url
      username: #dev - username
      password: #dev - password
      minimum-idle: 10
      maximum-pool-size: 10

logging:
  level:
    root: info
    org.kimcci.boot: debug
    
domains: <<<< phase별 속성
  naver-domain: http://dev-www.naver.com/
  kakao-domain: http://dev-www.kakao.com/

 

@ConfigurationProperties("domains")
@Data
public class DomainsProp {

    String naverDomain;
    String kakaoDomain;
}
@SpringBootApplication
@EnableConfigurationProperties({CommonProp.class, DomainsProp.class})
public class ApiApplication {

	public static void main(String[] args) {
		SpringApplication.run(ApiApplication.class, args);
	}
We recommend that you don’t mix profile-specific YAML files and multiple YAML documents. Stick to using only one of them.

application-local.yml, application-dve.yml로 분리하든가

application.yml에 profile + ---로 통합하든가

두 방식을 혼합해서는 쓰는걸 추천하지 않는다.

 

 

4. 혹시라도 프로퍼티가 너무 많아져 분리하고 싶다면

application-dev.yaml

spring:
  datasource:
    hikari:
      jdbc-url: #dev - jdbc url
      username: #dev - username
      password: #dev - password
      minimum-idle: 10
      maximum-pool-size: 10

logging:
  level:
    root: info
    org.kimcci.boot: debug

application-domain-dev.yaml (도메인url 속성만 모은 yml) 

domains:
  naver-domain: http://dev-www.naver.com/
  ....
  ....
  kakao-domain: http://dev-www.kakao.com/
  100개
--spring.profiles.active=dev, domain-dev

 

 

 

그 밖에 

spring:

  profiles: production & eu-central    

             production & (eu-central | eu-west)

docs.spring.io/spring-framework/docs/5.2.13.RELEASE/spring-framework-reference/core.html#beans-definition-profiles-java

반응형

'Spring-boot' 카테고리의 다른 글

[Spring boot]Redis - Lettuce 설정  (5) 2021.05.06
[Spring boot] 초기화 코드  (0) 2021.04.28
[Spring boot] Filter 설정  (2) 2021.04.28
[Spring boot] undertow  (0) 2021.04.23
댓글