Spring Boot ‐ 외부 설정과 프로필 - dnwls16071/Backend_Study_TIL GitHub Wiki

📚 외부 설정

스크린샷 2025-02-04 오전 11 04 30
  • 외부 설정 방법
    • OS 환경변수
    • 자바 시스템 속성
    • 커맨드 라인 인수
    • 외부 파일(설정 데이터) - application.yml, application.properties

[ 외부 설정 - 설정 데이터✅ ]

  • 프로필별로 분리하면 개발 환경과 운영 환경을 구분할 수 있다.
스크린샷 2025-02-04 오전 11 10 26
# 작성 예시
spring.config.activate.on-profile=dev
url=dev.db.com
username=dev_user
password=dev_pw
#---
spring.config.activate.on-profile=prod
url=prod.db.com
username=prod_user
password=prod_pw

📚 @ConfigurationProperties

  • 타입 안전한 설정 속성이다.
    • 외부 설정의 파일을 읽어서 사용하는 방법
    • Getter/Setter 기반 생성자 - 생성자가 둘 이상인 경우 사용할 생성자에 @ConstructorBinding 어노테이션을 작성해준다.
    • 자바 빈 검증기(Java Bean Validation)
@Getter
@ConfigurationProperties("my.datasource")
public class MyDataSourcePropertiesV2 {
    private String url;
    private String username;
    private String password;
    private Etc etc;

    public MyDataSourcePropertiesV2(String url, String username, String password, @DefaultValue Etc etc) {
        this.url = url;
        this.username = username;
        this.password = password;
        this.etc = etc;
    }

    @Getter
    public static class Etc {
        private int maxConnection;
        private Duration timeout;
        private List<String> options;
    
        public Etc(int maxConnection, Duration timeout, @DefaultValue("DEFAULT") List<String> options) {
            this.maxConnection = maxConnection;
            this.timeout = timeout;
            this.options = options;
        }
    }
}

📚 YAML

  • YAML(YAML Ain't Markup Language)은 사람이 읽기 좋은 데이터 구조를 목표로 하며 확장자는 yaml, yml이다.
  • 스페이스를 사용한 계층 구조를 기본으로 한다.
environments:
  dev:
    url: "https://dev.example.com"
    name: "Developer Setup"
  prod:
    url: "https://another.example.com"
    name: "My Cool App"

📚 @Profile

  • 각 환경 별로 외부 설정 값을 분리하는 것을 넘어서 스프링 빈도 분리할 수 있다.
⚠️ **GitHub.com Fallback** ⚠️