Spring(来自版本4)声称支持泛型类型注入,我试图做这样的事情:
public abstract class AbstractControl<T extends IService> {
@Autowired
private T service;
protected T getService(){
return service;
}
public void setService(T service) {
this.service = service;
}
}
然后是这个类的扩展:
public class FooControl extends AbstractControl<LoginService> {
}
但是Spring试图注入IService
。是否可以注入继承的类型?
答案 0 :(得分:3)
尝试将@Autowired
移动到子类中,如下所示:
public abstract class AbstractControl<T extends IService> {
private T service;
protected T getService(){
return service;
}
public void setService(T service) {
this.service = service;
}
}
子类:
public class FooControl extends AbstractControl<LoginService> {
@Override
@Autowired
public void setService(LoginService service) {
this.service = service;
}
}