Guice - 相同 return 类型的多个提供程序方法
Guice - Multiple provider methods for same return types
在我的模块中:
@Provides
@Singleton
public ExecutorService provideExecutorService(){
return new ExecutorService(config);
}
在我的 class:
@Inject
private ExecutorService executorService;
现在,我需要两个具有不同配置的 ExecutorService 实例:
@Provides
@Singleton
public ExecutorService provideExecutorServiceA(){
return new ExecutorService(configA);
}
@Provides
@Singleton
public ExecutorService provideExecutorServiceB(){
return new ExecutorService(configB);
}
如果我想在 class 中为使用 configB 创建的 ExecutorService 注入 ExecutorService,我该怎么做?
您可以使用 binding annotations :
@Provides
@Singleton
@Priority(Priority.Level.HIGH)
public static ReportingDal createHighPriorityReportingDal(DataSource dataSource,
DatabaseType databaseType,
ReportingQueryGenerator queryGenerator)
{
return new ReportingDalImpl(dataSource, databaseType, Queue.DEFAULT, queryGenerator);
}
@Provides
@Singleton
@Priority(Priority.Level.LOW)
public static ReportingDal createLowPriorityReportingDal(DataSource dataSource,
DatabaseType databaseType,
ReportingQueryGenerator queryGenerator)
{
return new ReportingDalImpl(dataSource, databaseType, Queue.MAINTENANCE, queryGenerator);
}
@Priority 注释如下所示:
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@BindingAnnotation
public @interface Priority
{
Level value();
public enum Level
{
LOW,
HIGH
}
}
请注意,如果您不想实现自定义注释,您可以使用内置 Guice 的 @Named annotation。然后你可以像这样在你的 类 中使用它:
@Inject
public ReportingJob(@Priority(Priority.Level.LOW) ReportingDal dal)
{
this.dal = dal;
}
编辑
或者,如果您通过私有字段注入:
@Inject
@Priority(Priority.Level.LOW)
private ReportingDal dal;
在我的模块中:
@Provides
@Singleton
public ExecutorService provideExecutorService(){
return new ExecutorService(config);
}
在我的 class:
@Inject
private ExecutorService executorService;
现在,我需要两个具有不同配置的 ExecutorService 实例:
@Provides
@Singleton
public ExecutorService provideExecutorServiceA(){
return new ExecutorService(configA);
}
@Provides
@Singleton
public ExecutorService provideExecutorServiceB(){
return new ExecutorService(configB);
}
如果我想在 class 中为使用 configB 创建的 ExecutorService 注入 ExecutorService,我该怎么做?
您可以使用 binding annotations :
@Provides
@Singleton
@Priority(Priority.Level.HIGH)
public static ReportingDal createHighPriorityReportingDal(DataSource dataSource,
DatabaseType databaseType,
ReportingQueryGenerator queryGenerator)
{
return new ReportingDalImpl(dataSource, databaseType, Queue.DEFAULT, queryGenerator);
}
@Provides
@Singleton
@Priority(Priority.Level.LOW)
public static ReportingDal createLowPriorityReportingDal(DataSource dataSource,
DatabaseType databaseType,
ReportingQueryGenerator queryGenerator)
{
return new ReportingDalImpl(dataSource, databaseType, Queue.MAINTENANCE, queryGenerator);
}
@Priority 注释如下所示:
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@BindingAnnotation
public @interface Priority
{
Level value();
public enum Level
{
LOW,
HIGH
}
}
请注意,如果您不想实现自定义注释,您可以使用内置 Guice 的 @Named annotation。然后你可以像这样在你的 类 中使用它:
@Inject
public ReportingJob(@Priority(Priority.Level.LOW) ReportingDal dal)
{
this.dal = dal;
}
编辑
或者,如果您通过私有字段注入:
@Inject
@Priority(Priority.Level.LOW)
private ReportingDal dal;