본문 바로가기

개발/Spring Boot

[Spring Boot] Multi Profiles

반응형

다중 프로파일 적용 하는 방법

 

1.env file example

# application-one.yml
spring:
  profiles.include:
    - two  # 포함시킬 프로파일
    - three  # 포함시킬 프로파일

same:
  who: I am one # 중복되는 프로퍼티

diff: # 중복되지 않는 프로퍼티
  one: 1111

--------------------------------------------
# application-two.yml
same: # 중복되는 프로퍼티
  who: I am two

diff: # 중복되지 않는 프로퍼티
  two: 2222
# application-three.yml
same: # 중복되는 프로퍼티
  who: I am three

diff: # 중복되지 않는 프로퍼티
  three: 3333

2.Spring profile 적용Test 코드

package com.example.test.SpringBootApplication;

import org.springframework.beans.factory.annotation.Value;
import javax.annotation.PostConstruct;

@SpringBootApplication
public class SpringBootApplication {

  @Value("${same.who}")
  private String whoAmI;

  @Value("${diff.one}")
  private String one;

  @Value("${diff.two}")
  private String two;

  @Value("${diff.three}")
  private String three;

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

  @PostConstruct
  public void print() {
    System.out.println("Who are you :" + whoAmI);

    System.out.println("One properties - " + one);

    System.out.println("Two properties - " + two);

    System.out.println("Three properties - " + three);
  }
}

3. 실행

java -jar App.jar --spring.profiles.active=one,two

 

4.결과

 

반응형