1 분 소요

Spring은 @Autowired 어노테이션을 이용한 다양한 의존성 주입 방법을 제공한다.

@Autowired 어노테이션은 Spring에게 의존성을 주입하라는 지시자 역할로 쓰이는데 생성자, 필드, setter에 붙일 수 있다.

※ Spring IoC 컨테이너에 의한 의존성 주입은 빈(Bean) 끼리만 가능하다.

생성자 주입

@Component
public class SampleController {
	private SampleRepository sampleRepository;
 
	@Autowired
	public SampleController(SampleRepository sampleRepository) {
		this.sampleRepository = sampleRepository;
	}
}

이렇게 생성자에 @Autowired 어노테이션을 붙여 의존성을 주입받을 수 있다.

Spring 4.3부터는 클래스의 생성자가 하나이고 그 생성자로 주입받을 객체가 빈으로 등록되어 있다면 생성자 주입에서 @Autowired를 생략할 수 있다.

필드주입

@Component
public class SampleController {
	@Autowired
	private SampleRepository sampleRepository;
}

변수 선언부에 @Autowired 어노테이션을 붙인다.

Setter 주입

@Component
public class SampleController {
	private SampleRepository sampleRepository;
 
	@Autowired
	public void setSampleRepository(SampleRepository sampleRepository) {
		this.sampleRepository = sampleRepository;
	}
}

Setter 메소드에 @Autowired 어노테이션을 붙인다. 이 세개의 코드는 모두 동일하게 SampleController에 SampleRepository를 주입하도록 한다.

생성자, 필드, Setter 주입 중 어떤 방법을 택할 것인가?

Spring framework reference에서 권장하는 방법은 생성자를 통한 주입이다.

생성자를 사용하는 방법이 좋은 이유는 필수적으로 사용해야하는 의존성 없이는 인스턴스를 만들지 못하도록 강제할 수 있기 때문이다. SampleController가 SampleRepository 없이는 제대로 동작할 수 없다면 SampleController 입장에서 SampleRepository는 반드시 있어야 하는 객체이다.

그것을 강제할 수 있는 가장 좋은 방법이 생성자 주입 방법을 쓰는것이다.

필드, Setter 주입 방법의 필요성

순환 참조(Circular Dependency)

A가 B를 참조하고 B가 A를 참조하는 상태

A 클래스와 B 클래스가 순환 참조 관계이고 둘 다 생성자 주입을 사용한다면 A와 B중 어떤 인스턴스도 생성할 수 없고 결과적으로 어플리케이션이 실행조차 되지 않는다.

가급적이면 순환 참조를 피하는게 좋지만 어쩔수 없는 상황이라면 필드나 setter 주입 방법을 사용할 수 있다.

태그:

카테고리:

업데이트: