반응형
Environment 프로파일과 프로퍼티를 다루는 인터페이스
ApplicationContext extends EnvironmentCapable
- getEnvironment()
Environment
- EnvironmentCapable
프로파일
- 빈들의 그룹
- Environment의 역할은 활성화할 프로파일 확인 및 설정
프로파일 유즈케이스
- 테스트 환경에서는 A라는 빈을 사용하고, 배포 환경에서는 B라는 빈을 쓰고 싶은 경우!
- 이 빈은 모니터링 용도니까 테스트할 때는 필요가 없고 배포할 때만 등록이 되면 좋겠다!
- 1. 각각의 환경에 따라.. 다른 빈들을 써야하는 경우!
- 2. 특정 환경에서만 어떤 빈을 등록하는 경우
프로파일 정의하기
- 클래스에 정의
- @Configuration @Profile("test")
- @Component @Profile("test")
- 메소드에 정의
- @Bean @Profile("test")
package com.example.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()));//[]
System.out.println(Arrays.toString(environment.getDefaultProfiles()));//[Default]
}
}
1-1. 클래스에 정의 @Configuration
@Configuration 이 붙은 클래스에 Profile을 test로 지정해주면,..
package com.example.demospring51;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("test") //test프로파일로 이 application을 실행하기 전까지는 이 빈설정파일이 적용이 안된다. test프로파일로 실행할때만 실행되는 빈 설정파일이 된다,.
public class TestConfiguration {
@Bean
public BookRepository bookRepository(){
return new TestBookRepository();
}
}
그냥 실행하면 에러가 나게 되는데 (왜? test라는 profile이 적용이 안됐기때문에 빈 설정파일이 안읽히고, 빈 주입이 실패하기때문에 오류가 발생한다.)
해결 방법 2가지..
프로파일 설정하기
- -Dspring.profiles.active="test,A,B....."
- @ActiveProfiles (테스트용)
1. Active profiles : test 로 적용 (community 버전에서는 안보일수가 있음)
2. VM options : -Dspring-.profiles.active="test" 를 적용
그리고 실행하면?
test 프로파일이 적용된것을 확인 할 수 있다.
- @Component @Profile
package com.example.demospring51;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Repository;
@Repository
@Profile("test") //@Repository에 적용을 해준다.
public interface BookRepository {
}
2. 메소드에 정의
- @Bean @Profile("test")
package com.example.demospring51;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
//@Profile("test") //test프로파일로 이 application을 실행하기 전까지는 이 빈설정파일이 적용이 안된다.
//test프로파일로 실행할때만 실행되는 빈 설정파일이 된다,.
public class TestConfiguration {
@Bean
@Profile("test") // **빈에 profile 적용
public BookRepository bookRepository(){
return new TestBookRepository();
}
}
프로파일 표현식
- ! (not)
- & (and)
- | (or)
@Profile("!test") //test가 아닌 경우에
@Profile("!test & !prod") //test, prod 둘 다 아닌 경우
@Profile("!test | !prod") //test 또는 prod 둘 중에 하나라도 아닌 경우
항상 가장 간단한것이 좋다. 내가 사용할때 가장 간단한 표현식을 사용하는게 좋은 코드이다.
반응형