Profile
์๋น์ค ํ๊ฒฝ์ ๋ฐ๋ผ ์๋ก ๋ค๋ฅธ ํ๊ฒฝ ๋ณ์๋ฅผ ์ฌ์ฉํ๊ณ ์ ํ ๋(dev, prod) ์ฌ์ฉํ๋ ๊ธฐ๋ฅ์๋ Profile๊ณผ Property๊ฐ ์๋ค.
์ ์
์๋น์ค ํ๊ฒฝ์ ๋ฐ๋ผ ์๋ก ๋ค๋ฅธ ํ๊ฒฝ ๋ณ์๋ฅผ ์ฌ์ฉํด์ผ ํ ์๋ ์๋ค. ์๋ฅผ ๋ค์ด, ๊ฐ๋ฐ ์์ ๋ฐฐํฌ ์ ์๋ก ๋ค๋ฅธ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์๋ฒ๋ฅผ ์ฌ์ฉํ๊ณ ์ ํ๋ค๋ฉด ๊ฐ๊ฐ ์๋ก ๋ค๋ฅธ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ์ ๋ณด๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
์ด๋ฅผ ํ๊ฒฝ๋ง๋ค ์๋์ผ๋ก ๋ณ๊ฒฝํ๋ ๊ฒ์ ์ค์๋ฅผ ์ ๋ฐํ๊ธฐ ์ฝ๊ธฐ ๋๋ฌธ์, Spring์์๋ Profile
์ด๋ผ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ์ฌ ์ฝ๊ฒ ์ค์ ์ ๋ณด๋ฅผ ๊ตฌ๋ถํ ์ ์๋๋ก ํ๋ค.
Profile ๊ธฐ๋ฅ์ ์ด์ฉํ๋ฉด, ๋ณ๋์ ์ค์ (Configuration)์ ์ด์ฉํ์ฌ ์คํ๋ง ์ปจํ ์ด๋๋ฅผ ์ด๊ธฐํํ ์ ์๋ค.
์ฌ์ฉ๋ฒ
๋ค์๊ณผ ๊ฐ์ด @Profile ์ด๋ ธํ ์ด์ ์ ํตํด ํ๊ฒฝ ๋ณ์๋ฅผ ๊ตฌ๋ถํ๋ค.
@Configuration
@Profile("dev")
public class DsDevConfig {
... ๊ฐ๋ฐ ํ๊ฒฝ์์ ํ์ํ ํ๊ฒฝ ๋ณ์๋ค
}
@Configuration
@Profile("prod")
public class DsProdConfig {
... ๋ฐฐํฌ ํ๊ฒฝ์์ ํ์ํ ํ๊ฒฝ ๋ณ์๋ค
}
์ปจํ ์ด๋๋ฅผ ์ด๊ธฐํํ๊ธฐ ์ ์ฌ์ฉํ ํ๋กํ์ ์ ํํ๋ฉด ๋๋ค. ์๋์ ์์๋๋ก ์คํํ์ฌ์ผ ํ๋ค.
public class MyConfigClass {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("dev"); // dev ํ๋กํ ํ์ฑํ
context.register(MyConfigClass.class, DsDevConfig.class, DsProdConfig.class);
context.refresh(); // ์ปจํ
์ด๋ ์ด๊ธฐํ
}
์ฌ๋ฌ ๊ฐ์ ํ๋กํ์ ์ด์ฉํ๋ ๊ฒฝ์ฐ ์ฌ๋ฌ ๊ฐ์ ํ๋กํ๋ช ์ ํ๋ผ๋ฏธํฐ๋ก ์ ๋ฌํ๋ฉด ๋๋ค.
context.getEnvironment().setActiveProfiles("dev", "mysql"); // dev ํ๋กํ ํ์ฑํ
๋๋ ํ๋กํ์ ์ฌ๋ฌ ๊ฐ์ ์ด๋ฆ์ ์ง์ ํ๊ณ ์ ํ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋ช ์ํ๋ฉด ๋๋ค.
@Configuration
@Profile("prod,test") // ํ๋กํ๋ช
์ผ๋ก "prod" ๋๋ "test"๊ฐ ์ ํ๋ ์ ํฌํจ๋๋ ์ค์ ๋ค
public class DsProdConfig {
... ๋ฐฐํฌ ํ๊ฒฝ์์ ํ์ํ ํ๊ฒฝ ๋ณ์๋ค
}
๋ค์๊ณผ ๊ฐ์ด ๋ํดํธ ํ๋กํ(ํ๋กํ ์ ํ๋ฌธ์ด ์๋ ๊ฒฝ์ฐ ์ ์ฉํ ํ๋กํ)์ ์ง์ ํ ์๋ ์๋ค.
@Configuration
@Profile("!dev") // !๋ฅผ ์ด์ฉํ์ฌ ๊ธฐ๋ณธ ํ๋กํ ์ง์
public class DsProdConfig {
... ๋ฐฐํฌ ํ๊ฒฝ์์ ํ์ํ ํ๊ฒฝ ๋ณ์๋ค
}
Last updated