๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์›น๊ณต๋ทฐ/์Šคํ”„๋ง

[spring] maven config ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ ์ถ”๊ฐ€/์„ค์ •/ํ™œ์šฉ

by ์ด๋…ธํ‚ค_ 2022. 6. 9.

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 ํŒŒ์ผ์—์„œ ์ฝ์–ด์˜จ ๊ฐ’์„ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

๋Œ“๊ธ€