[SpringBoot] 프로파일(Profile)

2022. 11. 1. 16:20JAVA/Spring

1. 프로파일(Profile)

프로파일은 실행 환경별(운영, 개발, 로컬 등)로 등록할 빈이 다르거나 프로퍼티 등을 다르게 해주어야 할 때 사용합니다.

 

@Profile 애노테이션 사용 위치

  • @Configuration
  • @Component

특정 프로파일 활성화 방법

  • spring.profiles.active

추가적인 프로파일 방법

  • spring.config.import

프로파일용 프로퍼티 형식

  • application-{profile}.yml

 

2. @Profile 애노테이션 실습

1. 설정 클래스 파일에 @Profile 애노테이션 적용

@Profile("prod")
@Configuration
public class BaseConfiguration {
    @Bean
    public String hello(){
        return "hello prod";
    }
}
@Profile("test")
@Configuration
public class TestConfiguration {
    @Bean
    public String hello(){
        return "hello test";
    }
}

 

2. 테스트코드에 활성화할 프로파일 설정

@SpringBootTest
@ActiveProfiles(profiles = {"test"})
public class TestProfilesTest {

 

3. 테스트코드 추가 및 실행 결과 확인

@SpringBootTest
@ActiveProfiles(profiles = {"test"})
public class TestProfilesTest {
    @Autowired
    private String hello;

    @Test
    public void testHello(){
        assertThat(hello).isEqualTo("hello test");
    }
}

 

3. 프로파일용 프로퍼티 실습

resources 디렉토리에 프로파일별로 설정할 프로퍼티들을 설정하는 것을 실습합니다.

 

1. main/resources/application-prod.yml 파일 작성

yonghwan:
  name: yonghwan prod

 

2. main/resources/application-test.yml 파일 작성

yonghwan:
  name: yonghwan test

 

3. 프로퍼티 클래스 정의

@ConfigurationProperties("yonghwan")
@Validated
@Component
@Getter
@Setter
public class YonghwanProperties {
    @NotEmpty
    private String name;
    private int age;
    private String fullName;

    @DurationUnit(ChronoUnit.SECONDS)
    private Duration sessionTimeout = Duration.ofSeconds(30);
}

 

4. 테스트코드 추가 및 실행 결과 확인

@SpringBootTest
@ActiveProfiles(profiles = {"test"})
public class TestProfilesTest {
    @Autowired
    private String hello;

    @Autowired
    private YonghwanProperties yonghwanProperties;

    @Test
    public void testHello(){
        assertThat(hello).isEqualTo("hello test");
    }
    
    @Test
    public void testProfileProperty(){
        assertThat(yonghwanProperties.getName()).isEqualTo("yonghwan test");
    }
}

 

4. 또 다른 프로파일 추가 (spring.config.import)

프로파일용 파일안에 또다른 프로파일 파일을 가져올 수 있습니다.

 

1. spring.config.import의 값을 application-proddb.yml로 설정하여 추가적인 프로파일을 추가합니다.

main/resources/application-prod.yml

yonghwan:
  name: yonghwan prod
spring:
  config:
    import: application-proddb.yml

main/resources/application-proddb.yml

yonghwan:
  db-name: h2

 

2. 프로퍼티 클래스를 수정합니다.

@ConfigurationProperties("yonghwan")
@Validated
@Component
@Getter
@Setter
public class YonghwanProperties {
    @NotEmpty
    private String name;
    private int age;
    private String fullName;
    private String dbName;

    @DurationUnit(ChronoUnit.SECONDS)
    private Duration sessionTimeout = Duration.ofSeconds(30);
}

 

3. 테스트 코드를 추가하고 실행 결과를 확인합니다.

@SpringBootTest
@ActiveProfiles(profiles = {"prod"})
public class ProdProfilesTest {

    @Autowired
    private String hello;
    
    @Autowired
    private YonghwanProperties yonghwanProperties;

    @Test
    public void testHello(){
        assertThat(hello).isEqualTo("hello prod");
    }
    
    @Test
    public void testGetDbName(){
        assertThat(yonghwanProperties.getDbName()).isEqualTo("h2");
    }
}

 

References

source code : https://github.com/yonghwankim-dev/springboot_study/tree/main/springboot_utilization/src/test/java/kr/yh/profiles
[인프런] 스프링 부트 개념과 활용