是否可以为子类中的实例自动关联基类中定义的字段?
所以, 界面:
public interface Xyz { ...}
抽象基类:
public abstract class Abc {
Xyz xyz;
}
我要自动装配接口的具体实现的抽象类的子类:
public class Def extend Abc {
@Autowired
// Here I want to autowire a concrete implementation of Xyz, maybe called XyzImpl. Can I do this maybe in a constructor or ...?
}
public class Ghi extend Abc {
@Autowired
// Here I want to autowire a concrete implementation of Xyz, maybe called XyzImpl. Can I do this maybe in a constructor or ...?
}
public class Jkl extend Abc {
@Autowired
// Here I want to autowire a concrete implementation of Xyz, maybe called XyzImpl. Can I do this maybe in a constructor or ...?
}
答案 0 :(得分:1)
尝试一下:
Abc
类中,进行此更改private final Xyz xyz;
Abc
类中,添加一个带有Xyz
参数的构造函数;在此构造函数中设置xyz
字段。Def
类中,添加一个带有Xyz
参数并调用super(xyz)
的构造函数。xyz
构造函数中的Def
参数。