我的开发广泛使用机器人腿绑定问题.我知道如何解决它与PrivateModule在吉斯,但目前尚不清楚如何做到这一点与Scala的蛋糕图案来完成.
有人可以解释一下如何做到这一点,理想情况下,根据Jonas Boner在博客文章末尾的咖啡示例中的具体例子?也许有一个可以配置为左侧和右侧的加热器,注入一个方向和一个def isRightSide?
dependency-injection scala guice cake-pattern robot-legs-problem
我有这个用例非常类似于Guice的机器人腿例子,除了我不知道我有多少"腿".因此,我无法使用机器人腿示例所需的注释.
我期望在带有Guice的Multibindings扩展的java.util.Set中收集所有这些"leg".
从技术上讲,在PrivateModule中,我想直接将实现作为Multibindings扩展提供的集合的元素公开.我只是不知道该怎么做.
有关参考和代码示例,请参阅此处的robot-legs示例:http://code.google.com/p/google-guice/wiki/FrequentlyAskedQuestions#How_do_I_build_two_similar_but_slightly_different_trees_of_objec
这是我的确切用例:
我有以下内容:
// Main application
public interface MyTree {...}
public interface MyInterface {
public MyTree getMyTree() {}
}
public abstract class MyModule extends PrivateModule {}
public class MyManager {
@Inject MyManager (Set<MyInterface> interfaces){ this.interfaces = interfaces }
}
public class MainModule extends AbstractModule {
public void configure () {
// Install all MyModules using java.util.ServiceLoader.
}
}
// In expansion "square.jar"
public class SquareTree implements MyTree {...}
public class SquareImplementation implements MyInterface { …Run Code Online (Sandbox Code Playgroud) 我是否需要创建一个新模块,并将接口绑定到不同的实现?
Chef newChef = Guice.createInjector(Stage.DEVELOPMENT, new Module() {
@Override
public void configure(Binder binder) {
binder.bind(FortuneService.class).to(FortuneServiceImpl.class);
}
}).getInstance(Chef.class);
Chef newChef2 = Guice.createInjector(Stage.DEVELOPMENT, new Module() {
@Override
public void configure(Binder binder) {
binder.bind(FortuneService.class).to(FortuneServiceImpl2.class);
}
}).getInstance(Chef.class);
Run Code Online (Sandbox Code Playgroud)
我无法触及Chef Class和Interfaces.我只是一个客户端在运行时绑定到Chef的FortuneService到不同的接口.
java dependency-injection inversion-of-control guice robot-legs-problem
我遇到的蛋糕模式的大多数示例似乎都将依赖关系视为单例类型服务; 在组件的最终组装中,每种类型只有一个实例.在使用Cake Pattern进行依赖注入时,是否可以编写具有多个特定类型实例的配置(可能以不同方式配置)?
请考虑以下组件.通用HTTP服务:
trait HttpService { def get(query:String):String }
trait HttpServiceComponent {
val httpService:HttpService
class HttpServiceImpl(address:String) extends HttpService {
def get(query:String):String = ...
}
}
Run Code Online (Sandbox Code Playgroud)
贸易和公司服务,每个服务都依赖于HttpService,它可能是不同的实例:
trait TradeService { def lastTrade(symbol:String):String }
trait TradeServiceComponent {
this:HttpServiceComponent => // Depends on HttpService
val tradeService:TradeService
class TradeServiceImpl extends TradeService {
def lastTrade(symbol:String):String =
httpService.get("symbol=" + symbol)
}
}
trait CompanyService { def getCompanySymbols(exchange:String):String }
trait CompanyServiceComponent {
this:HttpServiceComponent => // Depends on different HttpService instance
val companyService:CompanyService
class CompanyServiceImpl extends CompanyService …Run Code Online (Sandbox Code Playgroud)