lundi 27 mai 2019

How to use Spring Constructor DI in classes implementing an interface

So I want to achieve something like this::

@Component
public class ComponentA {}


@Component
public class ComponentB {}


public interface MyInterface

void doSomething();


public class MyInterfaceImplA implements MyInterface

private final componentA;

@Inject
public MyInterfaceImplA(ComponentA componentA){
this.componentA = componentA;
}

public void doSomething(){
    componentA.doThis();
}


public class MyInterfaceImplB implements MyInterface

private final componentB;

@Inject
public MyInterfaceImplB(ComponentB componentB){
this.componentB = componentB;
}

public void doSomething(){
    componentB.doThat();
}

What I basically want is to use different Components to execute things in the implementing classes. But as the components are different classes so this is not possible.

My question is if there is a good way to set this architecture up in this or a similar way? Or is there a pattern to achieve this in a better way?

Aucun commentaire:

Enregistrer un commentaire