基本例子
定义一个模块(依赖关系模型及其图形):
@Module
public class CoffeeModule{
@Provides
public CoffeeMaker provideCoffeeMaker(){
return new CoffeeMaker();
}
@Provides
public Coffee provideCoffee(CoffeeMaker coffeeMaker){
return new Coffee(coffeeMaker);
}
}
定义组件:
@Component(
modules={
CoffeeModule.class
}
)
interface CoffeeComponent {
DeveloperActivity inject(DeveloperActivity developerActivity);
}
注入依赖项:
class DeveloperActivity extends ...{
@Inject
Coffee myCoffee;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DaggerCoffeeComponent.builder()
.coffeeModule(new CoffeeModule())
.build()
.inject();
}
}