尝试为Android使用匕首依赖注入器。
这是扩展应用程序类:
public class MyApplication extends Application {
private ObjectGraph objectGraph;
protected List<Object> getModules() {
List<Object> objectArrayList = new ArrayList<Object>();
objectArrayList.add(new AndroidModule(this));
objectArrayList.add(new DeviceModule());
return objectArrayList;
}
@Override
public void onCreate() {
super.onCreate();
objectGraph = ObjectGraph.create(getModules().toArray());
}
public void inject(Object object) {
objectGraph.inject(object);
}
}
我的模块:
安卓模块
@Module(library = true)
public class AndroidModule {
private final MyApplication myApplication;
public AndroidModule(MyApplication myApplication) {
this.myApplication = myApplication;
}
@Provides
@Singleton
@ForApplication
public Context provideApplicationContext() {
return myApplication.getApplicationContext();
}
@Provides
@Singleton
public LocationManager provideLocationManager() {
return (LocationManager)myApplication.getSystemService(Context.LOCATION_SERVICE);
}
@Provides
public NetworkInfo provideNetworkInfo() {
ConnectivityManager connectivityManager = (ConnectivityManager)myApplication.getSystemService(Context.CONNECTIVITY_SERVICE);
return connectivityManager.getActiveNetworkInfo();
}
}
设备模块
@Module(injects = MainActivity.class, complete = false, library = true)
public class DeviceModule {
@Provides
public SystemValidator provideSystemValidator() {
return new AndroidSystemValidator();
}
@Provides
public Device provideDevice(SystemValidator systemValidator) {
return new Device(systemValidator);
}
}
设备类
public class Device {
private final SystemValidator systemValidator;
@Inject
public Device(SystemValidator systemValidator) {
this.systemValidator = systemValidator;
}
public void validate() {
boolean isGPSTurnedOn = systemValidator.isGPS();
boolean isNetworkActive = systemValidator.isNetwork();
}
}
系统验证程序类
public class AndroidSystemValidator implements SystemValidator {
@Inject
LocationManager locationManager;
@Inject
NetworkInfo networkInfo;
@Override
public boolean isNetwork() {
return (null != networkInfo && (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE));
}
@Override
public boolean isGPS() {
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
}
这段代码编译成功,但在运行时,我尝试将对象注入objectgraph
java.lang.StackOverflowError
at com.xxxx.app.modules.system.DeviceModule$$ModuleAdapter$ProvideDeviceProvidesAdapter.get(DeviceModule$$ModuleAdapter.java:77)
at com.xxxx.app.modules.system.DeviceModule$$ModuleAdapter$ProvideDeviceProvidesAdapter.get(DeviceModule$$ModuleAdapter.java:113)
at com.xxxx.app.modules.system.DeviceModule$$ModuleAdapter$ProvideDeviceProvidesAdapter.get(DeviceModule$$ModuleAdapter.java:77)
at com.xxxx.app.modules.system.DeviceModule$$ModuleAdapter$ProvideDeviceProvidesAdapter.get(DeviceModule$$ModuleAdapter.java:113)
at com.xxxx.app.modules.system.DeviceModule$$ModuleAdapter$ProvideDeviceProvidesAdapter.get(DeviceModule$$ModuleAdapter.java:77)
at com.xxxx.app.modules.system.DeviceModule$$ModuleAdapter$ProvideDeviceProvidesAdapter.get(DeviceModule$$ModuleAdapter.java:113)
编辑:
代码被工作解决方案更改,但即使
java.lang.StackOverflowError
和MainActivity
对象被标记为LocationManager
,也为空。试图通过添加NetworkInfo
来告诉@Inject
将这些对象放置在何处,但没有成功。如何在不创建参数化construtor的情况下注入这些对象? 最佳答案
我想你的问题是
@Provides
public Device provideDevice(Device device) {
return device;
}
这个装置是从哪里来的?你需要在这一点上创建一个新实例。
对于您的设备和AndroidSystemValidator,我认为您应该使用构造函数注入它们。如果你想用匕首来做这件事
@Inject public Device(SystemValidator systemValidator) {
this.systemValidator = systemValidator;
}
否则,使用类似的模块执行您刚刚更改为
@Provides
public Device provideDevice(SystemValidator systemValidator) {
return new Device(systemValidator);
}