๋ฐ˜์‘ํ˜•
DI
์„ค๋ช…

์˜์กด์„ฑ ์ฃผ์ž…์ด๋ผ๊ณ ๋„ ํ•˜๋ฉฐ, IoC (Inversion of Control) ๋ผ๊ณ  ํ•˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด ๋””์ž์ธ ํŒจํ„ด ์ค‘ ํ•˜๋‚˜.
์–ด๋–ค ํด๋ž˜์Šค๊ฐ€ ํ•„์š”๋กœ ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์™ธ๋ถ€์—์„œ ์ƒ์„ฑํ•œ ํ›„, ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๊ณผ์ •

DI ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ†ตํ•ด ๊ฐ ์ปดํฌ๋„ŒํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ†ตํ•ฉ ๊ด€๋ฆฌํ•˜๋ฉด์„œ ์ปดํฌ๋„ŒํŠธ๊ฐ„ ์˜์กด์„ฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

โœณ๏ธŽ ๋นˆ (@bean)
spring DI container์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋Š” ๊ฐ์ฒด 
ApplicationContext.getBean(XXX.class) ์œผ๋กœ ์–ป์–ด์ง€๋Š” ๊ฐ์ฒด๋ฅผ ์˜๋ฏธ


โœณ๏ธŽ ์ปดํฌ๋„ŒํŠธ (@Component)
์‚ฌ์šฉ์ž๊ฐ€ ๋งŒ๋“  bean ์„ ์ž๋™์œผ๋กœ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ ๊ฒƒ


โœณ๏ธŽ IoC ์ปจํ…Œ์ด๋„ˆ (Inversion of Control)
์˜์กด์„ฑ ์ฃผ์ž…์„ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋ฐ˜์ด๊ณ  ์ธ์Šคํ„ด์Šค ๊ตฌ์„ฑ ๋ฐ ๋นˆ ์กฐ๋ฆฝ์„ ๋‹ด๋‹นํ•œ๋‹ค.
๋นˆ ํŒฉํ† ๋ฆฌ(Bean Factory)๋ผ๊ณ ๋„ ํ•จ.
Bean Factory์— ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ปจํ…Œ์ด๋„ˆ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ApplicationContext๋ผ๊ณ  ๋ถ€๋ฆ„. (Bean Factory์˜ ํ™•์žฅ)


โœณ๏ธŽ Bean Factory
Bean ์„ ๋“ฑ๋ก/์ƒ์„ฑ/์กฐํ™”/๋ฐ˜ํ™˜ ๊ด€๋ฆฌํ•จ.


โœณ๏ธŽ ApplicationContext
spring bean์„ ๋ณด์œ ํ•˜๊ณ ์žˆ๋Š”๊ณณ
Bean Factory ์™€ ๊ธฐ๋Šฅ ๊ฐ™๊ณ , Spring์˜ ๋ถ€๊ฐ€ ์„œ๋น„์Šค๋ฅผ ์ถ”๊ฐ€๋กœ ์ œ๊ณต


์–ด๋–ค ์ปดํฌ๋„ŒํŠธ๋Š” ์‹ฑ๊ธ€ํ„ด(singleton) ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋ฉฐ, ์–ด๋–ค ์ปดํฌ๋„ŒํŠธ๋Š” ํ”„๋กœํ† ํƒ€์ž…(prototype) ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ์ธ์Šคํ„ด์Šค ์Šค์ฝ”ํ”„(scope) ๊ด€๋ฆฌ๋ฅผ DI ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋Œ€์‹  ํ•ด์ค€๋‹ค.

 

DI ๋ฐฉ๋ฒ• 3๊ฐ€์ง€

1. ์„ค์ •์ž ๊ธฐ๋ฐ˜ (setter-based)
  setter method ์— @Autowired ์„ ์ ์šฉ

@RestController
public class AController {
    private A a;

    @Autowired
    public void setA(A a) {
	    this.a = a;
    }
}


2. ์ƒ์„ฑ์ž ๊ธฐ๋ฐ˜ (contructor-based)
  ์ƒ์„ฑ์ž ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ์‹

@RestController
public class AController {
    private final A a;
    
    public AController(A a){
        this.a = a;
    }
}

 

3. ํ•„๋“œ๊ธฐ๋ฐ˜ (field-based)
  ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ํ•„๋“œ์— @Autowired ์„ ์ ์šฉ 

@RestController
public class AController {
    
    @Autowired
    private A a;
}

 

โœณ๏ธŽ @Autowired
๋นˆ์„ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ์‹

 

