是否有可能自動綁定泛型類? 考慮一下: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