티스토리 뷰

 

| 스프링 부트 프로파일 (Spring Boot Profile)

 

스프링 부트에서는 프로파일(Profile)을 통해 스프링 부트 애플리케이션의 런타임 환경을 관리할 수 있습니다.

ex)어플리케이션 작동 시 테스트 환경에서 실행할 지 프로덕션 환경에서 실행할 지를 프로파일을 통해 관리할 수 있죠.

 

다음은 프로덕션과 테스트 환경을 각각 외부 설정 파일을 통해서 관리하는 예시입니다.

눈여겨 봐야할 것은 spring.profiles.active 키를 통해 어떤 프로파일을 활성화할 것인지를 정하는 부분입니다. 

 

또한 아래 코드에서 @Profile을 통해 프로파일 기능을 구현하는 것을 볼 수 있습니다. @Profile에 인자로 들어가는 값은 프로파일이 현재 인자값과 일치할 시 아래 코드에서 명시한 스프링 빈을 등록하라는 뜻입니다.

# application.properties
spring.profiles.active=prod

 

@Profile("prod")
@Configuration
public class BaseConfiguration {

    @Bean
    public String hello(){
        return "hello production";
    }
}

@Profile("test")
@Configuration
public class TestConfiguration {

    @Bean
    public String hello(){
        return "hello test";
    }
}

@Component
public class AppRunner 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 Application {

    public static void main(String[] args)  {
        SpringApplication application = new SpringApplication(Application.class);
        application.setWebApplicationType(WebApplicationType.NONE);
        application.run(args);
    }
}

//결과

=============
hello production
=============

 

 

| application-{프로파일}.properties 파일을 통한 프로파일 관리

 

스프링 부트에서는 application.properties를 통해 외부 설정값을 관리해왔습니다.

하지만 application-{프로파일}.properties 파일을 생성하여 관리하게 되면 application.properties 보다 우선순위가 높게 외부 설정값이 관리되므로 보다 편하게 프로파일을 관리할 수 있습니다. 

 

또한 properties 파일에 spring.profiles.include 키를 통해 어떤 설정파일을 해당 프로파일에 추가할 것인지 정할 수 있습니다.



@Component
@ConfigurationProperties("hi")
public class SaelobiProperties {

    private String name;

    private String fullName;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
}

 

@Component
public class AppRunner implements ApplicationRunner {

    @Autowired
    private String hello;

    @Autowired
    private HiProperties hiProperties;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("=============");
        System.out.println(hello);
        System.out.println(hiProperties.getName());
        System.out.println(hiProperties.getFullName());
        System.out.println("=============");
    }
}

 

# application.properties
spring.profiles.active=prod

# application-prod.properties 가 우선순위가 높으므로 
# 아래 값은 다른 값으로 오버라이딩 된다.

hi.name=power

 

# application-prod.properties
hi.name=hi prod

# 추가할 프로파일 설정
spring.profiles.include=hi2
# application-hi2.properties
hi.fullName=hong

 

 

 

 

결과

=============
hello production
hi prod
hong
=============