0. ์ ์ฌ์ฉํ๋?
- ๊ฐ๋ฐํ๊ฒฝ๊ณผ ๋ฐฐํฌํ๊ฒฝ์ด ๋ค๋ฅผ ๊ฒฝ์ฐ๊ฐ ๋น์ผ๋น์ฌํ๋ค.
- ์ผ๋ฐ์ ์ผ๋ก ๊ฐ๋ฐ์ ์๋์ฐ์์, ๋ฐฐํฌ๋ ๋ฆฌ๋
์ค ์๋ฒ์์ ํ๊ฒ ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
- ๋งค๋ฒ ํ๊ฒฝ์ค์ ์ ๊ทธ์๋ง๊ฒ ์์ ํ๋ค๋ณด๋ฉด ๊ฐ๋ ์ค์๋ฅผ ํ๊ธฐ ๋ง๋ จ.
- ์์ ๊ฐ๋ฐ๋ชจ๋/๋ฐฐํฌ๋ชจ๋๋ฅผ ๊ตฌ๋ถํ์ฌ ์ค์ ํ์ผ์ ๊ฐ์ ์ฝ์ด์ค๋๋กํ์.
1. ์ฌ์ฉํ๋ ํ์ผ
- web.xml : ๊ฐ๋ฐ๋ชจ๋์ธ์ง ๋ฐฐํฌ๋ชจ๋์ธ์ง๋ฅผ ๊ตฌ๋ถ
- config.properties : config.properties ๋ฟ๋ง ์๋๋ผ config-dev.prop~, config-prod.prop~ ๋ฑ ๋ฐฐํฌ ํ๊ฒฝ๋ณ ์ค์ ํ์ผ
- Config.java : ์ค์ ํ์ผ์ key, value๋ฅผ ์ฝ์ด์ ์ค์ ์ฝ๋์์ ์ฌ์ฉํ๊ฒ ๋ง๋ค์ด์ฃผ๋ ํด๋์ค
2. web.xml
- ~/src/main/webapp/WEB-INF/web.xml ํ์ผ
- ์๋ ๋ด์ฉ์ ์ถ๊ฐํ๋ค.
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
- ์ฌ๊ธฐ์ dev๋ ์ด๋ค ํ๋กํผํฐ ํ์ผ์ ์ฌ์ฉํ ์ง๋ฅผ ๊ตฌ๋ถํ๋ ๋ณ์๋ก ์ฌ์ฉ๋๋ค.
- ์ด ์์๋ config-dev.properties ํ์ผ์ ์ฌ์ฉํ ๊ฒ์ด๋ค.
3. config.properties
- ~/src/main/resources/config.properties
- ์ค์ ๋ก ์ค์ ๊ฐ๋ค์ด ์ ์๋์ด์๋ ํ์ผ์ด๋ค.
- ํ๋กํผํฐ ํ์ผ์ 2์ข
๋ฅ๊ฐ ์๋ค.
(1) config.properties : ๊ฐ๋ฐ ๋ฒ์ ์ด๋ ๋ฐฐํฌ๋ฒ์ ์ด๋ ์๊ด์์ด ๊ณตํต์ ์ธ ์ค์ ๊ฐ๋ค์ด ๋ช
์๋จ.
(2) config-{์ข
๋ฅ}.properties: ๋์ ๊ฒฝ์ฐ์๋ config-dev.properties, config-prod.properties, config-test.properties ์ด๋ ๊ฒ ํ์ผ์ด ์กด์ฌํ๋ค.
์ฃผ์ํ ๊ฒ์ ํ์ผ๋ช
์ ๋ฌ๋ผ๋ ๋ด๋ถ๋ฅผ ๊ตฌ์ฑํ๋ key name์ ๋์ผํด์ผํ๋ค.
๊ทธ๋ ์ง ์์๋ค๋ฉด ๋น์ ์ ํ๋ก๊ทธ๋จ์ ์คํํ๋ค๊ฐ ๊ฐ์๊ธฐ ์ฃฝ์์๋ ์๋ค. ("serverip"๊ฐ A.properteis์๋ ์๊ณ , B.properties์ ์์ผ๋ฉด ์ด๋ฅผ ํ์ฉํ๋ ํด๋์ค์์ ๋ถ๋ช
ํ ์๋ฌ๋ฅผ ๋ง๋ค์ด๋ฒ๋ฆฐ๋ค.)
- ๋์ ๊ณตํต ์ค์ ํ์ผ(config.properties)์ ์๋์ ๊ฐ์ด ์ ์๋์ด์๋ค.
#Connection config
timeout.millisec = 10000
conn.max.total = 100
conn.max.per.route = 5
{์ข
๋ฅ}๋ web.xml์์ ์ ์ํ value๊ฐ์ด๋ค.
4. Config.java
- ์ฐ์ ๋จผ์ ์๋ ์ฝ๋๋ฅผ ๋ณต์ฌํ์.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@PropertySources({
@PropertySource("classpath:config-${spring.profiles.active}.properties"),
@PropertySource("classpath:config.properties")
})
public class WsConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer wsConfigBean() {
return new PropertySourcesPlaceholderConfigurer();
}
}
- ${spring.profiles.active} ๋ถ๋ถ์ web.xml์์ ์ ์ํ ์ด๋ฆ์ผ๋ก ๋์ฒด๋์ด ๋ค์ด๊ฐ๋ค.
- ์ค์ ํ์ผ์ ์ฌ๋ฌ๊ฐ๋ฅผ ๋ช
์ํ ์ ์๋๋ฐ config.properties์ config-dev.properties(๋๋ dev๋ก ๋ช
์ํ์ผ๋ฏ๋ก)๋ก ์ค์ ๋์๋ค.
5. ํ์ฉ
์ฌ์ฉํ๊ณ ์ ํ๋ ํด๋์ค์์ ์๋ ์ฒ๋ผ ์ ์ธํ๋ค.
public class TestContorller {
@Value("${druid.ip}")
String druidip;
@Value("${druid.query.port}")
String druidport;
}
๊ทธ๋ผ config ํ์ผ์์ ์ฝ์ด์จ ๊ฐ์ ํด๋์ค ๋ด๋ถ์์ ํ์ฉํ ์ ์๋ค.
๋๊ธ