IoC(Inversion of Control) 제어의 역전

Yuno·2024년 8월 10일
0

JavaSpring

목록 보기
2/16

✏️Inversion of Control(IoC) 란?

프로그램의 흐름 제어를 개발자가 아닌 프레임워크나 컨테이너에 맡기는 설계패턴.
IoC의 핵심 아이디어는 객체의 생성, 관리, 그리고 의존성을 애플리케이션의 제어 흐름 외부에서 처리 하도록 하는것.

📌 기본 개념 :

  • 전통적인 제어 흐름 : 개발자가 직접 개체를 생성하고, 의존성을 설정하며, 객체 간의 관계를 관리
  • IoC 제어 흐름 : 프레임워크나 컨테이너가 객체를 생성하고, 의존성을 설정하며, 객체 간의 관계를 관리

전통적인 방식

public class MyService {
	private final MyRepository myRepository = new MyRepository();
}

IoC 방식

@Component
public class MyService {
	private final MyRepository myRepository;
    
    @Autowired
    public MyService(MyRepository myRepository) {
    	this.myRepository = myRepository;
    }
}

👉 IoC 방식에서는 MyServiceMyRepository 를 직접 생성하지 않고, 프레임워크가 의존성을 주입

profile
Hello World

0개의 댓글