如果没有@Inject构造函数,则无法提供Dagger 2对象

时间:2017-09-02 04:24:03

标签: java android dagger-2 dagger

我对Dagger 2很新,我有以下课程。

我有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);
    }
}

现在我遇到的问题是,如果我只在我的启动中注入DaoSession并注释掉GlobalPref impl它只会工作但是当我将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

我不能将两个对象都注入同一个活动的原因是什么?

1 个答案:

答案 0 :(得分:0)

必须从一个组件和一个组件完成注入。

应该很容易看到错误消息指出无法提供的对象是您尝试由另一个组件注入的对象。

Dagger没有进行“半”注射,一个组件必须注入所有字段。如果可以进行部分注射,最终可能会出现不一致的状态,因为Dagger无法知道如何,何时或何处注入其余的字段。简而言之,这是不可能的。您必须使用单个组件。

  

但是我很快就会有更多模块,我不知道是否有一个组件可以处理所有模块...

没关系。根据您的设置,您最终会得到相当多的模块和可能的相当多的组件。确保在适当的地方使用SubComponents,如果你有大的依赖关系组分成多个模块,你甚至可以让模块包含其他模块。

相关问题