我有一个要求,我需要使用泛型,因为客户端在使用我的服务时可以使用他们想要的数据类型。代码如下:
public interface MyService<T extends Base> {
void meth1(T type);
}
@Service("abc")
public class MyServiceImpl<T> implements MyService<T extends Base> {
@Override
public void meth1(T type) {
}
}
其中Base是一个抽象类。客户端将使用它的子类来调用此服务。
public abstract class Base {
}
问题:实现类(MyServiceImpl)无法编译。我不确定这里的问题。
客户端将使用此服务并推送其数据类型,这是Base的子类。例如:
@Autowired
@Qualifier("abc")
private MyService<xx> service;
service.meth1(xx) //where xx is subclass of Base
我在这里做了一些根本错误的事情。请建议。
答案 0 :(得分:3)
类型绑定应该在type参数的声明中:
public class MyServiceImpl<T extends Base> implements MyService<T>