[Spring] Profile ์„ค์ •ํ•˜๊ธฐ

์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ ๊ฐ stage๋งˆ๋‹ค profile ์„ค์ •์„ ์•Œ์•„๋ณด์ž

@Profile ์• ๋…ธํ…Œ์ด์…˜์€ ์–ด๋””์— ์„ค์ •?

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

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

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

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

2๊ฐœ์˜ Configurationํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ , hello ๋ผ๋Š” ๋นˆ์„ ๋“ฑ๋กํ–ˆ๋‹ค. ๊ฐ๊ฐ ํ…Œ์ŠคํŠธ์šฉ, ํ”„๋กœ๋•ํŠธ์šฉ ์œผ๋กœ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ return ํ•œ๋‹ค.

@Component
public class Runner implements ApplicationRunner {

    @Autowired
    private String hello;

    @Override
    public void run(ApplicationArguments args) {
        System.out.println(hello);
    }
}

hello ๋นˆ์„ ์ฃผ์ž…๋ฐ›์•„ ์ถœ๋ ฅํ•œ๋‹ค. ๋‹น์—ฐํžˆ ์–ด๋–ค profile๋กœ ๋„์šธ์ง€์— ๋Œ€ํ•œ ์„ค์ •์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

*************************
APPLICATION FAILED TO START
***************************

Description:

Field hello in com.example.demo.Runner required a bean of type 'java.lang.String' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'java.lang.String' in your configuration.

ํ”„๋กœํŒŒ์ผ ํ™œ์„ฑํ™”

  1. application.properties์˜ propertiy๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•
spring.profiles.active=prod

prod ํ™˜๊ฒฝ์œผ๋กœ springBoot๋ฅผ ๋„์šฐ๋ฉด hello prod๊ฐ€ ์ฐํžˆ๊ณ  test๋กœ ์‹คํ–‰ ์‹œํ‚ค๋ฉด, hello test๊ฐ€ ์ฝ˜์†”์ฐฝ์— ์ฐํžŒ๋‹ค.

hello prod
  1. ํŒจํ‚ค์ง•์„ ํ•˜๊ณ ๋‚œ jar ํŒŒ์ผ์„ ์‹คํ–‰ํ•  ๋•Œ arg๋กœ ๋„˜๊ฒจ์„œ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•
$ mvn clean package -DskipTests
$ java -jar target/*.jar --spring.profiles.active=prod

์ด ๊ฒฝ์šฐ์— application.properties์˜ spring.profiles.active=test๋กœ ํ•ด๋†“๊ณ  ํŒจํ‚ค์ง•์„ ํ•ด๋„, [Java] jar๋ฅผ ํ†ตํ•ด์„œarg๋กœ ๋„˜๊ธฐ๋Š” ๊ฒฝ์šฐ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์ด ๋˜์„œ, prod ํ”„๋กœํŒŒ์ผ์ด ์ ์šฉ๋จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

ํ”„๋กœํŒŒ์ผ์šฉ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ธฐ

application-{stage}.properties ํ˜•์‹์˜ ํ”„๋กœํŒŒ์ผ์šฉ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ”„๋กœํŒŒ์ผ Include

# application.properties ํŒŒ์ผ
spring.profile.include = realdb

์ด๋Ÿฐ ์‹์œผ๋กœ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด, application-realdb.properties ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๋Š” application.properties ํŒŒ์ผ์ด ๋งŒ๋“ค์–ด ์ง„๋‹ค.

Best Practice ๋Š” ๋ฌด์—‡?

์ด๋Ÿฐ์‹์œผ๋กœ propertiesํŒŒ์ผ์„ ๊ฐ ์Šคํ…Œ์ด์ง• ๋ณ„๋กœ ๋‚˜๋ˆ„๊ณ , ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” app์— ๋Œ€ํ•œ ์ •๋ณด๋‚˜, ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์—ฐ๋™ํ•˜๋Š” app-key๊ฐ™์€ ์ •๋ณด๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ๊ฐ stage๋ณ„๋กœ common์˜ properties ํŒŒ์ผ์„ includํ•˜์—ฌ์„œ ๊ณตํ†ต์ธ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ •๋ฆฌ

profile์€ ๊ฐ Stageํ™˜๊ฒฝ์— ๋”ฐ๋ผ ๊ตฌ์„ฑํ•ด์•ผ ํ•˜๋Š” ๋นˆ์„ค์ •, ์ •๋ณด๋“ค์ด ์ƒ์ดํ•˜๊ฒŒ ๋‹ค๋ฅธ ๊ฒฝ์šฐ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ ์Šคํ…Œ์ด์ง•ํ™˜๊ฒฝ์— ๋งž๋Š” ConfigurationํŒŒ์ผ์„ ๊ตฌ์„ฑ, profile์„ ์‹คํ–‰ํ•  ๋•Œ๋Š” spring.profiles.actives ์˜ต์…˜์„ ํ†ตํ•ด์„œ ์„ค์ •ํ–ˆ๋‹ค. ๊ฐ ์Šคํ…Œ์ด์ง• ๋ณ„ ์ •๋ณด๋“ค์ด ๋งŽ์„ ๋•Œ๋Š” ํ”„๋กœํŒŒ์ผ์šฉ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ๊ฐ ๋งŒ๋“ค์–ด์„œ ๋ถ„๋ฆฌํ–ˆ๋‹ค.

์—ฐ๊ด€ ํฌ์ŠคํŠธ