๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Springboot

@Profile

 

Spring Framework์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

ํŠน์ •ํ•œ ํ”„๋กœ ํŒŒ์ผ์—์„œ๋งŒ ํŠน์ •ํ•œ ๋นˆ์„ ๋“ฑ๋กํ•˜๊ณ  ์‹ถ๋‹ค๊ฑฐ๋‚˜

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋™์ž‘์„ ํŠน์ •ํ•œ ํ”„๋กœํŒŒ์ผ์—์„œ ๋นˆ ์„ค์ •์„ ๋‹ค๋ฅด๊ฒŒ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์“ด๋‹ค.

 

@Profile

@Profile("prod") // ์„ค์ •ํŒŒ์ผ ์ž์ฒด๊ฐ€ "prod"๋ผ๋Š” ํ”„๋กœํŒŒ์ผ์ผ ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๋‹น์—ฐํžˆ hello ๋นˆ๋„ ๋ชป์“ด๋‹ค.
@Configuration
public class BaseConfiguration {

    @Bean
    public String hello(){
        return "hello";
    }
}
@Profile("test") // ์„ค์ •ํŒŒ์ผ ์ž์ฒด๊ฐ€ "test"๋ผ๋Š” ํ”„๋กœํŒŒ์ผ์ผ ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.
@Configuration
public class TestConfiguration {

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

 

spring.profiles.active

์–ด๋–ค ํ”„๋กœํŒŒ์ผ์„ ํ™œ์„ฑํ™”ํ•  ๊ฒƒ์ธ์ง€ ์ง€์ •ํ•œ๋‹ค.

[ application.properties ]

spring.profiles.active=test
jueun.name=JUEUN

 

 

application-{profile}.properties

ํ”„๋กœํŒŒ์ผ์šฉ ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์˜ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ธฐ๋ณธ์ ์ธ application.properties๋ณด๋‹ค ๋†’๋‹ค.
์˜ค๋ฒ„๋ผ์ด๋”ฉํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

์˜ˆ์ œ 1

[ application-test.properties ]

jueun.name=jueun test

JUEUN ์ด ์•„๋‹Œ jueun test๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

application.properties์—์„œ ์ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰์‹œ ์‚ฌ์šฉํ•  ํ”„๋กœํŒŒ์ผ์„ spring.profiles.active=test๋กœ ์„ค์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— @Profile("test") ์„ค์ •์ด ์ ์šฉ๋˜๊ณ  @ConfigurationProperties("jueun") ์— ํ•ด๋‹น ์„ค์ •์˜ ๋นˆ์ด ์ฃผ์ž…๋˜์–ด jueun test๊ฐ€ ์ถœ๋ ฅ๋œ ๊ฒƒ์ด๋‹ค.

์˜ˆ์ œ 2

[ application-prod.properties ]

jueun.name=jueun prod

program arguments ์— --spring.profiles.active=prod ์ž…๋ ฅํ•˜์—ฌ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜

ํ„ฐ๋ฏธ๋„์—์„œ ๋นŒ๋“œ ํ›„ java -jar target/demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod ์‹คํ–‰ํ•˜๋ฉด

jueun prod๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.