ApplicationContext이 상속하고 있는 것들 중 EnvironmentCapable을 알아본다.
1. 프로파일 
 - 빈들이 묶음
 - 운영에서는 이런 기능을 쓰겠다. 개발에서는 이런 기능쓰겠다.
    이처럼 각각의 상황에 따라 다른 빈을 사용하는경우를 위해 프로파일 이라는 기능이 생겼다.
 - ApplicationContext의 Environment라는 인터페이스를 통해 사용 
 - ApplicationContext가 상속하고 있는 EnvironmentCapable 이 Environment를 가지고 있어 이를 이용한다.
 - 프로파일 확인 하는 소스 아래 
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;
@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()));
        // default 파일 확인
        // 어떤 프로파일이든 기본으로 설정되는 파일
        System.out.println(Arrays.toString(environment.getDefaultProfiles()));
    }
}
1-1. Configuration 설정 방법 
 프로파일 test 일때 빈 생성 하는 Configuration 파일 생성 
package com.bpkim.demospring51;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("test") // 프로파일이 Test일 때만 실행 >> Test 일 때만 BookRepository 빈 등록
public class TestConfiguration {
    @Bean
    public BookRepository bookRepository(){
        return new TestBookRepository();
    }
}
- 테스트에 사용 빈 파일 
 어노테이션을 만들지 않아 스프링에서 자동으로 빈 설정안하도록  위에 TextConfiguration 에서만 빈 생성 
package com.bpkim.demospring51;
public interface BookRepository {
}
package com.bpkim.demospring51;
public class TestBookRepository implements BookRepository {
}- BookRepository 빈을 가져오게 하여 확인
package com.bpkim.demospring51;
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()));
        // default 파일 확인
        // 어떤 프로파일이든 기본으로 설정되는 파일
        System.out.println(Arrays.toString(environment.getDefaultProfiles()));
    }
}
- 실행결과

test 프로파일이 아니기 때문에 BookRepository를 찾을 수 없다. 
 
- text 프로파일을 설정하여 정상 작동하는지 확인 해보기 
 jvm 옵션에 프로파일 값을 설정 하여 실행 

- 정상 작동 확인

 1-2 컴포넌트 스캔으로 프로파일 지정 
 - 위에서 만든 TestConfiguration 삭제 후 Repository 에 어노테이션으로 등록 
package com.bpkim.demospring51;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Repository;
@Repository
@Profile("test")
public class TestBookRepository implements BookRepository {
}
 1-3. 프로파일 표현식
 - !(not), &(and), |(or)
 프로파일 prod가 아닐때 빈 생성 소스
package com.bpkim.demospring51;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Repository;
@Repository
@Profile("!prod")
public class TestBookRepository implements BookRepository {
}
'개발 > Spring' 카테고리의 다른 글
| IoC - MessageSource (0) | 2021.04.11 | 
|---|---|
| IoC - Environment - 프로퍼티 (0) | 2021.04.11 | 
| IoC - 빈의 스코프 (0) | 2021.04.06 | 
| IoC - Component, 컴포넌트 스캔 (0) | 2021.04.06 | 
| IoC - Autowire(오토와이어) (0) | 2021.04.04 | 
 
									
								 
									
								 
									
								