1

Is it possible to autowire an instance in a subclass for a field which is fdefined in the base class?

So, The interface:

public interface Xyz { ...}

The abstract base class:

public abstract class Abc {
    Xyz xyz;
}

The subclass(es) of the absctract class where I want to autowire a concrete implementation of the interface:

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 ...?
}
du-it
  • 2,561
  • 8
  • 42
  • 80

1 Answers1

1

Try this:

  1. In the Abc class, make this change private final Xyz xyz;
  2. In the Abc class, add a constructor that takes an Xyz parameter; set the xyz field in this constructor.
  3. in the Def class, add a constructor that takes an Xyz parameter and which calls super(xyz).
  4. Autowire the xyz parameter in the Def constructor.
DwB
  • 37,124
  • 11
  • 56
  • 82