I have a bean which implements two interfaces. The barebones code is as follows:
interface InterfaceA {
...
}
interface InterfaceB {
...
}
public class ClassC implements InterfaceA, InterfaceB {
...
}
In my AppConfig I am specifying the following:
@Bean(name = "InterfaceA")
public InterfaceA interfaceA() {
return new ClassC();
}
@Bean(name = "InterfaceB")
public InterfaceB interfaceB() {
return new ClassC();
}
And I use it so:
public class MyClass {
@Inject
private final InterfaceA a;
public MyClass(@Named("InterfaceA") InterfaceA a) {
this.a = a;
}
...
}
However, Spring complains that:
No qualifying bean of type [com.example.InterfaceA] is defined: expected single matching bean but found 2: InterfaceA, InterfaceB
Similar question was asked and answered for EJB here but I could not find anything for Spring beans. Anybody know the reason?
The workaround is to introduce a new interface which extends both InterfaceA
and InterfaceB
and then let ClassC
implement that. However, I am loath to change my design because of framework constraints.
ClassC
... The return type of the method doesn't matter here, as the bean will be bothInterfaceA
andInterfaceB
. You don't need an additional interface... – Chaucerian