2017-09-02 82 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,并且如果您将大型依赖关系组拆分为多个模块,则甚至可以让模块包含其他模块。

相关问题