Android Dagger Dependency Injection fails on private Fields

前端 未结 3 1401
既然无缘
既然无缘 2020-12-18 19:29

I\'m new to dagger (though I have experience with DI from working on Java EE WebApps using Weld).

What I\'m trying to do is to inject a dependency into a class. The

3条回答
  •  执笔经年
    2020-12-18 20:31

    Making a private field 'package visible' may not always be what you want. The Dagger documentation suggests the following:

    Injecting final fields and private members. For best performance Dagger generates code. Work around this by using constructor injection.

    Here's an example:

    private ItemFactoryImpl itemFactory;
    private BuildingFactory buildingFactory;
    
    @Inject
    public World(ItemFactoryImpl itemFactory, BuildingFactory buildingFactory) {
        this.itemFactory = itemFactory;
        this.buildingFactory = buildingFactory;
    }
    

提交回复
热议问题