[Spring Boot #8] 스프링 부트 프로파일(Profile)
- 📚 Spring/Spring Boot
- 2020. 4. 28. 12:16
| 스프링 부트 프로파일(Profile)
스프링부트에서는 프로파일(Profile) 을 통해 스프링 부트 애플리케이션의 런타임 환경을 관리할 수 있다. 예를 들어 어플리케이션 작동 시 테스트 환경에서 실행할 지 프로덕션 환경에서 실행할 지를 프로파일을 통해 관리 할 수 있다는 것이다.
다음은 프로덕션과 테스트 환경을 각각 외부 설정 파일을 통해서 관리하는 예이다.
중요한 부분은 spring.profiles.active 키를 통해 어떤 프로파일을 활성화 할 것 인지를 정하는 부분이다.
아래 코드에서 @Profile을 통해 프로파일 기능을 구현한 것을 볼 수 있는데 @Profile 에 인자로 들어가는 값은 프로파일이 현재 인자값과 일치할 시 아래 코드에서 명시한 스프링 빈을 등록하라는 뜻이다.
# application.properties
spring.profiles.active=prod
@Profile("prod")
@Configuration
public class BaseConfiguration {
@Bean
public String hello(){
return "hello";
}
}
@Profile("test")
@Configuration
public class TestConfiguration {
@Bean
public String hello(){
return "hello Test";
}
}
@Component
public class SampleRunner implements ApplicationRunner {
@Autowired
private String hello;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("===================");
System.out.println(hello);
System.out.println("===================");
}
}
@SpringBootApplication
public class SpringinitApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(SpringinitApplication.class);
app.setWebApplicationType(WebApplicationType.NONE);
app.run(args);
}
}
===================
hello
===================
| application-{프로파일}.properties 파일을 통한 프로파일 관리
스프링 부트에서는 application.properties를 통해 외부 설정값을 관리하는데 application-{프로파일}.properties 파일을 생성하여 관리하게 되면 application.properties보다 우선순위가 높게 외부 설정값이 관리되므로 편하게 관리할 수 있다.
'📚 Spring > Spring Boot' 카테고리의 다른 글
[Spring Boot #10] 스프링 부트 테스트 (Spring Boot Test) (0) | 2020.04.29 |
---|---|
[Spring Boot #9] 스프링 부트 로깅(Logging) (0) | 2020.04.28 |
[Spring Boot #7] 스프링 부트 외부 설정 (0) | 2020.04.27 |
[Spring Boot #6] 이벤트 리스터 (0) | 2020.04.26 |
[Spring Boot #5] 스프링부트 HTTP/2 적용 - undertow (0) | 2020.04.25 |