Posts /

의존성 주입(DI)이란?

19 Apr 2017

DI(Dependency Injection)

의존적 주입

Reference전달에 따른 DI

DI diagram
Dependency Connect

DI유형

a. 생성자를 이용(Constructor Injection)

class MovieLister...  
    public MovieLister(MovieFinder finder) {
        this.finder = finder;       
    }
class ColonMovieFinder...  
    public ColonMovieFinder(String filename) {
        this.filename = filename;
    }

b. setter() Method를 이용(Setter Injection)

// Spring Container
class MovieLister {...  
    private MovieFinder finder;
    public void setFinder(MovieFinder finder) {
        this.finder = finder;
    }
class ColonMovieFinder...  
    public void setFilename(String filename) {
        this.filename = filename;
    }
}

c.초기화 인터페이스를 이용(Interface Injection)

// Avalon
public interface InjectFinder {  
    void injectFinder(MovieFinder finder);
}

class MovieLister implements InjectFinder...  
    public void injectFinder(MovieFinder finder) {
        this.finder = finder;
    }
    
public interface InjectFinderFilename {  
    void injectFilename (String filename);
}

class ColonMovieFinder implements MovieFinder, InjectFinderFilename......  
    public void injectFilename(String filename) {
        this.filename = filename;
    }

참고 : http://choiwy.tistory.com/


Twitter Facebook Google+