没有使用 Dagger 获得单例
Not getting singletons using Dagger
我刚刚开始在我当前的 Android 应用程序中使用 Dagger,我不确定如何实现我正在寻找的功能(我相信我正在寻找什么 是可能)。
基本上我的情况是这样的。我有以下 classes 我想让它们的 instances/injections 由 Dagger 管理。我还希望其中一些是单例,由 (1) 指出:
- CipherFactory - 简单的包私有工厂
- CryptoService (1) - 依赖于 CipherFactory
- SerializationService (1) - 依赖于 Jackson 的 ObjectMapper
- PlayerApplicationService (1) - 依赖于 SerializationService
- PlayerRepository (1) - 依赖于 SerializationService、CryptoService
现在,我可以让 Dagger 创建这些服务并适当地注入它们的依赖关系;问题是一些 classes 依赖于多个 class 并且通过日志记录语句显示每次注入时我都会得到 new 个实例.
这是我目前拥有的上述内容,包括 Dagger 模块 classes:
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
,它被两个 classes) 在我只有 want/need 一个的地方创建。
根据@Andyccs 的评论,我已将所有内容合并到一个应用程序级模块 class 并删除了应用程序中的所有其他模块 class:
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();
}
}
现在一切正常。
我刚刚开始在我当前的 Android 应用程序中使用 Dagger,我不确定如何实现我正在寻找的功能(我相信我正在寻找什么 是可能)。
基本上我的情况是这样的。我有以下 classes 我想让它们的 instances/injections 由 Dagger 管理。我还希望其中一些是单例,由 (1) 指出:
- CipherFactory - 简单的包私有工厂
- CryptoService (1) - 依赖于 CipherFactory
- SerializationService (1) - 依赖于 Jackson 的 ObjectMapper
- PlayerApplicationService (1) - 依赖于 SerializationService
- PlayerRepository (1) - 依赖于 SerializationService、CryptoService
现在,我可以让 Dagger 创建这些服务并适当地注入它们的依赖关系;问题是一些 classes 依赖于多个 class 并且通过日志记录语句显示每次注入时我都会得到 new 个实例.
这是我目前拥有的上述内容,包括 Dagger 模块 classes:
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
,它被两个 classes) 在我只有 want/need 一个的地方创建。
根据@Andyccs 的评论,我已将所有内容合并到一个应用程序级模块 class 并删除了应用程序中的所有其他模块 class:
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();
}
}
现在一切正常。