使用dagger2的简单示例
已更新
我有一个拥有Computer和waterpump类的Motor类。我这样写的是为什么Motor中的子注入部件为空?
这是我的运动类,使用startEngin方法检查计算机和waterPump启动。
public class Motor {
@Inject
public Computer computer;
@Inject
public WaterPump waterPump;
public Motor(){
}
// here computer and waterPump are null and not injected
public boolean startEngin(){
if(computer!=null && waterPump!=null){
return true;
}else{
return false;
}
}
}
这是具有型号名称和电压的计算机类:
public class Computer {
private int vultage;
private String model;
public Computer(String model ,int vultage){
this.model=model;
this.vultage = vultage;
}
}
这是WaterPump:
public class WaterPump {
private String name;
public WaterPump(String name){
this.name = name;
}
}
这是我的模块:
@Module
public class MotorModule {
Context context;
String motoName;
String computerName;
String waterPupName;
int voltage;
public MotorModule(Context context, String computerName, String waterPupName, int voltage) {
this.context = context;
this.waterPupName = waterPupName;
this.computerName = computerName;
this.voltage = voltage;
}
@Provides
@Singleton
Motor provideMotor() {
return new Motor();
}
@Provides
@Singleton
Computer provideComputer() {
return new Computer(computerName, voltage);
}
@Provides
@Singleton
WaterPump provideWaterPump() {
return new WaterPump(waterPupName);
}
@Provides
@Singleton
Context provideContext() {
return this.context;
}
}
这是我的组件类,我知道不需要getMotor方法。
@Singleton
@Component(modules = {MotorModule.class})
public interface MotorComponent {
// Motor getMotor();
void inject(MainActivity activty);
并且在活动中注入的电机为空:
public class MainActivity extends AppCompatActivity {
@Inject
public Motor motor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DaggerMotorComponent.builder().motorModule(new MotorModule
(this, "mahdi'PC", "my " +
"Water pump", 12)).build().inject(this);
if (motor.startEngin()) {
Toast.makeText(this, "it is started", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "motor is not provided", Toast.LENGTH_SHORT).show();
}
}
}
}
答案 0 :(得分:2)
一切看起来都不错,除了Motor类本身。你用@Inject注释了两个字段(所以Dagger现在知道它可以在那里注入东西),但你实际上从未要求Dagger用数据“填充”这些变量。 您应该明确要求在代码中的某处填写这些数据。 1种方法是通过'构造函数注入',这样你的构造函数应该是什么样的
public Motor(Computer computer, WaterPump waterPump) {
this.computer = computer;
this.waterPump = waterPump;
}
......并在模块中:
@Provides
@Singleton
Motor provideMotor(Computer computer, Waterpump waterpump) {
return new Motor(computer, waterpump);
}
或者你可以从你的Motor的构造函数中调用dagger实例并执行以下操作:
myDaggerInstance.inject(this);
并记得使用@Inject注释为Motor注释构造函数。
使用任何一种方法,你明确告诉Dagger在某个时间点完成这些依赖,所以它们不再是null。干杯!
答案 1 :(得分:1)
您需要保存对MotorComponent实例的引用,并使用它注入Activtiy和Motor类。 将void inject(Motor m)添加到组件中,并调用component.inject(myMotor),或者在Motor类上使用构造函数注入。
public class Motor {
Computer computer;
WaterPump waterPump;
public Motor( Computer computer, WaterPump waterPump){
this.computer = computer;
this.waterPump = waterPump;
}
// here computer and waterPump are null and not injected
public boolean startEngin(){
if(computer!=null && waterPump!=null){
return true;
}else{
return false;
}
}
}
/** Add method, providing Motor class instance - it will use another
.provide() methods from this component to get computer and waterpump objects
(Costtructor injection)
*/
/**
* Arguments is provided by DI
* @param computer
* @param waterPump
* @return
*/
@Provides
@Singleton
Motor provideMotors(Computer computer, WaterPump waterPump) {
Motor motor = new Motor(computer, waterPump);
return motor
}
/** Store reference to your component (better do it in Application) */
MotorComponent component = DaggerMotorComponent.builder().motorModule(new MotorModule
(this, "mahdi'PC", "my " +
"Water pump", 12)).build();
// inject into Activity
componen.inhject(this);
答案 2 :(得分:1)
@Provides
@Singleton
Motor provideMotor() {
return new Motor();
}
和
public class Motor {
@Inject
public Computer computer;
@Inject
public WaterPump waterPump;
public Motor(){
}
应该是以下之一:
1)
@Singleton
public class Motor {
@Inject
public Computer computer;
@Inject
public WaterPump waterPump;
@Inject
public Motor() {
}
和
public MotorModule(Context context, String computerName, String waterPupName, int voltage) {
this.context = context;
this.waterPupName = waterPupName;
this.computerName = computerName;
this.voltage = voltage;
}
//@Provides
//@Singleton
//Motor provideMotor() {
// return new Motor();
//}
或强>
2)。
public class Motor {
public Computer computer;
public WaterPump waterPump;
public Motor(Computer computer, WaterPump waterPump) {
this.computer = computer;
this.waterPump = waterPump;
}
和
@Provides
@Singleton
Motor provideMotor(Computer computer, WaterPump waterPump) {
return new Motor(computer, waterPump);
}