是否有可能自动绑定泛型类? 考虑一下:guice:自动绑定泛型类
通用接口:
public interface IAction<T> {
T echo(T inst);
}
龙子类型:
public class LongAction implements IAction<Long> {
@Override
public Long echo(Long inst) {return inst;}
}
字符串亚型:
public class StringAction implements IAction<String> {
@Override
public String echo(String inst) {return inst;}
}
自定义模块: 公共类CustomModule扩展AbstractModule {
@Override
protected void configure() {
//do this automagically?
bind(new TypeLiteral<IAction<String>>() {}).to(StringAction.class);
bind(new TypeLiteral<IAction<Long>>() {}).to(LongAction.class);
//
}
}
主要
// i know i can obtain the correct instance
IAction<String> is = injector.getInstance(new Key<IAction<String>>(){});
是否可以自动绑定,以某种方式(如:基地抽象类,反射或其他)StringAction
和LongAction
类的绑定?我试过使用反射无济于事。
你想如何自动发现'LongAction','StringAction'等?他们是在一个特定的包或什么?如果两个班实施'IAction''怎么办? –
2014-11-15 22:43:10
@TavianBarnes他们在一个特定的包。我不介意假设现在没有两个同样的'T',所以不会有两个'IAction' –
2014-11-16 10:29:14