2

基本上这两种方法合二为一:

Guice 可以根据参数自动创建不同类的实例吗?

Guice 只注入了一些构造函数

例如:用户操作处理程序是使用映射和辅助注入创建的,即每个都需要一些静态绑定以及从请求动态创建的操作上下文。

class OpHandler { void handle()... }
class SendHandler extends OpHandler {
    public SendHandler(DAL dal, SendContext context) { ... }
}
class ReceiveHandler extends OpHandler {
    public ReceiveHandler(DAL dal, ReceiveContext context) { ... }
}

servlet:
onGet() {
    op = resolveOp();
    switch (op) {
    SEND: create SendContext and pass it somehow to ioc.getInstance(SendHandler.class)
    RECEIVE: create ReceiveContext and pass it somehow to ioc.getInstance(ReceiveHandler.class)
    }
}
4

0 回答 0