2017-09-02 156 views
1

我對Dagger 2相當陌生,而且我有以下類。沒有@Inject構造函數就不能提供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,它會簡單地工作,但是當我將GlobalPref與Daos一起添加時分裂國家它不能建立並給了我以下錯誤消息:

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在構建foloder產生。

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

+0

您是否嘗試過使用兩個模塊的單個組件? –

+0

@RuwankaMadhushan我想到了,但是我很快就會有更多的模塊,我不知道是否有一個組件處理所有模塊是一個好主意......我不明白爲什麼這不會工作 –

回答

0

注射必須從一個組件和一個組件只。

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

匕首不做「半」注射,一個組件必須注入所有的字段。如果可以進行部分注射,那麼最終可能會出現不一致的狀態,因爲Dagger無法知道注入剩餘字段的方式,時間或位置。總之,這是不可能的。您將不得不使用一個單個組件組件。

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

沒關係。根據您的設置,最終會有相當多的模塊和可能的相當多的組件。確保在適當的情況下使用SubComponents,並且如果您將大型依賴關係組拆分爲多個模塊,則甚至可以讓模塊包含其他模塊。

相關問題