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
객체 간의 의존 관계를 자신이 아닌 외부의 조립기가 수행해준다는 개념이다.

실제로 의존하는 객체를 지정하는 가장 간단한 방법은 코드에 직저접 명시하는 것이다.


댓글

이 블로그의 인기 게시물

미해결 JEUS log error : deploy 작업 시 발생

SLF4J(Simple Logging Facade For Java) org.slf4j.Logger logback.xml

PLS-00215: 문자열 길이 제약은 (1 .. 32767)범위이어야 합니다