๐ŸŒฑ ์Šคํ”„๋ง ๋ถ€ํŠธ Profile๋กœ ๋‹ค๋ฅธ ํ™˜๊ฒฝ ๊ตฌ์„ฑ


Profile?

  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ผ๋ถ€ configuration์„ ๋ถ„๋ฆฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ผ๋ถ€ configuration์„ ํŠน์ • ํ™˜๊ฒฝ์—์„œ๋งŒ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
  • ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ์„ค์ •๊ฐ’์„ ๋‹ฌ๋ฅด๊ฒŒ ๋กœ๋”ฉํ•  ๋•Œ

@Profile

  • @Component, @Configuration, @ConfigurationProperties์„ @Profile์™€ ํ•จ๊ป˜ ์„ ์–ธ

@Configuration
@Profile("production")
public class ProductionConfiguration {

    // ...

}
  • ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰์‹œ spring.profiles.active ํ”„๋กœํผํ‹ฐ๋กœ ํ”„๋กœํŒŒ์ผ์„ ์ง€์ •ํ•˜๋ฉด ํ•ด๋‹น ํ”„๋กœํŒŒ์ผ์ด ์ ์šฉ๋œ ๋นˆ๋งŒ ๋“ฑ๋ก๋จ

application-properties

spring.profiles.active=dev,hsqldb

Example

  1. application-properties์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜
    spring.profiles.active=prod
    
  2. @Profile ์–ด๋…ธํ…Œ์ด์…˜๊ณผ ํ•จ๊ป˜ @Configuration ๋“ฑ๋ก

image

image

  1. ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰์‹œ activeํ•œ prod์— ๋“ฑ๋ก๋œ hello ๋นˆ์ด ์ถœ๋ ฅ๋จ

image

@Profile๊ณผ @ActiveProfiles

  • ์„œ๋กœ ๋‹ค๋ฅธ ํ™˜๊ฒฝ์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ profile์„ ํ™œ์„ฑํ™”ํ•˜์—ฌ ํ•„์š”ํ•œ ๋นˆ๋งŒ ๋“ฑ๋กํ•˜๋„๋ก ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜
  • @Profile์€ SpringBootApplication์„ ์‹คํ–‰ํ•  ๋•Œ ์‚ฌ์šฉ
  • @ActiveProfiles์€ ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉ

application.properties

  • application-{profile}.properties ํ˜•์‹์œผ๋กœ ํŒŒ์ผ ์ƒ์„ฑ
  • ์ด๋Š” @ConfigurationProperties๋กœ ๊ฐ„์ฃผ๋˜๊ณ  ๋กœ๋“œ๋จ
    • *.properties , *.yml ํŒŒ์ผ์— ์žˆ๋Š” property๋ฅผ ์ž๋ฐ” ํด๋ž˜์Šค์— ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ(๋ฐ”์ธ๋”ฉ) ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜
  • application-{profile}.properties์€ application.properties ๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’๊ฒŒ๋จ
  • ๋นŒ๋“œํ•  ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช…๋ น์–ด์™€ ํ•จ๊ป˜ ํŠน์ • ํ™˜๊ฒฝ์„ค์ •์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Œ
    --spring.profiles.active=production
    
  • properties ํŒŒ์ผ์— spring.profiles.include๋ฅผ ํ†ตํ•ด ์ถ”๊ฐ€ํ•  ํ”„๋กœํŒŒ์ผ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ

Example

application-properties

spring.profiles.active=dev
profile-common.name = default_name_mazzi
defaultonly.name = defualt_name

application-prod.properties

profile-common.name = test_name_mazzi
testonly.name = test_name

application-dev.properties

profile-common.name = dev_name_mazzi
devonly.name = dev_name

AppRunner

@Component
public class AppRunner implements ApplicationRunner {
    @Autowired
    Environment env;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("=====================================================");
        System.out.println("spring.profiles.active : " + args.getOptionValues("spring.profiles.active"));
        System.out.println("Enviroment's Active Profile : " + Arrays.toString(env.getActiveProfiles()));
        System.out.println("defaultonly.name : " + env.getProperty("defaultonly.name"));
        System.out.println("testonly.name : " + env.getProperty("testonly.name"));
        System.out.println("devonly.name : " + env.getProperty("devonly.name"));
        System.out.println("profile-common.name : " + env.getProperty("profile-common.name"));
        System.out.println("=====================================================");
    }
}

์‹คํ–‰ ๊ฒฐ๊ณผ

image

  • profile-common.name๋Š” ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์ด ๊ณตํ†ต์œผ๋กœ ๊ฐ€์ง„ ํ”„๋กœํผํ‹ฐ
  • dev ํ”„๋กœํŒŒ์ผ์„ ํ™œ์„ฑํ™”
  • ๊ณตํ†ต ์†์„ฑ์ธ profile-common.name์€ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ dev ํ”„๋กœํŒŒ์ผ์˜ ๊ฒƒ์œผ๋กœ ์˜ค๋ฒ„๋ผ์ด๋“œ ๋จ

์ฐธ๊ณ ์ž๋ฃŒ