所以我想我最终想通了。如果您发现我的工作有问题,请发表评论。
接口的定义如下:
public interface Deserialiser<T> { T get(String content) throws IOException; List<T> getList(String content) throws IOException;}接口的实现是这样的…
public class DeserialiserImp<T> implements Deserialiser<T> { private ObjectMapper objectMapper = new ObjectMapper(); private final Class<T> klass; @Inject public DeserialiserImp(TypeLiteral<T> type){ this.klass = (Class<T>) type.getRawType(); } @Override public T get(String content) throws IOException { return objectMapper.readValue(content, klass); } @Override public List<T> getList(String content) throws IOException { return objectMapper.readValue(content, TypeFactory.collectionType(ArrayList.class, klass)); }}我像这样绑定2
bind(new TypeLiteral<Deserialiser<User>>(){}).annotatedWith(Names.named("user")).to(new TypeLiteral<DeserialiserImp<User>>(){});然后,我要做的就是使用它…
@Inject@Named("user")private Deserialiser<User> deserialiserImp;public void test(String userString) { User user = deserialiserImp.get(UserString);}如果该类作为要在DAO对象中使用的抽象类,则此模式也可以很好地工作
这篇文章对我有帮助



