Dagger
下面主要分析,Dagger 中使用 @Provides 注解一个构造方法来提供依赖的过程。
举个栗子
老王再次开车去东北
还是那个老王
- Person
public class Person {
@Inject
Car car;
public void goDongbei(){
car.run();
}
}
一嗨租车
- PersonModule
@Module
public class PersonModule {
@Provides
public Car provideCar(){
return new Car();
}
}
车子使用合约
- PersonComponent
@Component(modules = PersonModule.class)
public interface PersonComponent {
void inject(Person person);
}
GTR
- Car
public class Car {
private static final String TAG = "Car";
public void run(){
Log.d(TAG, "run: ~~~~~~");
}
}