해당 게시물은 백기선님의 스프링 프레임워크 핵심 기술 강좌를 정리한 내용 입니다.
1. Environment
- Environment는 프로파일과 프로퍼티를 다루는 인터페이스이다.
- ApplicationContext는 EnvironmentCapable 인터페이스를 상속 받는다.
ApplicationContext extends EnvironmentCapable
그렇기 때문에 ApplicationContext의 getEnvironment()로 Environment 객체를 가져올 수 있다.
(EnvironmentCapable로 Environment를 다룰 수 있다.)
- Environment의 역할은 활성화 할 프로파일 확인 및 설정
2. 프로파일(Profile)
- 프로파일은 특정 실행 환경에서 사용할 빈들의 묶음을 말한다.
- 테스트 환경에서는 A라는 빈을 사용하고, 배포 환경에서는 B라는 빈을 사용하고 싶을 때 Profile을 이용할 수 있다.
3. 프로파일 확인하기
- Environment의 getActiveProfiles() : 현재 활성화한 프로파일을 가져온다.
" " getDefaultProfiles() : 기본적으로 적용되는 프로파일을 가져온다.
- getActiveProfiles()의 경우, Profile을 정의하지 않았기 때문에 []으로 출력 된다.
그리고 Profile이 정의 되지 않은 Bean들은 모두 Default Profile에 들어가게 된다.
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
ApplicationContext ctx;
@Override
public void run(ApplicationArguments args) throws Exception {
Environment environment = ctx.getEnvironment();
System.out.println(Arrays.toString(environment.getActiveProfiles()));
System.out.println(Arrays.toString(environment.getDefaultProfiles()));
}
}
4. 프로파일 정의하기
@Profile
- @Profile는 프로파일을 정의하며 클래스, 메서드에 사용 가능 하다.
① 클래스에 정의
@Configuration @Profile("test")
② 메서드에 정의
@Bean @Profile("test")
5. 프로파일 정의하기 - 예시
아래 코드에서 지정한 @Profile("test")은 해당 빈 설정 파일(TestConfiguration)이 test 프로파일 일 때만 적용된다는 것을 의미 한다.
[TestConfiguration.java]
@Configuration
@Profile("test")
public class TestConfiguration {
@Bean
public BookRepository bookRepository(){
return new TestBookRepository();
}
}
AppRunner 클래스에서는 앞서 정의한 빈 설정 파일(TestConfiguration.java)에서 등록한 빈을 주입 받는다.
아래의 코드를 실행하면 그 결과는 BookRepository 타입의 빈을 찾을 수 없다는 에러가 발생하게 된다.
그 이유는 test 프로파일로 설정하지 않고 애플리케이션을 실행 하였으므로 빈 설정 파일이 적용되지 않기 때문에
BookRepository 빈도 등록되지 않는다.
[AppRunner.java]
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
ApplicationContext ctx;
@Autowired
BookRepository bookRepository;
@Override
public void run(ApplicationArguments args) throws Exception {
Environment environment = ctx.getEnvironment();
System.out.println(Arrays.toString(environment.getActiveProfiles()));
System.out.println(Arrays.toString(environment.getDefaultProfiles()));
}
}
6. 프로파일 설정하기
① Active profiles 설정
- 인텔리제이 상단 메뉴에서 아래 그림처럼 [Edit Configurations]를 클릭 한다.
- Active profiles를 test로 지정한 다음 [Apply] – [OK]를 클릭 한다.
② VM Options 설정
- 앞서 살펴본 팝업 창에서 VM options를 아래와 같이 지정하는 방법도 가능하다.
-Dspring.profiles.active="test"
- 프로파일 설정을 완료하고 AppRunner를 다시 실행 하면 아래 그림처럼 getActiveProfiles()가 test로 출력되는 것을 확인 할 수 있다.
[실행 결과]
7. 프로파일 표현식
① ! : not
@Profile(“!prod”) // prod가 아닌 경우
② & : and
@Profile(“!prod & test”) // prod가 아니고 test인 경우
③ | : or
8. 프로파일 정의하기 [추가 내용]
- 앞서 살펴본 것과는 다르게 빈(Bean)에 바로 프로파일을 정의하는 방법에 대해 알아본다.
@Component @Profile(“test”)
@Repository
@Profile("test") // test에서 사용할 repository이다.
public class TestBookRepository implements BookRepository{
}
'Spring > Spring Core' 카테고리의 다른 글
[Section 1] IoC 컨테이너 7부: MessageSource (0) | 2020.09.06 |
---|---|
[Section 1] IoC 컨테이너 6부: Environment 2부. 프로퍼티 (0) | 2020.09.03 |
[Section 1] IoC 컨테이너 5부: 빈의 스코프(Scope) (0) | 2020.09.03 |
[Section 1] IoC 컨테이너 4부: @Component와 컴포넌트 스캔 (0) | 2020.09.03 |
[Section 1] IoC 컨테이너 3부: @Autowired (0) | 2020.09.03 |
댓글