ํ•„๋“œ ๊ธฐ๋ฐ˜ ๋ฐฉ์‹์€ ์ถ”์ฒœํ•˜์ง€ ์•Š๋Š”๋‹ค. (์ƒ์„ฑ์ž ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์„ ๊ถŒ์žฅ)
์ถ”์ฒœํ•˜์ง€ ์•Š๋Š” ์ด์œ ๋Š” ์ˆœํ™˜์ฐธ์กฐ ๋•Œ๋ฌธ! 

โœณ๏ธŽ ์ˆœํ™˜์ฐธ์กฐ
์„œ๋กœ ๋‹ค๋ฅธ ํด๋ž˜์Šค๊ฐ€ ์„œ๋กœ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ์ƒํƒœ, ๋”ฐ๋ผ์„œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์–ด๋–ค ๋นˆ์„ ๋จผ์ € ์ƒ์„ฑํ•ด์•ผํ• ์ง€ ํŒ๋‹จ์„ ํ•˜์ง€ ๋ชปํ•ด์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ!

์ƒ์„ฑ์ž ๋ฐฉ์‹์„ ์ถ”์ฒœํ•˜๋Š” ์ด์œ ๋Š” ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ์— ์šฉ์˜ํ•˜๋ฉฐ, ์‹คํ–‰ ์ค‘ ๊ฐ์ฒด๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  

 

DI ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์šฉ์˜ ์žฅ์ 
์žฅ์ 

  ์ธ์Šคํ„ด์Šค ์Šค์ฝ”ํ”„ ์ œ์–ด
  ์ธ์Šคํ„ด์Šค ์ƒ๋ช…์ฃผ๊ธฐ ์ œ์–ด
  AOP ๋ฐฉ์‹์œผ๋กœ ๊ณตํ†ต๊ธฐ๋Šฅ ์ ์šฉ ๊ฐ€๋Šฅ
  ์˜์กดํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถฐ ๋‹จ์œ„ํ…Œ์ŠคํŠธ ์‰ฝ๊ฒŒ ๊ฐ€๋Šฅ

 

 

๋นˆ ์„ค์ • ๋ฐฉ๋ฒ•
์„ค์ • ๋ฐฉ๋ฒ• 3๊ฐ€์ง€

1. ์ž๋ฐ” ๊ธฐ๋ฐ˜ ์„ค์ • ๋ฐฉ์‹ (Java-based configuration)
  - ์ž๋ฐ” ํด๋ž˜์Šค์— @Configuration, ๋ฉ”์†Œ๋“œ์— @Bean ์„ ์‚ฌ์šฉํ•ด ๋นˆ์„ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•

2. XML ๊ธฐ๋ฐ˜ ์„ค์ • ๋ฐ˜์‹ (XML-based configuration)
  - XML ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• (<bean>, <constructor-arg>, <property>)

3. ์• ๋„ˆํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜ ์„ค์ • ๋ฐฉ์‹ (Annotation-based configuration)
  - @Component ๊ฐ™์€ ๋งˆ์ปค ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ€์—ฌ๋œ ํด๋ž˜์Šค๋ฅผ ํƒ์ƒ‰ํ•ด์„œ(Component Scan) IoC์ปจํ…Œ์ด๋„ˆ์— ๋นˆ์„ ์ž๋™์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•.

 

 

์ปดํฌ๋„ŒํŠธ ์Šค์บ” (Component Scan)
์„ค๋ช…

ํด๋ž˜์Šค ๋กœ๋”(Class Loader)๋ฅผ ์Šค์บ”ํ•˜๋ฉด์„œ ํŠน์ • ํด๋ž˜์Šค๋ฅผ ์ฐพ์€ ๋‹ค์Œ, DI ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•

 

์ข…๋ฅ˜

@Component, @Controller, @Service, @Repository, @Configuration, @RestController, @ControllerAdvice, @ManagedBean, @Named ..

 

 


์ฐธ๊ณ 

(์ฑ…) ์Šคํ”„๋ง ์ฒ ์ € ์ž…๋ฌธ
http://blog.naver.com/PostView.nhn?blogId=gngh0101&logNo=221179100057&parentCategoryNo=&categoryNo=32&viewDate=&isShowPopularPosts=false&from=postView
https://dog-developers.tistory.com/12
https://ict-nroo.tistory.com/120
https://www.baeldung.com/spring-component-annotation
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-basics

๋ฐ˜์‘ํ˜•

'๊ฐœ๋ฐœ > spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

ํŠธ๋žœ์žญ์…˜  (0) 2021.11.25
Spring - ์Šคํ”„๋ง MVC ๊ฐ„๋‹จ ์ •๋ฆฌ  (0) 2021.06.05
Spring - AOP(Aspect-Oriented Programming)  (0) 2021.05.25
IOC/DI, DI์‘์šฉ  (0) 2017.11.20
spring ์‹œ์ž‘, maven ์„ค์น˜  (0) 2017.11.12

+ Recent posts