我是dagger2的新手,我有以下类。
我有2个模块:
daoSessionModule
@Module
public class DaoSessionModule {
private DaoSession daoSession;
private Context context;
public DaoSessionModule(Context context) {
this.context = context;
if(daoSession == null) {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this.context, "my_pocket");
Database db = helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
}
}
@Provides
LanguageDao providesLanguageDao() {
return daoSession.getLanguageDao();
}
@Provides
CategoryDao providesCategoryDao() {
return daoSession.getCategoryDao();
}
}
和 globalprefmodule
@Module
public class GlobalPrefModule {
private GlobalPref globalPerf;
public GlobalPrefModule(GlobalPref globalPerf) {
this.globalPerf = globalPerf;
}
@Provides
public GlobalPref providesGlobalPref() {
return this.globalPerf;
}
}
和它们的组件如下:
@Singleton
@Component(modules = {DaoSessionModule.class})
public interface DaoSessionComponent {
void inject(SplashActivity activity);
}
和
@Singleton
@Component(modules = {GlobalPrefModule.class })
public interface GlobalPrefComponent {
void inject(SplashActivity activity);
}
我在应用程序类中构建了这两个:
daoSessionComponent = DaggerDaoSessionComponent.builder()
.daoSessionModule(new DaoSessionModule(this))
.build();
globalPrefComponent = DaggerGlobalPrefComponent.builder()
.globalPrefModule(new GlobalPrefModule(new GlobalPref()))
.build();
并将它们注入我的飞溅活动:
public class SplashActivity extends BaseActivity {
@Inject
LanguageDao languageDao;
@Inject
GlobalPref globalPerf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initInjections();
}
private void initInjections() {
ZoopiApplication.app().getDaoSessionComponent().injectDao(this);
ZoopiApplication.app().getGlobalPrefComponent().injectGlobalPref(this);
}
}
现在,我面临的问题是,如果我只注入飞溅并评论GlobalPref Imps Imps,它将工作起作用,但是当我与DaoSession一起添加GlobalPref时,它就无法构建并给我以下内容。错误消息:
Error:(8, 52) error: cannot find symbol class DaggerDaoSessionComponent
Error:(9, 52) error: cannot find symbol class DaggerGlobalPrefComponent
Error:(16, 10) error: mypocket.com.zoopi.GlobalPref cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
mypocket.com.zoopi.GlobalPref is injected at
mypocket.com.zoopi.activities.SplashActivity.globalPerf
mypocket.com.zoopi.activities.SplashActivity is injected at
mypocket.com.zoopi.dagger.dagger2.components.DaoSessionComponent.injectDao(activity)
Error:(16, 10) error: mypocket.com.zoopi.models.LanguageDao cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
mypocket.com.zoopi.models.LanguageDao is injected at
mypocket.com.zoopi.activities.SplashActivity.languageDao
mypocket.com.zoopi.activities.SplashActivity is injected at
mypocket.com.zoopi.dagger.dagger2.components.GlobalPrefComponent.injectGlobalPref(activity)
和两个生成的类 daggerdaosessionComponent 和 daggerglobalprefcomponent 是在构建foloder中生成的。
我不能将两个对象注入相同活动的原因是什么?
必须从一个组件中进行注入,并且只有一个一个 component。
应该很容易看到错误消息指出无法提供的对象是您尝试注入另一个组件的对象。
dagger不进行"半"注射,一个组件必须注入所有磁场。如果有可能部分注射,您最终可能会出现不一致的状态,因为dagger无法知道如何,何时或在哪里注入其余领域。简而言之,这是不可能的。您必须使用单组件。
但是,我很快就会有更多模块,我不知道有一个组件来处理所有模块是一个好主意...
没关系。您最终将获得大量模块,并可能取决于您的设置。确保在适当的情况下使用子组件,如果您的依赖项分为多个模块,则可以将模块包含其他模块。