회사 프로젝트가 로컬에서는 외부 연동이 되지 않아 통합 테스트 시마다 외부연동 부분 제거를 위해 소스 수정을 했었다.
이러한 불편함을 개선하기 위해 @Profile 어노테이션과 application.yml을 통해 각각의 환경에서 다른 Bean을 사용하도록 설정해보았다.
- 예시는 사용법 정리를 위해 로직 없이 간략히 정리한 것으로 아래 환경에서 작성했다.
spring boot 2.5
gradle 7.2
// Default Profile: local
ext.profile = (!project.hasProperty('profile') || !profile) ? 'local' : profile
sourceSets {
main {
resources {
srcDirs "src/main/resources", "src/main/resources-${profile}"
}
}
}
tasks {
processResources {
duplicatesStrategy = org.gradle.api.file.DuplicatesStrategy.INCLUDE
}
}

# resources/apllication.yml
spring:
profiles:
active: local
jpa:
...
# resources-dev/apllication.yml
spring:
profiles:
active: dev
jpa:
...
/** controller */
@RestController
public class ProfileController {
private ProfileService profileService;
public ProfileController(ProfileService profileService) {
this.profileService = profileService;
}
@GetMapping("/api/test/profile")
public void profileTest() {
profileService.profile();
}
}
/** service */
public interface ProfileService {
void profile();
}
/** serviceImpl */
@Profile({"dev"})
@Slf4j
@Service
public class DevProfileService implements ProfileService {
@Override
public void profile() {
log.info("===================DevProfileService======================");
}
}
/** serviceImpl */
@Profile({"local"})
@Slf4j
@Service
public class LocalProfileService implements ProfileService {
@Override
public void profile() {
log.info("===================LocalProfileService======================");
}
}
./gradlew clean bootRun

./gradlew clean bootRun -Pprofile=dev
