Spring/Spring Boot

[Spring Boot] 2-3. Core Features: Profile

noahkim_ 2023. 10. 9. 02:11

profile은 애플리케이션의 설정을 분리하여 특정 프로파일에 필요한 환경설정을 제공할 수 있습니다.

@Profile 어노테이션을 통해 해당 클래스가 어떤 profile에 적용될지 가리킬 수 있습니다.

  • 어떤 프로파일을 활성화할지 명시할 수 있습니다.
    • property : spring.profiles.active 
    • command line args : --spring.profiles.active 
  • 기본 프로파일을 명시할 수 있습니다.
    • property : spring.profiles.default
  • 프로파일 활성화 조건을 명시할 수 있습니다.
    • property : spring.config.activate.on-profile

 

1. Adding Active Profiles

  • spring.profiles.active 프로퍼티는 PropertySource 순서를 따릅니다.
  • 즉 command line args를 사용하면 오버라이딩 됩니다.

 

include property

  • 기존 활성 프로필 외에 추가 프로필을 함께 활성화 할 수 있습니다.
spring:
  profiles:
    active: dev
    include: common

 

항목 내용
목적
- 재사용성: 공통 설정을 분리
- 유연성 및 가독성: 설정 구성을 모듈화
제한사항
- profile-specific 문서에서는 사용할 수 없음
- spring.config.activate.on-profile에 의해 활성화된 문서에서도 사용 불가
대안
SpringApplication.setAdditionalProfiles() 메서드를 사용하여 프로그래밍 방식으로 추가 프로필 설정 가능

 

2. Profile Groups

  • 프로필 그룹 기능은 세분화된 프로필을 논리적 그룹으로 묶어서 관리할 수 있게 해줍니다.
spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq
  • --spring.profile.active=production을 사용하면 production, proddb, prodmq 프로필이 한번에 활성화됩니다.

 

3. Programmatically Setting Profiles

  • SpringApplication.setAdditionalProfiles(…) 메서드를 호출하여 프로그래밍 방식으로 활성 프로필을 설정할 수 있습니다.
SpringApplication app = new SpringApplication(MyApp.class);
app.setAdditionalProfiles("dev", "debug");
app.run(args);

 

  • ConfigurableEnvironment 인터페이스를 사용하여 프로필을 활성화할 수 있습니다.
environment.setActiveProfiles("dev");

 

4. Profile-specific Configuration Files

  • 활성 프로필에 따라 알맞은 설정 파일이 자동으로 설정됨
  • config data 및 @ConfigurationProperties와 연계 가능

 

 

 

참고