컴공생의 다이어리

[스프링 부트, Spring Boot] @Profile, @ActiveProfiles 본문

Development/Spring & SpringBoot

[스프링 부트, Spring Boot] @Profile, @ActiveProfiles

컴공 K 2024. 1. 15. 23:00

@Profile

빈이나 컴포넌트에 프로필을 구분하여 빈을 로드하고 싶은 경우 @Profile을 활용하면 된다. 아래와 같이 @Profile을 통해 프로필(profile)이 active될 때 어떤 빈 혹은 컴포넌트를 등록할지 결정할 수 있다.

@Configuration
@Profile("prod")
public class ProdConfig{
    @Bean
    public DataSource dataSource(){
    // ... 생략
    }
}


@Configuration
@Profile("dev")
public class DevConfig{
    @Bean
    public DataSource dataSource(){
    // ... 생략
    }
}

 

 

프로필 이름 앞에 NOT 연산자인 !를 접두사로 붙여 프로필에서 제외시킬 수 있다. 아래의 경우 prod 프로필을 제외한 프로필에 대해서만 활성화 된다.

@Configuration
@Profile("!prod")
public class ProdExceptConfig{
    @Bean
    public DataSource dataSource(){
    // ... 생략
    }
}

 

 

 

 

@ActiveProfiles

@ActiveProfiles를 사용해 테스트 수행시 사용할 프로필을 지정할 수 있다. 아래의 예시의 경우 local과 default 프로필만 적용되어 동작한다.

@SpringBootTest
@ActiveProfiles("local")
public class RepoTest {
    @Autowired
    Repo repo;
    
    // ... 생략
}

 

 

 

 

 

 

 

 

https://velog.io/@injoon2019/Profile-ActiveProfiles

 

@Profile, @ActiveProfiles

케이의 블로그를 참고해서 개발환경과 배포 환경을 분리해봤다. 하지만 이렇게 분리하고 나니 테스트가 거의 다 깨졌다. 처음에는 테스트 쪽에 application.yml을 생성하면 된다고 생각했는데 그렇

velog.io

https://memo-the-day.tistory.com/32

 

Spring 프로필(Profiles) 설정방법(예제)

1. 개요 이 튜토리얼에서는 Spring에 프로파일을 소개하는 데 초점을 맞출 것입니다. 프로필은 프레임 워크의 핵심 기능으로, 빈을 다른 프로필 ( 예 : dev , test , prod) 에 매핑 할 수 있습니다. 그런

memo-the-day.tistory.com

 

728x90
Comments