์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- ํธ๋ ์ด๋
- ํด์ธ์ฌํ
- ์ฐจํธ๋ถ์
- ๋ง์ง
- ๊ฒฝ์ ์งํ
- ๋นํธ์ฝ์ธ
- ๋งค๋งค์ ๋ต
- ์ผ์
- ๊ธ๋ฆฌ
- ๊ตญ๋ด์ฌํ
- ๊ฒฝ์
- ํ์จ
- ์๋ฆฌ์ดํธํ๋
- ๋ฏธ๊ตญ์ฌํ
- ์ํธํํ
- ๋ฐ์ดํฐ๋ถ์
- C++
- Python
- ์งํ
- ํธ๋ ์ด๋ฉ
- ํ์ด์ฌ
- ๋ถ๋์ฐ
- ํ๋ก๊ทธ๋๋ฐ
- ํฌ์
- ์ฌํ
- ๋ฐ์ด๋ธ์ค
- ์ธ๊ณ๊ฒฝ์
- ์ฃผ์
- cpp
- ์ด๋๋ฆฌ์
- Today
- Total
์ถ์ ๊ณต์
[Spring] DI์์ ์ธ๋ถ ์ค์ ์ฃผ์ ํ๊ธฐ: @ConfigurationProperties ํ์ฉ ๋ณธ๋ฌธ
[Spring] DI์์ ์ธ๋ถ ์ค์ ์ฃผ์ ํ๊ธฐ: @ConfigurationProperties ํ์ฉ
dkrehd 2025. 4. 29. 19:42๐ฑ Spring DI์์ ์ธ๋ถ ์ค์ ์ฃผ์ ํ๊ธฐ: @ConfigurationProperties ํ์ฉ ๊ฐ์ด๋
์๋ ํ์ธ์! Spring (ํนํ Spring Boot) ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ๋ฐํ๋ค ๋ณด๋ฉด, ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ์ ์ ๋ณด, API ํค, ์๋ฒ ๋๋ฉ์ธ ๋ฑ ๋ค์ํ ์ค์ ๊ฐ๋ค์ ์ฝ๋์ ๋ถ๋ฆฌํ์ฌ ๊ด๋ฆฌํด์ผ ํ ํ์์ฑ์ ๋๋ผ๊ฒ ๋ฉ๋๋ค. ๊ฐ๋ฐ, ํ ์คํธ, ์ด์ ํ๊ฒฝ๋ง๋ค ๋ค๋ฅธ ์ค์ ์ ์ฌ์ฉํด์ผ ํ๊ฑฐ๋, ๋ฏผ๊ฐํ ์ ๋ณด๋ฅผ ์ฝ๋์ ์ง์ ๋ ธ์ถ์ํค์ง ์๊ธฐ ์ํด์์ฃ .
Spring์ ์ด๋ฌํ ์๊ตฌ์ฌํญ์ ๋ง์กฑ์ํค๊ธฐ ์ํด application.properties ๋๋ application.yml ๊ฐ์ ์ธ๋ถ ์ค์ ํ์ผ์ ํ์ฉํ๋ ๊ฐ๋ ฅํ๊ณ ํธ๋ฆฌํ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค. ์ค๋์ ๊ทธ์ค์์๋ ํ์ -์ธ์ดํ(Type-Safe) ํ๊ฒ ์ธ๋ถ ์ค์ ๊ฐ์ ์๋ฐ ๊ฐ์ฒด๋ก ๋ฐ์ธ๋ฉํ์ฌ ์ฌ์ฉํ๋ @ConfigurationProperties ์ด๋ ธํ ์ด์ ์ ๋ํด ์์ ์ฝ๋์ ํจ๊ป ์์ธํ ์์๋ณด๊ฒ ์ต๋๋ค.
์ ์ธ๋ถ ์ค์ ํ์ผ์ ์ฌ์ฉํ ๊น์?
์ฝ๋ ์์ ์ค์ ๊ฐ์ ์ง์ ํ๋์ฝ๋ฉํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๋ฌธ์ ๋ค์ด ๋ฐ์ํ ์ ์์ต๋๋ค.
- ํ๊ฒฝ๋ณ ์ค์ ๋ณ๊ฒฝ์ ์ด๋ ค์: ๊ฐ๋ฐ ํ๊ฒฝ๊ณผ ์ด์ ํ๊ฒฝ์ DB ์ ๋ณด๋ ์๋ฒ ์ฃผ์๊ฐ ๋ค๋ฅธ ๊ฒฝ์ฐ, ๋งค๋ฒ ์ฝ๋๋ฅผ ์์ ํ๊ณ ๋ค์ ๋น๋/๋ฐฐํฌํด์ผ ํฉ๋๋ค.
- ๋ณด์ ๋ฌธ์ : API ํค๋ ๋น๋ฐ๋ฒํธ ๊ฐ์ ๋ฏผ๊ฐํ ์ ๋ณด๊ฐ ์ฝ๋์ ๊ทธ๋๋ก ๋ ธ์ถ๋ ์ ์์ต๋๋ค.
- ์ ์ง๋ณด์ ์ด๋ ค์: ์ค์ ๊ฐ์ด ์ฝ๋ ๊ณณ๊ณณ์ ํฉ์ด์ ธ ์์ผ๋ฉด ๊ด๋ฆฌํ๊ธฐ ์ด๋ ต๊ณ ๋ณ๊ฒฝ ์ ๋๋ฝ๋ ๊ฐ๋ฅ์ฑ์ด ์์ต๋๋ค.
์ธ๋ถ ์ค์ ํ์ผ์ ์ฌ์ฉํ๋ฉด ์ด๋ฌํ ๋ฌธ์ ๋ค์ ํด๊ฒฐํ๊ณ , ์ค์ ์ ์ค์์์ ๊ด๋ฆฌํ๋ฉฐ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ ์ฐ์ฑ๊ณผ ๋ณด์์ฑ์ ๋์ผ ์ ์์ต๋๋ค.
Spring์ ํด๊ฒฐ์ฑ : @ConfigurationProperties
Spring Boot๋ application.properties ๋ application.yml ํ์ผ์ ํด๋์คํจ์ค ๋ฃจํธ(src/main/resources)์ ๋๋ฉด ์๋์ผ๋ก ๋ก๋ํ์ฌ ์ฌ์ฉํฉ๋๋ค. ํ์ง๋ง ๋จ์ํ @Value ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ฌ ๊ฐ๋ณ ์์ฑ์ ํ๋์ฉ ์ฃผ์ ๋ฐ๋ ๋ฐฉ์์ ์์ฑ์ด ๋ง์์ง์๋ก ๋ฒ๊ฑฐ๋กญ๊ณ ํ์ ์์ ์ฑ์ด ๋จ์ด์ง ์ ์์ต๋๋ค.
@ConfigurationProperties๋ ๊ด๋ จ ์๋ ์ค์ ๊ฐ๋ค์ ํ๋์ ์๋ฐ ๊ฐ์ฒด(POJO) ๋ก ๋ฌถ์ด์ ๊ด๋ฆฌํ๊ฒ ํด์ฃผ๋ ํจ์ฌ ์ธ๋ จ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
ํต์ฌ ๋จ๊ณ:
- ์ธ๋ถ ์ค์ ํ์ผ ์ ์ (application.properties)
- ์ค์ ๊ฐ์ ๋ด์ ์๋ฐ ํด๋์ค ์์ฑ ๋ฐ @ConfigurationProperties ์ ์ฉ (MyProperties.java)
- ํด๋น Properties ํด๋์ค๋ฅผ ๋น์ผ๋ก ๋ฑ๋กํ๊ณ ๋ฐ์ธ๋ฉ ํ์ฑํ (@EnableConfigurationProperties in Main.java)
- ์ค์ ๊ฐ์ฒด๋ฅผ ์ฃผ์ ๋ฐ์ ์ฌ์ฉ (@Autowired in Main.java)
์ด์ ์์ ์ฝ๋๋ฅผ ํตํด ๊ฐ ๋จ๊ณ๋ฅผ ์์ธํ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
๐ ์์ ์ฝ๋๋ก ๋ฐฐ์ฐ๋ ์ค์ ์ฃผ์ ๋ฐฉ๋ฒ
๐ธ (1) application.properties ํ์ผ ์์ฑ
๋จผ์ , src/main/resources ํด๋ ์๋์ application.properties ํ์ผ์ ๋ง๋ค๊ณ ์ค์ ๊ฐ๋ค์ ์ ์ํฉ๋๋ค. ์ฌ๊ธฐ์ ์ค์ํ ๊ฒ์ ๊ด๋ จ ์๋ ์์ฑ๋ค์ ๊ณตํต๋ ์ ๋์ฌ(prefix) ๋ก ์์ํ๊ฒ ๋ง๋๋ ๊ฒ์ ๋๋ค. ์์ ์์๋ mysite๋ฅผ ์ ๋์ฌ๋ก ์ฌ์ฉํ์ต๋๋ค.
# src/main/resources/application.properties
mysite.domain=www.ABC.com
mysite.email=admin@ABC.com
๐ธ (2) ์ค์ ์ ๋ณด๋ฅผ ๋ด์ ํด๋์ค ์์ฑ (MyProperties.java)
๋ค์์ผ๋ก, application.properties ํ์ผ์ mysite ์ ๋์ฌ๋ฅผ ๊ฐ์ง ์์ฑ๋ค์ ๋ด์ ์๋ฐ ํด๋์ค(MyProperties)๋ฅผ ์์ฑํฉ๋๋ค.
package com.yourpackage; // ํจํค์ง ๊ฒฝ๋ก๋ ์ค์ ํ๋ก์ ํธ์ ๋ง๊ฒ ์์ ํ์ธ์.
import org.springframework.boot.context.properties.ConfigurationProperties;
// @ConfigurationProperties(prefix="mysite"): "mysite" ์ ๋์ฌ๋ฅผ ๊ฐ์ง ์์ฑ๋ค์ ์ด ํด๋์ค์ ํ๋์ ๋ฐ์ธ๋ฉํฉ๋๋ค.
@ConfigurationProperties(prefix = "mysite")
public class MyProperties {
private String domain; // mysite.domain ๊ฐ๊ณผ ๋งคํ๋ฉ๋๋ค.
private String email; // mysite.email ๊ฐ๊ณผ ๋งคํ๋ฉ๋๋ค.
// Spring์ด ๊ฐ์ ๋ฐ์ธ๋ฉํ๊ธฐ ์ํด Getter์ Setter๊ฐ ํ์ํฉ๋๋ค.
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// (toString() ๋ฉ์๋ ๋ฑ์ ํ์์ ๋ฐ๋ผ ์ถ๊ฐ)
}
- @ConfigurationProperties(prefix = "mysite"): ์ด ์ด๋ ธํ ์ด์ ์ด ํต์ฌ์ ๋๋ค! Spring Boot์๊ฒ "application.properties (๋๋ ๋ค๋ฅธ ์ธ๋ถ ์ค์ ์์ค)์์ mysite. ์ผ๋ก ์์ํ๋ ์์ฑ๋ค์ ์ฐพ์์ ์ด ํด๋์ค์ ํ๋์ ์๋์ผ๋ก ์ฑ์ ๋ฃ์ด์ค!" ๋ผ๊ณ ์ง์ํฉ๋๋ค.
- ํ๋ ์ด๋ฆ ๋งคํ: Spring Boot๋ ์ ์ฐํ ๋ฐ์ธ๋ฉ ๊ท์น์ ๋ฐ๋ฆ ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก mysite.some-property ๋๋ mysite.someProperty๋ someProperty ํ๋์ ๋งคํ๋ฉ๋๋ค. (์: mysite.domain -> domain ํ๋)
- Getter/Setter ํ์: Spring์ด ๋ฆฌํ๋ ์ ์ ํตํด ๊ฐ์ ์ฃผ์ ํ๋ ค๋ฉด ํด๋น ํ๋์ ๋ํ public Getter์ Setter๊ฐ ํ์ํฉ๋๋ค. (Lombok์ @Getter, @Setter ๋๋ @Data๋ฅผ ์ฌ์ฉํด๋ ๋ฉ๋๋ค.)
๐ธ (3) ๋ฉ์ธ ์ ํ๋ฆฌ์ผ์ด์ ์์ฑ
@ConfigurationProperties ์ด๋ ธํ ์ด์ ์ ๋ถ์ธ ํด๋์ค๊ฐ ์ค์ ๋ก ๋์(์์ฑ ๊ฐ ๋ฐ์ธ๋ฉ)ํ๊ณ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก๋๊ฒ ํ๋ ค๋ฉด, ์ด ๊ธฐ๋ฅ์ ํ์ฑํํ๋ ๊ณผ์ ์ด ํ์ํฉ๋๋ค. @EnableConfigurationProperties ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ฌ ์ด ์์ ์ ์ํํฉ๋๋ค. ๋ณดํต @Configuration ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ์ค์ ํด๋์ค๋ ๋ฉ์ธ ์ ํ๋ฆฌ์ผ์ด์ ํด๋์ค์ ์ถ๊ฐํฉ๋๋ค.
package com.yourpackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
// @EnableConfigurationProperties({MyProperties.class}): MyProperties ํด๋์ค์ ๋ํ @ConfigurationProperties ์ฒ๋ฆฌ๋ฅผ ํ์ฑํํ๊ณ ,
// MyProperties๋ฅผ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋กํฉ๋๋ค.
@EnableConfigurationProperties({MyProperties.class})
@Configuration // ์ด ํด๋์ค๊ฐ ์ค์ ์ ๋ณด๋ฅผ ํฌํจํจ์ ์๋ฆผ
@ComponentScan // @Component ๋ฑ์ ์ค์บํ์ฌ ๋น์ผ๋ก ๋ฑ๋ก (์์ ์์๋ MyProperties ์ธ ๋ค๋ฅธ ๋น์ ์์ง๋ง ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ)
public class Main implements CommandLineRunner { // CommandLineRunner: ์ ํ๋ฆฌ์ผ์ด์
์์ ํ ํน์ ์ฝ๋ ์คํ
@Autowired
MyProperties prop; // 4๋จ๊ณ: ์ค์ ๊ฐ์ฒด ์ฃผ์
@Autowired
ApplicationContext ac; // ApplicationContext ์ฃผ์
(๋น ๋ชฉ๋ก ํ์ธ์ฉ)
// CommandLineRunner์ run ๋ฉ์๋: ์ ํ๋ฆฌ์ผ์ด์
์ด ์์๋๊ณ DI๊ฐ ์๋ฃ๋ ํ ์คํ๋จ
@Override
public void run(String... args) throws Exception {
// ๋ฑ๋ก๋ ๋น ๋ชฉ๋ก ํ์ธ (๋๋ฒ๊น
๋ชฉ์ )
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
Arrays.sort(beanDefinitionNames);
Arrays.stream(beanDefinitionNames)
.filter(b -> !b.startsWith("org")) // Spring ๋ด๋ถ ๋น ์ ์ธ
.forEach(System.out::println); // ๋น ์ด๋ฆ ์ถ๋ ฅ
// ์ฃผ์
๋ฐ์ MyProperties ๊ฐ์ฒด๋ฅผ ํตํด ์ค์ ๊ฐ ์ฌ์ฉ
System.out.println("prop.getDomain() = " + prop.getDomain());
System.out.println("prop.getEmail() = " + prop.getEmail());
}
public static void main(String[] args) {
// Spring Boot ์ ํ๋ฆฌ์ผ์ด์
์คํ
ApplicationContext context = SpringApplication.run(Main.class, args);
}
}
- @EnableConfigurationProperties({MyProperties.class}): ์ด ์ด๋ ธํ ์ด์ ์ด ๋งค์ฐ ์ค์ํฉ๋๋ค. Spring์๊ฒ MyProperties ํด๋์ค์ ์ ์๋ @ConfigurationProperties ์ค์ ์ ์ฒ๋ฆฌํ๋๋ก ์ง์ํ๊ณ , ๋์์ MyProperties ์์ฒด๋ฅผ ์คํ๋ง ์ปจํ ์ด๋์ ๋น์ผ๋ก ๋ฑ๋กํด์ค๋๋ค. ์ด์ ๋ค๋ฅธ ์ปดํฌ๋ํธ์์ @Autowired๋ฅผ ํตํด ์ฃผ์ ๋ฐ์ ์ ์๊ฒ ๋ฉ๋๋ค.
๐ธ (4) ์ค์ ๊ฐ์ฒด ์ฃผ์ ๋ฐ ์ฌ์ฉ
์ด์ @EnableConfigurationProperties ๋๋ถ์ ๋น์ผ๋ก ๋ฑ๋ก๋๊ณ ์ธ๋ถ ์ค์ ๊ฐ์ผ๋ก ์ฑ์์ง MyProperties ๊ฐ์ฒด๋ฅผ ํ์ํ ๊ณณ์์ ์ฃผ์ ๋ฐ์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
// ... Main ํด๋์ค ๋ด๋ถ ...
@Autowired // ์คํ๋ง ์ปจํ
์ด๋๊ฐ ๊ด๋ฆฌํ๋ MyProperties ๋น์ ์๋์ผ๋ก ์ฃผ์
๋ฐ์
MyProperties prop;
@Override
public void run(String... args) throws Exception {
// ์ฃผ์
๋ฐ์ prop ๊ฐ์ฒด์ getter ๋ฉ์๋๋ฅผ ํตํด ์ค์ ๊ฐ์ ์ ๊ทผ
System.out.println("prop.getDomain() = " + prop.getDomain()); // ์ถ๋ ฅ: prop.getDomain() = www.fastcampus.com
System.out.println("prop.getEmail() = " + prop.getEmail()); // ์ถ๋ ฅ: prop.getEmail() = admin@fascampus.com
}
- @Autowired MyProperties prop;: Spring์ด ๊ด๋ฆฌํ๋ MyProperties ๋น์ ์ฐพ์ prop ํ๋์ ์ฃผ์ ํด์ค๋๋ค. ์ด ์์ ์ prop ๊ฐ์ฒด๋ ์ด๋ฏธ application.properties ํ์ผ์ ๊ฐ๋ค๋ก ์ฑ์์ ธ ์์ต๋๋ค.
- CommandLineRunner ์ฌ์ฉ ์ด์ : main ๋ฉ์๋๋ static ๋ฉ์๋์ด๋ฏ๋ก @Autowired ํ๋(prop)๋ฅผ ์ง์ ์ฌ์ฉํ ์ ์์ต๋๋ค. CommandLineRunner ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ณ run ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ํ๋ฉด, Spring ์ ํ๋ฆฌ์ผ์ด์ ์ด ์์๋๊ณ ๋ชจ๋ ๋น์ ์์ฑ๊ณผ ์์กด์ฑ ์ฃผ์ ์ด ์๋ฃ๋ ํ์ run ๋ฉ์๋๊ฐ ํธ์ถ๋ฉ๋๋ค. ๋ฐ๋ผ์ run ๋ฉ์๋ ๋ด์์๋ ์์ ํ๊ฒ @Autowired๋ ์ธ์คํด์ค ๋ณ์(prop)์ ์ ๊ทผํ ์ ์์ต๋๋ค.
@ConfigurationProperties์ ์ฅ์
- ํ์ ์์ ์ฑ (Type Safety): ๋ฌธ์์ด ๊ธฐ๋ฐ์ @Value์ ๋ฌ๋ฆฌ, ์ ์๋ ํด๋์ค์ ํ์ (String, int, List ๋ฑ)์ผ๋ก ๊ฐ์ ๋ฐ์์ค๋ฏ๋ก ํ์ ์ค๋ฅ๋ฅผ ์ปดํ์ผ ์์ ๋๋ IDE์์ ๋ฏธ๋ฆฌ ๋ฐ๊ฒฌํ๊ธฐ ์ฝ์ต๋๋ค.
- ์ฝ๋ ๊ฐ๋ ์ฑ ๋ฐ ๊ตฌ์กฐํ: ๊ด๋ จ๋ ์ค์ ๊ฐ๋ค์ด ํ๋์ ํด๋์ค๋ก ๋ฌถ์ฌ์์ด ๊ด๋ฆฌ๊ฐ ์ฉ์ดํ๊ณ ์ฝ๋๊ฐ ๊น๋ํด์ง๋๋ค.
- IDE ์ง์: @ConfigurationProperties ํด๋์ค๋ IDE์ ์๋ ์์ฑ ๋ฐ ๊ฒ์ฆ ๊ธฐ๋ฅ์ ํ์ฉํ๊ธฐ ์ข์ต๋๋ค. (application.properties์์ ํค ์๋์์ฑ ๋ฑ)
- ์ ํจ์ฑ ๊ฒ์ฌ (Validation): JSR-303(Bean Validation) ์ด๋ ธํ ์ด์ (@NotNull, @Email, @Max ๋ฑ)์ Properties ํด๋์ค ํ๋์ ์ถ๊ฐํ์ฌ ์ค์ ๊ฐ์ ์ ํจ์ฑ์ ๊ฒ์ฆํ ์ ์์ต๋๋ค. (์์ ์๋ ์์ง๋ง ๋งค์ฐ ์ ์ฉํ ๊ธฐ๋ฅ)
- ์ ์ฐํ ๋ฐ์ธ๋ฉ: some-property, someProperty, SOME_PROPERTY ๋ฑ ๋ค์ํ ํ์์ ํ๋กํผํฐ ํค๋ฅผ ๋์ผํ ํ๋(someProperty)์ ๋ฐ์ธ๋ฉํ ์ ์์ต๋๋ค.
๊ฒฐ๋ก
Spring (ํนํ Spring Boot)์์ ์ธ๋ถ ์ค์ ๊ฐ์ ๊ด๋ฆฌํ๋ ๊ฐ์ฅ ๊ถ์ฅ๋๋ ๋ฐฉ๋ฒ ์ค ํ๋๋ @ConfigurationProperties๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค. ์ด๋ฅผ ํตํด ์ค์ ๊ฐ๋ค์ ํ์ -์ธ์ดํํ ์๋ฐ ๊ฐ์ฒด๋ก ์ฝ๊ฒ ๋ฐ์ธ๋ฉํ๊ณ , ์ฝ๋์ ์ค์ ์ ๋ช ํํ๊ฒ ๋ถ๋ฆฌํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ ์ฐ์ฑ, ๊ฐ๋ ์ฑ, ์ ์ง๋ณด์์ฑ์ ํฌ๊ฒ ํฅ์์ํฌ ์ ์์ต๋๋ค.