Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
dagger_2 [2021/02/07 04:05] – removed ledyxdagger_2 [2021/02/07 05:50] (current) – old revision restored (2018/12/24 15:10) ledyx
Line 1: Line 1:
 += Dagger 2 =
  
 +https://google.github.io/dagger/
 +
 +# 주입이 필요한 Interface와 그 Interface를 구현한 Class 작성. 이 때, 생성자 주입이 필요한 경우 생성자에 @Inject.
 +## Module(객체 공급자)을 작성한다. 각 함수는 "provide"라는 접두사를 붙인다.
 +# 1에서 작성한 각 Bean을 이용하는 Context역할. 즉, 각 Bean을 주입받을 Class를 작성한다.
 +## Component(객체를 사용할 수 있도록 다리 역할을 하는 연결자)을 작성한다. 이 때 2에서 작성한 Class Type의 Setter 혹은 Getter를 작성한다.
 +
 +{{tag>Framework Dependency_Injection}}
 +
 +Dagger 공식 홈에 있는 CoffeeMaker 예제 단순화.
 +
 += pom.xml =
 +<sxh xml ; collapse:true>
 +...
 +
 +    <dependencies>
 +        <dependency>
 +            <groupId>com.google.dagger</groupId>
 +            <artifactId>dagger</artifactId>
 +            <version>${dagger.version}</version>
 +        </dependency>
 +    </dependencies>
 +
 +    <build>
 +        <plugins>
 +            <plugin>
 +                <groupId>org.apache.maven.plugins</groupId>
 +                <artifactId>maven-compiler-plugin</artifactId>
 +                <version>3.6.1</version>
 +                <configuration>
 +                    <annotationProcessorPaths>
 +                        <path>
 +                            <groupId>com.google.dagger</groupId>
 +                            <artifactId>dagger-compiler</artifactId>
 +                            <version>${dagger.version}</version>
 +                        </path>
 +                    </annotationProcessorPaths>
 +                </configuration>
 +            </plugin>
 +        </plugins>
 +    </build>
 + 
 +...
 +</sxh>
 +
 += 주입 할 대상 =
 +
 +커피 머신이 작동하려면 커피 물을 끓일 Heater, 달여진 커피를 받을 Pump가 필요.
 +
 +
 +* Heater
 +
 +<sxh java>
 +public interface Heater {
 +    void on();
 +    void off();
 +    boolean isHot();
 +}
 +</sxh>
 +
 +<sxh java>
 +public class MyHeater implements Heater {
 +    private boolean heating;
 +
 +    public void on() {
 +        System.out.println("~ ~ ~ heating ~ ~ ~");
 +        this.heating = true;
 +    }
 +
 +    public void off() {
 +        this.heating = false;
 +    }
 +
 +    public boolean isHot() {
 +        return heating;
 +    }
 +}
 +</sxh>
 +
 +
 +* Pump
 +
 +<sxh java>
 +public interface Pump {
 +    void pump();
 +}
 +</sxh>
 +
 +<sxh java ; highlight:[4]>
 +public class MyPump implements Pump {
 +    private final Heater heater;
 +
 +    @Inject
 +    public MyPump(Heater heater) {
 +        this.heater = heater;
 +    }
 +
 +    public void pump() {
 +        if (heater.isHot()) {
 +            System.out.println("=> => pumping => =>");
 +        }
 +    }
 +}
 +</sxh>
 +
 +
 +== Module ==
 +
 +각 함수에 "provide" 접두사를 붙인다.
 +
 +<sxh java ; highlight:[1,4,9]>
 +@Module
 +public class CoffeeMakerModule {
 +
 +    @Provides
 +    Heater provideHeater() {
 +        return new MyHeater();
 +    }
 +
 +    @Provides
 +    Pump providePump(MyPump pump) {
 +        return pump;
 +    }
 +}
 +</sxh>
 +
 +
 +
 += 주입 받을 대상 =
 +
 +<sxh java ; highlight:[2,5,8]>
 +public class CoffeeMaker {
 +    @Inject
 +    Heater heater;
 +
 +    @Inject
 +    Pump pump;
 +
 +    @Inject
 +    public CoffeeMaker(Heater heater, Pump pump){
 +        this.heater = heater;
 +        this.pump = pump;
 +    }
 +
 +    public void brew(){
 +        heater.on();
 +        pump.pump();
 +        System.out.println("brew!");
 +        heater.off();
 +    }
 +}
 +</sxh>
 +
 +== Component ==
 +
 +<sxh java ; highlight:[1,3]>
 +@Component(modules = CoffeeMakerModule.class)
 +public interface CoffeeComponent {
 +    CoffeeMaker maker(); // getter 작성
 +}
 +</sxh>
 +
 +
 += Context =
 +
 +먼저 Build를 하여 DI가 적용된 코드가 생성되도록 한다.
 +
 +<sxh>
 +public class CoffeeApp {
 +    public static void main(String... args) {
 +        CoffeeComponent coffeeComponent = DaggerCoffeeComponent.create();
 +        coffeeComponent.maker().brew();
 +    }
 +}
 +</sxh>
 +
 +<code>
 +~ ~ ~ heating ~ ~ ~
 +=> => pumping => =>
 +brew!
 +</code>
dagger_2.1612670727.txt.gz · Last modified: by ledyx