์คํ๋ง ํ๋ ์์ํฌ(Spring Framework)์ ๊ฐ์ฅ ์ค์ํ ๊ฐ๋ ์ค ํ๋๋ DI(Dependency Injection, ์์กด์ฑ ์ฃผ์ )์ ๋๋ค. DI๋ฅผ ์ดํดํ๋ฉด ๋ณด๋ค ์ ์ฐํ๊ณ ์ ์ง๋ณด์ํ๊ธฐ ์ฌ์ด ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ค ์ ์์ต๋๋ค. ์ด๋ฒ ๊ธ์์๋ DI๊ฐ ๋ฌด์์ธ์ง, ์ ํ์ํ์ง, ๊ทธ๋ฆฌ๊ณ ์คํ๋ง์์ DI๋ฅผ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค!
1. ์์กด์ฑ(Dependency)์ด๋?
ํ๋์ ๊ฐ์ฒด๊ฐ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํ์๋ก ํ๋ ๊ด๊ณ๋ฅผ ๋ปํฉ๋๋ค.
์ฆ, ํ๋์ ๊ฐ์ฒด๊ฐ ๋์ํ๊ธฐ ์ํด ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํฉ๋๋ค.
์ ๋ ์์ผ๋ก ์์กด๊ณผ ์ฌ์ฉ์ ๋์ผํ ์๋ฏธ๋ก ์ธ๊ธํ๋๋ก ํ๊ฒ ์ต๋๋ค! (feat. ์ก์์ค)
์์ ๋ฅผ ๋ณด์๋ฉด,
public class ProductController {
private ProductService productService = new ProductService();
public String findProductName(){
productService.findProductName();
}
}
์์ฒ๋ผ ProductController์์ ProductService๋ฅผ ์ง์ ์์ฑ(new ProductService())ํ๊ณ ์์ต๋๋ค.
ํด๋์ค ๋ด๋ถ์์ ๊ฐ์ฒด๋ฅผ ์ง์ ์์ฑํ๋ ๊ฒ์ ๊ฐํ ๊ฒฐํฉ(Tight Coupling)์ ํด๋นํฉ๋๋ค.
์ฆ, ProductController ํด๋์ค๋ ProductService ํด๋์ค๋ฅผ ์ฌ์ฉ(=์์กด)ํ๋ฉฐ, ProductService๊ฐ ์๋ค๋ฉด ProductController๋ ์ ์์ผ๋ก ์๋ํ ์ ์์ต๋๋ค.
์ด๋ฌํ ์์กด๋๋ฅผ ๋ฎ์ถ๊ธฐ ์ํด ์คํ๋ง์์๋ DI(์์กด์ฑ ์ฃผ์ )๋ผ๋ ๊ฐ๋ ์ ์ฌ์ฉํ๊ฒ ๋ฉ๋๋ค!
2. DI(์์กด์ฑ ์ฃผ์ )๋?
๊ฐ์ฒด๊ฐ ํ์๋ก ํ๋ ์์กด์ฑ์ ์ง์ ์์ฑํ์ง ์๊ณ ์ธ๋ถ์์ ์ฃผ์ ๋ฐ๋๋ก ํ๋ ๊ฐ๋ ์ ๋๋ค.
์ด๋ฅผ ํตํด ํด๋์ค ๊ฐ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ณ ์ฝ๋์ ์ ์ง๋ณด์๋ฅผ ์ฉ์ดํ๊ฒ ํ ์ ์์ต๋๋ค.
์์ ์์ ๋ฅผ DI ๋ฐฉ์์ผ๋ก ์์ ํด ๋ณด๊ฒ ์ต๋๋ค.
public class ProductController {
private final ProductService productService;
// ์์ฑ์ ์ฃผ์
๋ฐฉ์
public ProductController(ProductService productService){
this.productService = proudctService;
}
public String findProductName(){
productService.findProductName();
}
}
ProductController๋ ์ด์ ์ฒ๋ผ productService๋ฅผ ์ง์ ์์ฑํ์ง ์๊ณ , ์ธ๋ถ์์ ์์ฑ์๋ฅผ ํตํด ์ฃผ์ ๋ฐ๊ธฐ ๋๋ฌธ์ ๊ฒฐํฉ๋๊ฐ ๋ฎ์์ง๋๋ค.
๋ํ, DI๋ฅผ ํตํด IoC๋ฅผ ์คํํ๋ฏ๋ก ๊ฐ์ฒด ๋ผ์ดํ ์ฌ์ดํด์ ์คํ๋ง ์ปจํ ์ด๋๊ฐ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์์ต๋๋ค.
(+ ์คํ๋ง์ ์ฑ๊ธํค ํจํด์ ํตํด ๊ฐ์ฒด ์ฌ์ฌ์ฉ๋ ๊ฐ๋ฅ)
ProductService(interface)์ ์ํ๋ ๊ฐ์ฒด(ProductAService, ProductBService)๊ฐ ์ฌ๋ฌ ๊ฐ์ง๋ผ๋ฉด,
DI๋ฅผ ์ ์ฉํ๊ธฐ ์ ์ ๋ฐฉ๋ฒ์์๋ ์ด๋ฅผ ProductController๊ฐ ์ง์ ์ด๋ค ๊ฐ์ฒด๋ก ๋ง๋ค์ง ์ ํํด์ผ ํ๊ณ
DI๋ฅผ ์ ์ฉํ ํ์๋ ๊ตฌ์ฒด์ ์ธ ๊ตฌํ์ฒด๊ฐ ์๋ ProductService์๋ง ์์กดํ๊ฒ ๋ฉ๋๋ค.
๊ทธ๋ผ ์ด๋ฐ ์์กด์ฑ์ ์ฃผ์ ํ๋ ๋ฐฉ๋ฒ์๋ ์ด๋ ํ ๊ฒ๋ค์ด ์๋์ง ์์๋ณด๊ฒ ์ต๋๋ค.
3. DI(์์กด์ฑ ์ฃผ์ ) ๋ฐฉ๋ฒ 3๊ฐ์ง
Setter Injection(setter ์ฃผ์ )
Setter ๋ฉ์๋๋ฅผ ํตํด ์์กด์ฑ์ ์ฃผ์ ํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
@Controller
public class ProductController {
private ProductService productService;
//setter ์ฃผ์
@Autowired
public void setProductService(ProductService productService){
this.productService = proudctService;
}
public String findProductName(){
productService.findProductName();
}
}
ProductController๊ฐ ์์ฑ๋ ํ setter๋ฅผ ํตํด ProductService๋ฅผ ์ฃผ์ ๋ฐ๋๋ค. (=final ๋ถ๊ฐ)
setter๊ฐ public ๊ถํ์ด๊ธฐ์ ์ด๋์๋ setter๋ฅผ ํธ์ถํ์ฌ ProductService๋ฅผ ๋ณ๊ฒฝ ๊ฐ๋ฅํ๋ค.
๋ํ, ํ์ ์์กด์ฑ์ด ํด๋จผ์๋ฌ๋ก ์ฃผ์ ๋์ง ์์ ์ ์๋ค.
Field Injection(ํ๋ ์ฃผ์ ) - ์ง์๐ข
setter ์ฃผ์ ์ ๋จ์ ์ ๋ณด์ํ๊ธฐ ์ํด ํ๋์ ์ง์ @Autowired๋ฅผ ์ ์ฉํ๋ ๋ฐฉ์์ด ์๊ฒจ๋ฌ์ต๋๋ค.
@Controller
public class ProductController {
@Autowired
private ProductService productService;
public String findProductName(){
productService.findProductName();
}
}
3๊ฐ์ง ๋ฐฉ๋ฒ ์ค ๊ฐ์ฅ ๊ฐ๊ฒฐํ์ง๋ง, ํด๋์ค ์ธ๋ถ์์ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํ์ฌ ํ ์คํธ๊ฐ ์ด๋ ต๋ค.
๋ค๋ฅธ ๋ฐฉ๋ฒ๋ค์ DI ํ๋ ์์ํฌ๊ฐ ์๋๋ผ๋ ์ ์ ์๋์ด ๊ฐ๋ฅํ๋, ํด๋น ๋ฐฉ๋ฒ์ DI ํ๋ ์์ํฌ์ ๋์ ์์ด๋ ์ฌ์ฉ์ด ๋ถ๊ฐํ๋ค.
TDD๊ฐ ๋ณดํธํ๋จ์ ๋ฐ๋ผ ํด๋น ๋ฐฉ๋ฒ์ ์ง์๋๋ค.
Constructor Injection(์์ฑ์ ์ฃผ์ ) - ๊ถ์ฅ๐
@Controller
public class ProductController {
private final ProductService productService;
@Autowired
public ProductController(ProductService productService){
this.productService = productService;
}
public String findProductName(){
productService.findProductName();
}
}
final ํค์๋์ ํจ๊ป ์ฌ์ฉํ์ฌ ๋ถ๋ณ์ฑ์ ์ ์งํ ์ ์๋ค. (๋๋ถ๋ถ์ ์์กด ๊ด๊ณ๋ ์คํ ํ ์ข ๋ฃ๊น์ง ๋ณํ์ง ์๋๋ค.)
์์ ์๋ฐ ์ฝ๋์ ๊ฐ์ฅ ๋น์ทํ ์ฝ๋๋ก ๊ฐ์ฒด ์งํฅ์ ํน์ง์ด ์ ๋ํ๋๋ค.
ํ์ ์์กด์ฑ์ ๊ฐ์ ํ ์ ์๊ธฐ ๋๋ฌธ์ ํด๋จผ ์๋ฌ์ ๊ฐ๋ฅ์ฑ์ด ๋ฎ๋ค.
์คํ๋ง ํ๋ ์์ํฌ ๊ณต์ ๋ฌธ์์์๋ Constructor Injection ๋ฐฉ์์ ๊ฐ์ฅ ๊ถ์ฅํ๊ณ ์์ต๋๋ค.
๋ ์์ธํ ๋ด์ฉ์ด ๊ถ๊ธํ์ ๋ถ์ ๊ณต์ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์๊ธธ..!!
Dependency Injection :: Spring Framework
Constructor-based DI is accomplished by the container invoking a constructor with a number of arguments, each representing a dependency. Calling a static factory method with specific arguments to construct the bean is nearly equivalent, and this discussion
docs.spring.io
๊ทธ๋ผ ์๋ ... ~

'๐ชBackend > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
MSA์์ Response Entity์ Response custom class๋ก ์๋ต ํต์ผํ๊ธฐ (2) | 2025.03.04 |
---|