我正在努力将Dagger 2设置到我的android项目中.这是我第一次使用这个框架,到目前为止一切顺利.但是我在你的项目中设置这个框架的方式看到了不同的方法,我想知道哪一个更好,因为我比较两者,对我来说结果有点相同.
我遵循了这个指南:https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2
在互联网上搜索所有这些都使用这种方法.它使用@Module和@Component来定义依赖项.
你的申请最终如下:
public class MyApp extends Application {
private NetComponent mNetComponent;
@Override
public void onCreate() {
super.onCreate();
// Dagger%COMPONENT_NAME%
mNetComponent = DaggerNetComponent.builder()
// list of modules that are part of this component need to be created here too
.appModule(new AppModule(this)) // This also corresponds to the name of your module: %component_name%Module
.netModule(new NetModule("https://api.github.com"))
.build();
// If a Dagger 2 component does not have any constructor arguments for any of its modules,
// then we …Run Code Online (Sandbox Code Playgroud)