티스토리 뷰
이번에는 profile 설정에 대해 살펴보도록 하자, 사실 외부 설정과 이어지는 내용이기도 하다.
profile 설정은 특정한 profile에서만 특정 Bean을 등록한다거나, Application 동작을 특정 profile에서 Bean 설정을 다르게 하고 싶을 때 사용한다.
먼저 예시로 사용할 Class를 소개하겠다.
1. prod profile을 갖는 Configuration class
@Profile("prod")
@Configuration
public class BaseConfiguration {
@Bean
public String hello() {
return "Hello";
}
}
2. test profile을 갖는 Configuration class
@Profile("test")
@Configuration
public class TestConfiguration {
@Bean
public String hello() {
return "Hello test";
}
}
3. Application Runner
@Component
public class SampleRunner implements ApplicationRunner {
@Autowired
private String hello;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(hello);
}
}
application.properties에 어떤 profile을 주느냐에 따라 Bean으로 등록될 운명이 결정된다 !
profile을 설정하는 방법은 spring.profiles.active 의 value값을 주면 된다.
# application.properties
spring.profiles.active = prod
'''
<result>
Hello
'''
# application.properties
spring.profiles.active = test
'''
<result>
Hello test
'''
위 예시를 보면 바로 이해할 거라 믿어 의심치 않겠다!
profile용 properties 파일도 생성 가능한데 application-{profile}.properties 의 형식으로 파일을 생성하면 된다.
이렇게 만들어진 profile용 properties 파일이 기본적인 properties 보다 우선순위가 높기 때문에 기존의 값을 덮어쓴다.
가령, 위 ApplicationRunner에서 이번에는 properties의 chany.name을 출력한다고 했을 때, 아래와 같은 결과를 얻을 수 있다.
# application.properties
chany.name = chany
spring.profiles.active = prod
# application-prod.properties
chany.name = chany PROD
# application-test.properties
chany.name = chany TEST
'''
<result>
chany PROD
'''
# application.properties
chany.name = chany
spring.profiles.active = test
# application-prod.properties
chany.name = chany PROD
# application-test.properties
chany.name = chany TEST
'''
<result>
chany TEST
'''
마지막으로, spring.profiles.include를 사용하면 추가할 profile properties를 설정할 수 있다.
즉, 특정 profile일 때 사용할 profile 전용 properties 파일을 생성하는 것이다.
간단하게, 아래와 같이,
# application.properties
chany.name = chany
spring.profiles.active = prod
# application-prod.properties
chany.name = chany PROD
spring.profiles.include = include
# application-include.properties
chany.description = chany Description
prod profile을 사용하면 spring.profiles.include 의 value에 해당하는 값에 해당하는 application-{value}.properties를 해당 profile의 properties파일로 설정할 수 있다.
위 예시에는 chany.description 이라는 값을 사용할 수 있게 된다.
@ 출처
'프로그래밍 > Spring Boot' 카테고리의 다른 글
[Spring Boot] 8. 스프링부트 Test하기 (0) | 2021.03.20 |
---|---|
[Spring Boot] 7. Log 설정 (0) | 2021.03.10 |
[Spring Boot] 5. 외부 설정 (0) | 2021.03.07 |
[Spring Boot] 4. Spring Boot 특징 - 기본편 (0) | 2021.03.02 |
[Spring Boot] 3. 자동 설정 구현(2) (0) | 2021.03.02 |