[Spring] IOC and DI

kyle Kim·2021년 12월 26일
0

Spring

목록 보기
2/4

IOC(Inversion of Control) Container

'new' 등 을 사용하지 않고 외부에서 의존성(객체라 생각하면 쉬움)을 가지고 오는것을 말한다.

Spring Bean

간단히 말하면 Object와 같은데 이 Object가 Spring Container에 의해 만들어졌을때 Spring Bean이라 부른다.

DI(Dependency Injection)

객체(Bean)를 직접 생성해서 사용하는게 아니라 외부에 만들어져있는 객체를 불러오는 스프링의 의존 관계 주입 기능이다. 모듈간의 의존성을 낮춰줌으로써 유연성을 높여준다.
3가지 방법이 있는데
1. Constructor Injection

public class Test{
private Example example;
@Autowired
public void setData(Example example){
this.example = example;
}

2.Field Injection

public class Test{
@Autowired
private Example example;
}

3.. Setter Injection

public class Test{
private Example example;
public Test(Example example){
this.example = example;
}

위와 같은 3가지 방법으로 사용할 수 있지만, 권장되는 방법은 1번인 Constructor Injection 방법이다.

profile
가고일(gagoil)의 개발일지

0개의 댓글