如何在 subclass 中为 base class 中定义的字段使用 @Autowired?

How to use @Autowired in subclass for field defined in base class?

是否可以在 subclass 中为在基础 class 中定义的字段自动装配实例?

所以, 界面:

public interface Xyz { ...}

抽象基础class:

public abstract class Abc {
    Xyz xyz;
}

抽象class 的子class(es),我想在其中自动装配接口的具体实现:

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 ...?
}

试试这个:

  1. Abc class 中进行此更改 private final Xyz xyz;
  2. Abcclass中,添加一个带有Xyz参数的构造函数;在此构造函数中设置 xyz 字段。
  3. Def class 中,添加一个带有 Xyz 参数并调用 super(xyz).
  4. 的构造函数
  5. Def 构造函数中自动装配 xyz 参数。