In my spring-based project I have a core module ('core') with a class
@Component public class Superclass { // stuff } instances of which are injected by type throughout the code like this:
public class AService { @Autowired private Superclass superclass; // service stuff } I also have two other modules that depend on the core module and one of which (let's call it 'module1') extends Superclass:
@component public class Subclass extends Superclass { // overridden stuff } The other module ('module2') uses Superclass as is.
Now I want that when I compile and run 'child1' an instance of Subclass is used everywhere an instance of Superclass is expected. So I write a configuration class:
@Configuration public class Module2Configuration { @Bean public Superclass superclass(){ return new Subclass(); } } When I run this I see both Superclass and Subclass instantiated which is definitely not what I want. How do specify in 'module1' which type Spring should instantiate?