IoC 컨테이너와 빈(6) : Environment 1부. 프로파일

1. ApplicationContext extends EnvironmentCapable

  • getEnvironment()
  • 원하는 환경설정에 따라서 주입받는 빈이 달라지게끔 설정할 수 있다.
@Configuration
@Profile("test")
public class TestConfiguration {

    @Bean
    public BookRepository bookRepository(){
        return new TestBookRepository();
    }
}
  • 위의 코드는 test라는 프로파일로 이 애플리케이션을 실행하기 전 까지는 해당 빈설정은 적용되지 않는다.
  • BookRepository를 주입받지 못함.

  • 위의 사진처럼 Edit Configuration에서 프로파일설정 가능하다.

2. 프로파일

  • 빈들의 그룹
  • Environment의 역할은 활성화할 프로파일 확인 및 설정

3. 프로파일 유즈케이스

  • 테스트 환경에서는 A라는 빈을 사용하고, 배포 환경에서는 B라는 빈을 쓰고 싶다.
  • 이 빈은 모니터링 용도니까 테스트할 때는 필요가 없고 배포할 때만 등록이 되면 좋겠다.

4. 프로파일 정의하기

  • 클래스에 정의
    • @Configuration @Profile("test")
    • @Component @Profile("test")
  • 메소드에 정의
    • @Bean @Profile("test")

5. 프로파일 설정하기

  • -Dspring.Profiles.active="test,A,B,..." (Edit Configuration 메뉴 > VM options)
  • @ActiveProfiles(테스트용)

6. 프로파일 표현식

  • ! (not)
  • & (and)
  • | (or)

좋은 웹페이지 즐겨찾기