簡體   English   中英

如果沒有@Inject構造函數,則無法提供Dagger 2對象

[英]Dagger 2 Object cannot be provided without an @Inject constructor

我對Dagger 2相當陌生,並且具有以下課程。

我有2個模塊:

會話模塊

@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暗示,它將簡單地起作用,但是當我將GlobalPref與Daosession一起添加時,它無法構建,並顯示以下錯誤消息:

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)

並且生成的兩個類DaggerDaoSessionComponentDaggerGlobalPrefComponent都在生成對象中生成。

我不能將兩個對象都注入同一活動的原因可能是什么?

注射必須從一個組成部分,只有一個組件來完成。

應該很容易看到錯誤消息指出無法提供的對象是您嘗試由其他組件注入的對象。

Dagger不會“半”注入,並且一個組件必須注入所有字段。 如果可以進行部分注入,則最終可能會導致狀態不一致,因為Dagger無法知道如何,何時或何地注入其余字段。 簡而言之,這是不可能的。 您將必須使用單個組件。

但是很快我就會有更多的模塊,我不知道讓一個組件來處理所有模塊是否是一個好主意...

沒關系。 您最終會得到很多模塊,可能還有很多組件,具體取決於您的設置。 確保在適當的地方使用SubComponents,如果您將大型依賴項組划分為多個模塊,則甚至可以使模塊包含其他模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM