[Spring] IoC์™€ DI

yenยท2024๋…„ 6์›” 30์ผ

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
1/1
post-thumbnail

๐Ÿ’ก IoC(Inverse of Control, ์ œ์–ด์˜ ์—ญ์ „)

: ๊ฐ์ฒด์˜ ์ƒ์„ฑ๋ถ€ํ„ฐ ์ƒ๋ช…์ฃผ๊ธฐ ๊ด€๋ฆฌ๊นŒ์ง€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ๋ฆ„์„ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ๊ด€๋ฆฌ

๊ฐœ๋ฐœ์ž์˜ ์—ญํ• ์ด ์ค„์–ด๋“ค์–ด ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์— ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๊ณ ,
์ฝ”๋“œ์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๊ณ  ์œ ์—ฐ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Œ.

๐Ÿ’ก DI(Dependency Injection, ์˜์กด์„ฑ ์ฃผ์ž…)

: ๊ฐ์ฒด ๊ฐ„์˜ ์˜์กด์„ฑ์„ ์™ธ๋ถ€์—์„œ ์ฃผ์ž…ํ•ด์ฃผ๋Š” ๊ฐœ๋…

๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๊ณ , ์ฝ”๋“œ์˜ ์œ ์—ฐ์„ฑ๊ณผ ํ…Œ์ŠคํŠธ ์šฉ์ด์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Œ


Spring Bean / Container

Spring Bean

  • ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด
  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“  ๊ตฌ์„ฑ์š”์†Œ
  • POJO(Plain Old Java Object)

๐Ÿƒ What is POJO?

Spring Container

= IoC Container

  • ApplicationContext ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„๋จ
  • ์Šคํ”„๋ง ๋นˆ์˜ ์ƒ์„ฑ, ์˜์กด์„ฑ ์ฃผ์ž…, ์ƒ๋ช…์ฃผ๊ธฐ ๊ด€๋ฆฌ๋ฅผ ์ฑ…์ž„

์˜์กด์„ฑ ์ฃผ์ž… ๋ฐฉ์‹

1. ์ƒ์„ฑ์ž ์ฃผ์ž… (Constructor Injection)

public class MyClass {
	private InjectionBean injectionBean;
	
	@Autowired
	public ConstructorInjection(InjectionBean injectionBean) {
	    this.injectionBean = injectionBean;
	}
}
  • ๊ฐ€์žฅ ๊ถŒ์žฅ๋˜๋Š” ๋ฐฉ์‹
  • ํ•„์ˆ˜์ ์ธ ์˜์กด์„ฑ ๋ช…์‹œ์ ์œผ๋กœ ์ œ๊ณต
  • ๋ถˆ๋ณ€์„ฑ ๋ณด์žฅ. ์ฆ‰, ์˜์กด์„ฑ ๋ณ€๊ฒฝ ์œ„ํ—˜ ์—†์Œ
    • ํ•„๋“œ์— final ๋ถ™์ผ ์ˆ˜ ์žˆ์Œ

2. ์„ธํ„ฐ ์ฃผ์ž… (Setter Injection)

public class MyClass {
	private InjectionBean injectionBean;
	
	@Autowired
	public void setInjectionBean(InjectionBean injectionBean) {
	    this.injectionBean = injectionBean;
	}
}
  • ์„ ํƒ์ ์ธ ์˜์กด์„ฑ์œผ๋กœ ์œ ์—ฐ์„ฑ ์ œ๊ณต
    โžก๏ธ ๋‹จ, ๊ฐ์ฒด๊ฐ€ ์™„์ „ํžˆ ์ดˆ๊ธฐํ™”๋˜๊ธฐ ์ „์— setter ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์˜์กด์„ฑ ๋ถ€์—ฌ๋˜์ง€ ์•Š์Œ
    ๋”ฐ๋ผ์„œ ์˜์กด์„ฑ์ด ๋ถ€์—ฌ๋œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋งˆ๋‹ค null ๊ฒ€์‚ฌ ์ˆ˜ํ–‰ ํ•„์š”
    ๋˜ํ•œ ์˜์กด์„ฑ์ด ๋ณ€๊ฒฝ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์–ด ๋ถˆ๋ณ€์„ฑ ๋ณด์žฅ๋˜์ง€ ์•Š์Œ.

๐Ÿ™‹ Constructor-based or setter-based DI?

Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to use constructors for mandatory dependencies and setter methods or configuration methods for optional dependencies. Note that use of the @Autowired annotation on a setter method can be used to make the property be a required dependency; however, constructor injection with programmatic validation of arguments is preferable.

The Spring team generally advocates constructor injection, as it lets you implement application components as immutable objects and ensures that required dependencies are not null. Furthermore, constructor-injected components are always returned to the client (calling) code in a fully initialized state. As a side note, a large number of constructor arguments is a bad code smell, implying that the class likely has too many responsibilities and should be refactored to better address proper separation of concerns.

โžก๏ธ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋ฐฉ์‹ ์ฑ„ํƒ
spring ํŒ€์€ ์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ์‹ ์ง€์ง€
๊ทธ ์ด์œ ๋Š”, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋ถˆ๋ณ€ ๊ฐ์ฒด๋กœ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๊ณ  ํ•„์ˆ˜ ์ข…์†์„ฑ์ด null์ด ์•„๋‹ˆ๋„๋ก ๋ณด์žฅํ•จ

์ •๋ฆฌํ•˜์ž๋ฉด
ํ•„์ˆ˜ ์˜์กด์„ฑ์—๋Š” ์ƒ์„ฑ์ž ์ฃผ์ž…
์„ ํƒ ์˜์กด์„ฑ์—๋Š” ์„ธํ„ฐ ์ฃผ์ž… ์‚ฌ์šฉ


3. ํ•„๋“œ ์ฃผ์ž… (Field Injection)

public class MyClass {
	@Autowired
	private InjectionBean injectionBean;
}
  • ์ฝ”๋“œ๋Ÿ‰ ์ ๊ณ  ๊ฐ„๊ฒฐํ•˜๋‹ค๋Š” ์žฅ์  ์žˆ์Œ
  • ํ•„๋“œ ์ฃผ์ž… ๋ฐฉ์‹์€ ๋ฆฌํ”Œ๋ ‰์…˜์„ ์‚ฌ์šฉ โžก๏ธ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์‹œ Mock ๊ฐ์ฒด ์ฃผ์ž… ์–ด๋ ค์›€
  • ์žฌ์‚ฌ์šฉ์„ฑ ์ €ํ•˜
  • ์–ด๋–ค ์˜์กด์„ฑ์ด ํ•„์š”ํ•œ์ง€ ์ง๊ด€์ ์œผ๋กœ ๋ช…์‹œ๋˜์ง€ ์•Š์Œ. (์ˆจ๊ฒจ์ง„ ์˜์กด์„ฑ)

์ฐธ๊ณ 

https://edu.nextstep.camp/s/OiPrZU5t/ls/A3P44ALq
https://f-lab.kr/insight/core-of-spring-framework-dependency-injection-and-ioc-container?gad_source=1&gclid=CjwKCAjwhIS0BhBqEiwADAUhc-SxqhdB4uq3tEbb2gaHzbJH-t0TBTjpILuKLFnVq5fG3C8guawG8hoC1IQQAvD_BwE
https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html
https://spring.io/blog/2016/03/04/core-container-refinements-in-spring-framework-4-3

0๊ฐœ์˜ ๋Œ“๊ธ€