IOC (Inversion Of Control) 제어의 역전
뭔가 엄청 멋진말 같죠?…..그만큼 어렵겠죠?………
네 그렇게 쉽게 이해 할 수 있는 아이가 아닙니다 ^^ 그래서 지금 포스팅을 하는거죠 ㅎㅎㅎㅎ
(쉬우면 굳이 이렇게 정리를 할 필요가 없겠죠 ㅠ)
제어의 역전? 뭔소리야 그게…
자 한번 쉽게 생각해봅시다.
제어란 : 무언가 통제한다는것이죠?
역전 : 상황이 역전되다 할때 그 역전이죠?? 거꾸로됬다!!!
자 합쳐 봅시다.
<aside> 💡 “무언가를 통제하는게 역전 되었다.” ⇒ “제어를 하는 권한을 가진 주체가 바꼈다.”
</aside>
자 그렇다면 주체는 누구를 의미하는걸까요?
“라떼는 말이야….”
네 맞아요…옛날 시적에 개발자들은 “의존관계”의 제어를 직접 했습니다!
하지만 지금은 제어권이 “컨테이너”로 넘어가게 되었고, 객체의 생성과 생명주기 관리를 모두 컨테이너가 한다는거에요!
-제어의 역전
그렇다면 컨테이너는 뭐에요??
쉽게 말을 해주고 싶은데 이건 솔직히 Spring을 다루고 있다면, 어느정도 알거라고 생각해서 정확한 정의만 내리겠습니다!
Container
- 인스턴스(객체)의 생명주기를 관리하며, 생성된 인스턴스들에게 추가적인 기능들을 제공하는것.
- 객체의 생성과 소멸을 제어한다, 컨트롤한다, 담당한다.
자 여기까지 이해했다면, 제어의 역전은 이제 전혀 어럽지 않습니다!
이제부터 두루뭉실한 용어들의 정의를 내리는게 아닌 실제로 Spring에서의 제어의역전, 컨테이너에 대한 내용을 다룰것입니다!
IOC Container?
Spring에서는 DI, IOC의 개념을 사용하여 객체들의 생성과 생명주기를 담당하고 있습니다. 앞서 말했듯이,
생명주기, 객체 생성을 담당하는 아이를 Container라 부르며 Spring에서는 IOC container라고 부릅니다!!
IOC Container의 종류가 있다고??
- BeanFactory즉 bean(자바 객체) 등록, 생성, 조회, 반환을 관리합니다. bean 자체가 필요하지 않다면 인스턴스화 하지 않습니다!
- 객체를 생성하고, DI를 처리하는 기능만을 제공합니다 !
- ApplicationContextBaenFactory와 유사하지만 기존 BeanFactory보다 더 많은 기능을 제공해줍니다.하지만 핵심 기능은 이게 아닙니다. BeanFactory와 제일 큰 차이점은 ApplicationContext는 Context가 초기화 되는 시점에
- 미리 모든 빈들을 load합니다. 즉, 미리 Bean을 생성해 놓아서 필요할때 즉시 사용할 수 있어서 빠르다는거죠!
- 예를 들어, 이미지같은 파일들을 load 할 수 있는 포괄적인 방법을 알려준다든지요!
- 스프링이 제공하는 다양한 부가 기능이 있습니다!
마지막으로 정리!
즉 IOC는 제어의 권한이 개발자 ⇒ 컨테이너 로 바뀌게 되었고,
컨테이너가 하는 제어는 ⇒ 객체의 생성, 생명주기 관리 등등 이고,
이로 인해서 개발자들은 필요할때마다 직접 생성하거나, 필요없을때 소멸시킨다든지 그럴 필요 없이
필요할때 가져다가 쓰고 알아서 컨테이너가 생명주기를 관리 해주니 소멸 또한 자동으로 해주어서
객체 관리에 힘을 쏟을 필요없이, 프레임워크에 필요한 부품을 개발하고 조립하는 방식의 개발만 하면된다!!!!!
TMI :
“마틴 파울러” : 스프링에서 사용하는 경량 컨테이너들이 이야기 하는 IOC를 DI 라고 하는것이 바람직 하다.
뭔말이냐면, 사실 스프링에서 최초로 IOC의 개념을 사용한것은 아닙니다.
스프링의 가장 핵심적인 기능임에도, 그들이 최초로 만든건 아니였다는거죠 ㅎㅎ
개발자마다 다양한 방식과 용어로 분류를 하고 있으니, 주의해서 소통을 해야겠습니다!
(니가 맞네 내가 맞네 지랄하면 코딩할 시간이 줄어들잖아요 ^^)
'Spring & Spring Boot' 카테고리의 다른 글
Spring 에서 ajax 사용하기 (0) | 2023.01.16 |
---|---|
Spring bean(빈이란?) (0) | 2023.01.16 |
Spring Framework 파일업로드 하기 (2) | 2023.01.16 |
DI (Dependency Injection) 의존성 주입 (0) | 2023.01.16 |
Spring Interceptor 핸들러 가로채기 🙂 (4) | 2023.01.16 |