Profile 이란?
Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments. Any @Component or @Configuration can be marked with @Profile to limit when it is loaded
spring 공식 문서에서 설명하고 있는 profile이다. 스프링에서 사용하고 있는 설정을 분리할 수 있는 방법으로
@Profile을 통해 해당 코드를 특정 profile을 실행하는 경우에만 spring bean으로 등록될 수 있게 설정하는 것이다.
이를 이용해서 사용하는 Database 등을 나눠서 이용할 수 있다.
Profile 사용법
@Profile
@Repository
@Profile("prod")
public class MemoryVoucherRepository implements VoucherRepository{
}
- 원하는 component 혹은 configuration 대상에 @Profile 을 붙이고 사용할 profile 이름을 괄호 안에 적는다.
- 배열 형식으로 여러 개의 profile 상태에서도 사용 가능 @Profile({"dev", "prod"})
- 특정 profile을 제외하고 싶다면 @Profile("!dev") 등 느낌표를 이용해 부정문을 사용한다.
application.yml
spring:
profiles:
active: local
---
spring:
config:
activate:
on-profile: local
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/profile
username: root
password:
---
spring:
config:
activate:
on-profile: test
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/profile_test
username: root
password:
- '---'을 이용해 profile을 구분한다.
- profile 지정을 위해 spring.config.activate.on-profile로 지정한다.
- 사용하고자 하는 profile을 위해 spring.profiles.active로 지정한다.
실제로 나는 다음과 같이 Springboot + MySQL(Docker)로 개발할 때 동일한 DB를 로컬환경과 test환경으로 나눠서 사용하였다.
(테스트 환경에서 사용한 결과물을 로컬 환경에서 돌려볼 때 반영하고 싶지 않았기 때문이다.)
spring.profiles.active를 local로 설정해 준 것으로 local로 지정된 profile들이 돌아가도록 설정해서 서버를 돌릴 수 있다.
만약 test profile로 돌리고 싶다면 spring.profiles.active를 test로 바꿔주면 test로 서버를 실행할 수 있다.
Profile 사용법
1. 앞서 설명한 spring.config.activate.on-profile 설정을 application.yaml 파일에서 해준다.
2. CLI로 실행할 경우 java -jar app.jar --spring.profiles.active=prod로 실행하여 profile 정보를 인수로 넘겨준다.
3. Intellij를 사용할 경우 edit configuration에서 profile을 설정할 수 있다.

- java program arguments에 --spring.profiles.active=[profile_name]
- Acrive profile: [profile_name]
둘 중 하나를 사용하면 Intellij에서 profile을 적용할 수 있다.
@ActiveProfiles
@ActiveProfiles is a class-level annotation that is used to declare which bean definition profiles should be active when loading an ApplicationContext for an integration test.
@ActiveProfile은 spring 통합테스트에서 원하는 profile을을 가져올 수 있게 설정하는 것을 의미한다.
E.g. 다음과 같이 profile에 따라 반환되는 String 값을 다르게 설정
-> @Profile("dev") -> dev / @Profile("loc") -> loc 반환하게 설정
@Profile("prod")
@Component
public class Prod implements ReturnString{
@Override
public String returnResult() {
return "prod";
}
}
@SpringBootTest
@ActiveProfiles("prod")
public class ProfileTest {
static final Logger log = LoggerFactory.getLogger(ProfileTest.class);
@Autowired
ReturnString returnString;
@Test
@DisplayName("ActiveProfiles 선택")
void profileTest() {
log.info("result = {}", returnString.returnResult());
}
}

Profile을 입력하지 않은 기본 component
profile을 입력하지 않은 component에 대해 특정 프로파일을 가동한다는 전제 하에 작동되는지 확인
-> 기본적으로 프로파일을 기입하지 않은 정보에 대해서는 그대로 작동을 한다.
@Component
public class Default {
public String defaultComponent(){
return "default";
}
}
@SpringBootTest
@ActiveProfiles("prod")
public class ProfileTest {
static final Logger log = LoggerFactory.getLogger(ProfileTest.class);
@Autowired
Default aDefault;
@Test
@DisplayName("no profile component")
void noProfile(){
log.info("case without profile = {} ", aDefault.defaultComponent());
}
}

같은 원리로 yaml 파일에서도 profile 설정이 되어 있지 않은 부분에 설정한 값들은
어떤 profile이 작동하더라도 기본으로 들어가게 된다.
spring:
jpa:
show-sql: true
---
spring:
config:
activate:
on-profile: prod
---
spring:
config:
activate:
on-profile: dev
---
spring:
config:
activate:
on-profile: local
-> 가장 위에 jpa 관련 설정은 세 가지 프로파일에 모두 적용됨
* 참고
spring version에 따라 yaml file에 쓰는 권장 방식이 달라지므로 그에 맞춰 설정해 주는 것이 좋다.
spring version 2.4 이전 -> spring.profile : [profile name]
spring version 2.4 이후 -> spring.config.activate.on-profile : [profile name]
출처:
https://colabear754.tistory.com/112
https://wonyong-jang.github.io/spring/2022/08/11/Spring-Profile.html
[Spring] application profile 환경 별 설정 분리 - SW Developer
Spring Profile는 어플리케이션 설정을 특정 환경에서만 적용되게 하거나, 환경 별(local, develop, production 등)로 다르게 적용 할 때 사용 한다. Spring boot 2.4 버전이 릴리즈 되면서 application.properties, applic
wonyong-jang.github.io
[Spring Boot] 다중 Profile을 이용하여 환경에 따라 다른 설정 적용하기
목차 개요 프로젝트를 진행하다 보면 로컬 개발 환경, 테스트 서버, 운영 서버에 따라 DB나 서버 포트 등과 같이 환경에 따라 설정을 다르게 적용해야 할 때가 있다. 이 때, Spring Boot의 다중 Profile
colabear754.tistory.com
@ActiveProfiles :: Spring Framework
@ActiveProfiles is a class-level annotation that is used to declare which bean definition profiles should be active when loading an ApplicationContext for an integration test.
docs.spring.io
22. Profiles
Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments. Any @Component or @Configuration can be marked with @Profile to limit when it is loaded: In the normal Spring way, you ca
docs.spring.io