Dependency injection
5.4.1. Dependency injection
Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other object instance with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method.DI exists in two major variants,
Constructor-based dependency injection
public class SimpleMovieListener { // the simpleMovieListener has a dependency on a MovieFinder private MovieFinder movieFinder; // a constructor so that the Spring container can 'inject' a MovieFinder public SimpleMovieListner(MovieFinder movieFinder) { this.movieFinder = movieFinder; // business logic that actually 'uses' the injected MovieFinder is omitted... } |
Setter-based dependency injection
public class SimpleMovieListener { // the simpleMovieListener has a dependency on the MovieFinder private MovieFinder movieFinder; // a setter method so that the Spring contaner can 'inject' a MovieFinder public void setMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; // business logic that actually 'uses' the injected MovieFinder is omitted... } |
3. Dependency injection과 Spring Framework
객체 간의 의존 관계를 자신이 아닌 외부의 조립기가 수행해준다는 개념이다.
실제로 의존하는 객체를 지정하는 가장 간단한 방법은 코드에 직저접 명시하는 것이다.
댓글
댓글 쓰기