没有使用Dagger的单身人士

时间:2015-03-22 13:28:10

标签: java android singleton dagger

我刚刚在我当前的Android应用程序中开始使用Dagger,我不确定如何实现我正在寻找的功能(我相信我正在寻找的可能的)。

基本上,我的情况就是这样。我有以下课程,我希望他们的实例/注射由Dagger管理。我也希望其中一些是单身,由(1)指出:

  • CipherFactory - 简单的包私有工厂
  • CryptoService(1) - 依赖于CipherFactory
  • SerializationService(1) - 依赖于Jackson的ObjectMapper
  • PlayerApplicationService(1) - 依赖于SerializationService
  • PlayerRepository(1) - 依赖于SerializationService,CryptoService

现在,我可以让Dagger创建这些服务并适当地注入他们的依赖项;问题是某些类依赖于多个类,并且它通过日志记录语句显示,每次注入时我都会获得 new 实例。

以下是我目前对上述内容的看法,包括Dagger模块类:

CryptoService.java

@Singleton
public class CryptoService {
    private final CipherFactory cipherFactory;

    @Inject
    CryptoService(CipherFactory cipherFactory) {
        this.cipherFactory = cipherFactory;
    }
    ...
}   

CryptoServiceModule.java

@Module(
        injects = CryptoService.class,
        complete = true,
        library = true
)
public class CryptoServiceModule {

    @Provides
    @Singleton
    CipherFactory provideCipher() {
        return new CipherFactory();
    }

}

SerializationService.java

@Singleton
public class SerializationService {

    private final ObjectMapper objectMapper;

    @Inject
    SerializationService(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }
    ...
}

SerializationServiceModule.java

@Module(
        injects = SerializationService.class,
        library = true,
        complete = true
)
public class SerializationServiceModule {

    @Provides
    @Singleton
    ObjectMapper provideMapper() {
        return new ObjectMapper();
    }

}

PlayerApplicationService.java

public class PlayerApplicationService {

    private SerializationService serializationService;

    @Inject
    public PlayerApplicationService(SerializationService serializationService) {
        this.serializationService = serializationService;
    }
    ....
}

PlayerApplicationServiceModule.java

@Module(
        injects = PlayerApplicationService.class,
        includes = {
                SerializationServiceModule.class
        },
        complete = true,
        library = true
)
public class PlayerApplicationServiceModule {
}

PlayerRepository.java

@Singleton
public class PlayerRepository implements Repository<Player> {

    private final SerializationService serializationService;
    private final CryptoService cryptoService;

    @Inject
    public PlayerRepository(SerializationService serializationService, CryptoService cryptoService) {
        this.serializationService = serializationService;
        this.cryptoService = cryptoService;
    }
    ...
}

PlayerRepositoryModule.java

@Module(
        injects = PlayerRepository.class,
        includes = {
                SerializationServiceModule.class,
                CryptoServiceModule.class
        },
        complete = true,
        library = true
)
public class PlayerRepositoryModule {
}

我错过了什么?正如我上面提到的,所有这些东西都按原样运行,它看起来就像我得到了一些依赖项的多个实例(SerializationService,例如,它被两个类使用)在我只想要/需要的地方创建。

1 个答案:

答案 0 :(得分:1)

基于@Andyccs的评论,我已将所有内容整合到应用程序级Module类中,并删除了应用程序中的所有其他Module类:

<强> PlayerApplicationModule.java

@Module(
        injects = {
                PlayerApplication.class,
                CryptoService.class,
                SerializationService.class,
                PlayerApplicationService.class
        },
        complete = true,
        library = true
)
public class PlayerApplicationModule {
    private PlayerApplication application;

    public PlayerApplicationModule(PlayerApplication application) {
        this.application = application;
    }

    @Provides
    @Singleton
    public Context provideApplicationContext() {
        return application;
    }

    @Provides
    @Singleton
    public CipherFactory provideCipherFactory() {
        return new CipherFactory();
    }

    @Provides
    @Singleton
    public ObjectMapper provideObjectMapper() {
        return new ObjectMapper();
    }

}

现在一切都按预期工作了。