【发布时间】:2018-06-21 17:11:25
【问题描述】:
我想尝试在我的项目中使用 Guice,但遇到了简单(从我的角度来看)的问题。
假设我有接口
public interface TradeService {
public boolean buy(Player player, ProductID productId);
}
它有几个实现与它的依赖关系:
public CarsTradeService implements TradeService {
//...implementation here...
}
public BoatsTradeService implements TradeService {
//...implementation here...
}
public AirplanesTradeService implements TradeService {
//...implementation here...
}
我了解如何配置实现并为它们提供所需的依赖项 - 为此我需要创建 guice "modules",它看起来像
public class CarsTradeModule extends AbstractModule {
@Override
protected void configure() {
bind(TradeService.class).to(CarsTradeService.class).in(Singleton.class);
}
}
和类似的模块来休息两个服务。好的,模块已构建。但后来,当我需要将此实现注入某个类时 - 我如何才能注入完全需要的实现?
例如,如果我需要获取 CarsTradeService 的实例 - 我如何才能准确地获取此实例?
【问题讨论】:
-
你尝试了什么,什么没用?
标签: java dependency-injection guice