Dagger 2依赖关系图不完整

桑达昂

我正在使用Dagger 2在我的新Android应用程序中注入依赖项。我想在服务中注入一个道。

模块:

@Module
public class DenkoStationModule {
    @Provides
    @Singleton
    public DenkoStationDao provideDenkoStationDao() {
        DaoMaster daoMaster = new DaoMaster(DenkoApplication.getDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Log.d("dao", "station dao created");
        return daoSession.getDenkoStationDao();
    }

    @Provides
    @Singleton
    public DenkoStationService provideDenkoStationService() {
        Log.d("service", "station service created");
        return new DenkoStationService();
    }
}

服务:

public class DenkoStationService {

    private DenkoStationDao denkoStationDao;

    @Inject
    public void setDenkoStationDao(DenkoStationDao denkoStationDao) {
        this.denkoStationDao = denkoStationDao;
    }

    public List<DenkoStation> fetchAllDenkoStations() {
        Log.d("service", "loading all");
        return denkoStationDao.loadAll();
    }
}

成分:

@Singleton
@Component(modules = {DenkoStationModule.class})
public interface DenkoStationComponent {

    DenkoStationService provideDenkoStationService();
}

我从这样的活动中使用它:

DenkoStationComponent denkoStationComponent = Dagger_DenkoStationComponent.builder().denkoStationModule(new DenkoStationModule()).build();
        DenkoStationService denkoStationService = denkoStationComponent.provideDenkoStationService();

        List<DenkoStation> denkoStations = denkoStationService.fetchAllDenkoStations();

看起来dao没有被注入。

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List org.bitbucket.infovillafoundation.denko.dao.DenkoStationDao.loadAll()' on a null object reference
            at org.bitbucket.infovillafoundation.denko.service.DenkoStationService.fetchAllDenkoStations(DenkoStationService.java:23)
            at org.bitbucket.infovillafoundation.denko.activity.HomeActivity.onCreate(HomeActivity.java:29)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

当使用依赖项注入解决方案时,您希望框架也注入依赖项的依赖项。是框架问题还是我做错了什么?

康拉德·克拉科维克(Konrad Krakowiak)

Dagger 2使用注释处理器如果要在对象中注入某些东西,则必须在使用实例之前调用负责注入的方法。您有初始化的地方Component吗?如果没有任何地方,则必须这样做。

在组件内部创建初始化器,如下所示:

@Singleton
@Component(modules = {DenkoStationModule.class})
public interface DenkoStationComponent {


    public final static class Initializer {

        public static HierarchyViewerComponent init() {
            return Dagger_DenkoStationComponent.builder()
                    .denkoStationModule(new DenkoStationModule()) //it is not necessary because DenkoStationModule doesn't have params
                    .build();
        }

        void inject(DenkoStationService deviceInfoProvider);

    }
 }

之后,您必须通过方法初始化Component:

DenkoStationComponent.Initializer.init();

应用程序对象是执行此操作的好地方。并且您必须保留此实例(它可以是静态的)。

现在,您可以注入dao对象,如下所示:

public class DenkoStationService {

   @Inject
   DenkoStationDao denkoStationDao;

    @Override
    public void onCreate() {
        super.onCreate();
        //lines below are responsible for inject your object

        //use line below if you keep component instance in some object 
        //ObjectWhichContainsComponent.methodToGetComponent().inject(this);
        //or
        DenkoStationComponent.Initializer.init().inject(this);

        //if you didn't initialise component before

    }

    public List<DenkoStation> fetchAllDenkoStations() {
        Log.d("service", "loading all");
        return denkoStationDao.loadAll();
    }
}

如有任何疑问,请在评论中提问。

在这里您可以找到更多信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在不完整的父级上对Dagger .plus()进行追溯

来自分类Dev

Dagger 2依赖关系是否可以注入?

来自分类Dev

Dagger 2子组件与组件依赖关系

来自分类Dev

Dagger 2的依赖注入

来自分类Dev

Dagger 2注入依赖链

来自分类Dev

依赖组件的 Dagger 注入

来自分类Dev

如何通过Dagger2提供GoogleApiClient依赖关系?

来自分类Dev

Dagger 2 Singleton 和 Activity 级别的依赖关系

来自分类Dev

Dagger2不生成Dagger *类

来自分类Dev

使用Dagger 2查看依赖项注入

来自分类Dev

在模块中添加依赖项-Dagger 2

来自分类Dev

Android Dagger2 依赖注入

来自分类Dev

如何避免 Dagger 2 的循环依赖?

来自分类Dev

Dagger 2 和依赖注入地狱?

来自分类Dev

Dagger2 - 跨模块依赖

来自分类Dev

Dagger 2活动注射无效

来自分类Dev

使用Dagger 2投入服务

来自分类Dev

构造函数注入Dagger 2

来自分类Dev

Dagger 2和界面实现

来自分类Dev

Dagger 2演示者注入

来自分类Dev

Android Dagger 2:注入与提供

来自分类Dev

构造函数注入Dagger 2

来自分类Dev

使用@Binds和@Inject字段的Dagger2依赖关系循环

来自分类Dev

如何在 Dagger2 中实例化我们的依赖图实例

来自分类Dev

Dagger 2 访问依赖模块中的命名依赖项

来自分类Dev

如何模拟Dagger活动对象图?

来自分类Dev

如何使用Dagger 2注入pojo依赖项?

来自分类Dev

Dagger 2 @ Component.Builder依赖项缺少setters错误

来自分类Dev

使用Dagger2进行内部依赖注入