Properties
์๋น์ค ํ๊ฒฝ์ ๋ฐ๋ผ ์๋ก ๋ค๋ฅธ ํ๊ฒฝ ๋ณ์๋ฅผ ์ฌ์ฉํ๊ณ ์ ํ ๋(dev, prod) ์ฌ์ฉํ๋ ๊ธฐ๋ฅ์๋ Profile๊ณผ Property๊ฐ ์๋ค.
์ ์
์คํ๋ง์ ์ฌ๋ฌ ํ๊ฒฝ ๋ณ์๋ฅผ ํฌํจํ ์ธ๋ถ์ ํ์ผ
์ ์ด์ฉํ์ฌ ์คํ๋ง ๋น์ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค. ์ด ์ธ๋ถ์ ํ์ผ์ ํ๋กํผํฐ ํ์ผ์ด๋ผ๊ณ ํ๋ฉฐ, properties ํ์ฅ์๋ฅผ ๊ฐ๋๋ค.
์ฌ์ฉ๋ฒ
ํ๋กํผํฐ ํ์ผ์ ๋ค์๊ณผ ๊ฐ์ ํ์์ผ๋ก ์์ฑ๋๋ค.
ํ๋กํผํฐ๋ช
=๊ฐ
ํ๋กํผํฐ๋ช
=๊ฐ
...
์ด ํ๋กํผํฐ ํ์ผ ๋ด์ ๋ณ์๋ค์ ๋ค์์ ๊ณผ์ ์ ๊ฑฐ์ณ ์ฌ์ฉํ ์ ์๋ค.
PropertySourcesPlaceholderConfigurer ๋น ๋ฑ๋ก
@Value ์ ๋ ธํ ์ด์ ์ ํตํด ํ๋กํผํฐ ๊ฐ ์ฌ์ฉ
1. PropertySourcesPlaceholderConfigurer ๋น ๋ฑ๋ก
@Configuration
public class Config {
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer configurer =
new PropertySourcesPlaceholderConfigurer();
configurer.setLocations(
new ClassPathResource("application.properties"),
new ClassPathResource("database.properties")
);
return configurer;
}
}
PropertySourcesPlaceholderConfigurer
๋น์ ๋ฑ๋กํ๋ ๋ฉ์๋๋ ๋ฐ๋์ static์ผ๋ก ์ ์ธ๋์ด์ผ ํ๋ค.
2. @Value ์ ๋
ธํ
์ด์
์ ํตํด ํ๋กํผํฐ ๊ฐ ์ฌ์ฉ
๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
@Value("${property.value}")
private String propertyValue;
@Value ์ด๋ ธํ ์ด์ ์ ํ๋ผ๋ฏธํฐ๋ก ํ๋กํผํฐ์ ์ด๋ฆ์ ${} ์์ ๋ฃ์ด ์ ๋ฌํ๋ฉด ํด๋น ์ด๋ ธํ ์ด์ ์ด ์ฌ์ฉ๋ ๋ณ์์ ํ๋กํผํฐ์ ๊ฐ์ด ํ ๋น๋๋ค.
Last updated