在我看到的有限例子之外,我很难绕过如何使用Dagger 2.0.我们来看一个阅读应用程序的例子.在这个阅读应用程序中,有一个用户的故事库和登录的能力.为了这个例子,感兴趣的类是:
MainApplication.java - 扩展申请
LibraryManager.java - 经理,负责在用户的库中添加/删除故事.这是从MainApplication
AccountManager.java - 经理负责保存所有用户的登录信息.它可以从LibraryManager中调用
我仍然试图围绕我应该创建的组件和模块.这是我到目前为止可以收集的内容:
创建一个HelperModule提供AccountManager和LibraryManager实例:
@Module
public class HelperModule {
@Provides
@Singleton
AccountManager provideAccountManager() {
return new AccountManager();
}
@Provides
@Singleton
LibraryManager provideLibraryManager() {
return new LibraryManager();
}
}
Run Code Online (Sandbox Code Playgroud)
创建一个MainApplicationComponent列出HelperModule其模块列表:
@Singleton
@Component(modules = {AppModule.class, HelperModule.class})
public interface MainApplicationComponent {
MainApplication injectApplication(MainApplication application);
}
Run Code Online (Sandbox Code Playgroud)
包含@Injects LibraryManager libraryManager在MainApplication应用程序中并将应用程序注入图形中.最后,它查询注入LibraryManager的库中的故事数:
public class MainApplication extends Application {
@Inject LibraryManager libraryManager;
@Override
public void onCreate() {
super.onCreate();
component = DaggerMainApplicationComponent.builder()
.appModule(new AppModule(this))
.helperModule(new HelperModule())
.build();
component.injectApplication(this);
// Now that we have an injected LibraryManager instance, use it
libraryManager.getLibrary();
}
}
Run Code Online (Sandbox Code Playgroud)
注入AccountManager到LibraryManager
public class LibraryManager {
@Inject AccountManager accountManager;
public int getNumStoriesInLibrary() {
String username = accountManager.getLoggedInUserName();
...
}
}
Run Code Online (Sandbox Code Playgroud)
但问题是AccountManager当我尝试使用它时,它是null,我LibraryManager不明白为什么或如何解决问题.我认为这是因为MainApplication注入图中的不直接使用AccountManager,但是我是否需要将LibraryManager图注入到图中?
小智 7
修改你的类如下,它会工作:
你的POJO:
public class LibraryManager {
@Inject AccountManager accountManager;
public LibraryManager(){
MainApplication.getComponent().inject(this);
}
public int getNumStoriesInLibrary() {
String username = accountManager.getLoggedInUserName();
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
你的组件接口:
@Singleton
@Component(modules = {AppModule.class, HelperModule.class})
public interface MainApplicationComponent {
void inject(MainApplication application);
void inject(LibraryManager lm);
}
}
Run Code Online (Sandbox Code Playgroud)
你的申请类:
public class MainApplication extends Application {
private static MainApplicationComponent component;
@Inject LibraryManager libraryManager;
@Override
public void onCreate() {
super.onCreate();
component = DaggerMainApplicationComponent.builder()
.appModule(new AppModule(this))
.helperModule(new HelperModule())
.build();
component.injectApplication(this);
// Now that we have an injected LibraryManager instance, use it
libraryManager.getLibrary();
}
public static MainApplicationComponent getComponent(){return component;}
}
Run Code Online (Sandbox Code Playgroud)
实际上,您需要对所有依赖类执行相同的操作,基本上您可以访问所有Activity子类中的应用程序类,因此将get组件作为静态方法是无所不在的.但是对于POJO,你需要以某种方式捕获组件.有很多方法可以实施.这只是一个例子,让你知道它是如何工作的.现在你可以摧毁火星:)
| 归档时间: |
|
| 查看次数: |
2812 次 |
| 最近记录: |