刚开始使用Dagger 2,我对如何设置一切感到困惑.
我正在尝试注入一个POJO,但它总是为空.
首先,一些代码:
App.java
private AppComponent appComponent; @Override public void onCreate() { super.onCreate(); appComponent = DaggerAppComponent .builder() .appModule(new AppModule(this)) .build(); } public AppComponent component() { return appComponent; }
AppModule.java
@Module public class AppModule { private Application app; public AppModule(Application app) { this.app = app; } @Provides @Singleton public Application application() { return app; } }
AppComponent.java
@Singleton @Component(modules = AppModule.class) public interface AppComponent { void inject(App application); Application application(); }
NetworkingManager.java
@Singleton public class NetworkingManager { private Context ctx; @Inject public NetworkingManager(Context context) { this.ctx = context; } }
NetModule.java
@Module public class NetModule { @Provides @Singleton public NetworkingManager provideNetworkingManager(Application application) { return new NetworkingManager(application); } }
NetComponent.java
@Singleton @Component(modules = {NetModule.class},dependencies = {AppModule.class}) public interface NetComponent { void inject(NetworkingManager networkingManager); }
SomeClass.java
@Inject NetworkingManager networkingManager; public void doSomethingWithNetworkManager() { networkManager.doStuff(); }
我花了很多时间查看大量的教程,SO问题和示例,但我无法弄清楚我做错了什么.
我99%肯定我有一些设置错误,但我无法弄清楚是什么.
解决方法
根据您的评论,您希望在您的应用程序中随处可用NetworkingManager.
让我们从您对Component的定义开始:
@Singleton @Component(modules = AppModule.class) public interface AppComponent { void inject(App application); }
这告诉Dagger这个组件将注入App类.现在,您还可以告诉Dagger您想要注入的其他课程.因此,如果您还要注入一个Activity,例如你会添加:
@Singleton @Component(modules = AppModule.class) public interface AppComponent { void inject(App application); void inject(MainActivity activity) //Where MainActivity is a class that extends Activity. }
请注意,这不是IMO共享应用程序范围依赖关系的最佳方式;您应该创建一个继承自AppComponent的Component,并使AppComponent公开所需的共享依赖项.
现在让我们来看看你的模块类:
@Module public class NetModule { @Provides @Singleton public NetworkingManager provideNetworkingManager(Application application) { return new NetworkingManager(application); } }
在这里你是@Provideing一个NetworkingManager,没关系.您的NetworkingManager需要一个应用程序(真正的上下文),为什么不在NetworkingManager里面提供App,或者更好的是为什么不在AppModule中提供NetworkingManager,因为AppModule应该@Provide整个应用程序常见的东西:
@Module public class AppModule { private Application app; public AppModule(Application app) { this.app = app; } @Provides @Singleton public Application application() { return app; } @Provides @Singleton public NetworkingManager provideNetworkingManager(Application application) { return new NetworkingManager(application); } }
现在在你的App类中:
public class App extends Application { private AppComponent appComponent; @Override public void onCreate() { super.onCreate(); appComponent = DaggerAppComponent .builder() .appModule(new AppModule(this)) .build(); appComponent.inject(this); } public AppComponent component() { return appComponent; } }
在我们假设的MainActivity中:
public class MainActivity extends Activity { private AppComponent appComponent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); appComponent = ((App)getApplicationContext()).getAppComponent(); appComponent.inject(this); } }
您似乎没有正确使用@Component(dependencies = {…}).当您想要使用上面提到的机制将依赖关系从一个Component暴露给另一个Component时,会使用依赖关系.