티스토리 뷰
/**
https://devlog-wjdrbs96.tistory.com/169?category=882236 그대로 복사했습니다.
*/
저번 글에서 IoC 컨테이너의 핵심적인 인터페이스 중에 하나가 ApplicationContext라고 정리했었다. ApplicationContext는 BeanFacotry의 기능만을 하는 것이 아니라 다른 여러 기능들을 가지고 있다.
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
예를들면 위와 같이 상속받는 것들이 많은데 이번 글에서는 EnvironmentCapable에 대해서 정리해보려 한다. EnvironmentCapable 인터페이스는 프로파일과 프로퍼티를 다루는 인터페이스인데, 먼저 프로파일 기능에 대해서 알아보자.
1. 프로파일(Profile)이란?
- 빈들의 묶음
- 예를들면 어떤 테스트 환경에서는 정해진 빈들을 사용하겠다 라는 의미(각각의 환경에서 다른 빈들을 사용 or 특정 환경에서 어떤 빈을 등록해야 하는 경우에 사용함)
테스트 환경에서는 A라는 빈을 사용하고, 배포 환경에서는 B라는 빈을 쓰고 싶다. B라는 빈은 모니터링 용도니까 테스트할 때는 필요가 없고 배포할 때만 등록이 되면 좋을때 지정해줄 수 있는것이 프로파일이다.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("test")
public class TestConfiguration {
@Bean
public BookRepository bookRepository() {
return new TestBookRepository();
}
}
이러한 Bean 설정 파일을 만들었다고 가정하자. (@Configuration 어노테이션을 사용하면 현재 파일이 빈 설정 파일이라는 것을 알려주는 의미인 것 같다) 위의 코드를 보면 @Profile("test")라는 것이 있는데 의미는 test라는 Profile로 애플리케이션을 실행하기 전까지는 Bean 설정이 적용되지 않는다.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@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()));
}
}
따라서 위와 같이 BookRepository를 Autowire를 이용해 의존성 주입을 하려했을 때 에러가 발생한다. (BookRepository가 Bean으로 등록되어 있지 않기 때문이다) 그러면 이번에 프로파일을 지정해보자.
현재 나는 인텔리제이를 사용하고 있는데 상단 실행버튼 왼쪽을 보면 DemoApplication 이라고 글씨가 써져있는 곳을 볼 수 있을 것이다. 클릭 후에 Edit Configurations를 누르면 위와 같은 창을 볼 수 있다. 거기서 VM options에다가
-Dspring.profiles.active="test"
를 작성해주면 된다. 그리고 코드를 실행하면 BookRepository도 Bean으로 등록되어 의존성 주입도 되는 것을 알 수 있다.
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Repository;
@Repository
@Profile("test")
public class TestBookRepository implements BookRepository {
// Profile
}
그리고 ComponentScan으로 등록되는 Bean에도 Profile을 지정할 수 있다는 점도 알아두자. 그리고 Profile의 표현식도 사용할 수 있는데 !(not), &(and), | (or) 이다.
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Repository;
@Repository
@Profile("!test & prod")
public class TestBookRepository implements BookRepository {
// Profile
}
굳이 예를들면 아래와 같다. 위와 같은 것이 있다정도만 가볍게 알아두길 바란다. 그리고 이번에는 두 번째 기능인 프로퍼티에 대해서 정리해보자.
# 인프런 강의 (백기선 / 스프링 프레임워크 핵심 기술)
IoC 컨테이너 1부: 스프링 IoC 컨테이너와 빈 https://joinwithyou.tistory.com/24
IoC 컨테이너 2부: ApplicationContext와 다양한 빈 설정 방법 https://joinwithyou.tistory.com/25
IoC 컨테이너 3부: @Autowire https://joinwithyou.tistory.com/26
IoC 컨테이너 4부: @Component와 컴포넌트 스캔 https://joinwithyou.tistory.com/27
IoC 컨테이너 5부: 빈의 스코프 https://joinwithyou.tistory.com/28
IoC 컨테이너 6부: Environment 1부. 프로파일 https://joinwithyou.tistory.com/29
IoC 컨테이너 6부: Environment 2부. 프로퍼티 https://joinwithyou.tistory.com/30
IoC 컨테이너 7부: MessageSource https://joinwithyou.tistory.com/31
IoC 컨테이너 8부: ApplicationEventPublisher https://joinwithyou.tistory.com/32
IoC 컨테이너 9부: ResourceLoader https://joinwithyou.tistory.com/33
# Reference
https://devlog-wjdrbs96.tistory.com/165?category=882236 (블로그)
https://www.inflearn.com/course/spring-framework_core (백기선 강의)
'Spring' 카테고리의 다른 글
IoC 컨테이너 7부: MessageSource (0) | 2021.06.06 |
---|---|
IoC 컨테이너 6부: Environment 2부. 프로퍼티 (0) | 2021.06.06 |
IoC 컨테이너 5부: 빈의 스코프 (0) | 2021.06.06 |
IoC 컨테이너 4부: @Component와 컴포넌트 스캔 (0) | 2021.06.06 |
IoC 컨테이너 3부: @Autowire (0) | 2021.06.06 |