If you have one interface called A that has one method signature called print; Now if you have 3 classes implementing A and you call A.print how do you know which class method gets invoked. THERE IS NO NEWING OF AN OBJECT
public interface A()
{
public void print(){}
}
@Component
public class B implements A
{
public void print()
{
system.out.print("B");
}
}
@Component
public class c implements A
{
public void print()
{
system.out.print("C");
}
}
@Component
public class d implements A
{
public void print()
{
system.out.print("d");
}
}
public class runner()
{
@Autowired
private A aThing_;
aThing_.print();
}
An interface defines an interaction contract or, in other words, defines a set of methods that every class implementing that interface should provide.
Oracle’s answer to the question, What’s an Interface? is:
The invocation depends on the type of the object implementing the interface.
You’ll be invoking
B‘sprintmethod’s implementation for theprintmethod defined in theAinterface.EDIT: The point of an interface is defining the interaction with an object regardless of its actual type. That code seems to be the autoscanning of a group of components behind the same interface to show that you can define a set of different components to handle the same situation in a different way, given the context.
AFAIK the autowire defaults to the field’s name. You can define which interface implementation you want to inject with the
@Qualifier("CLASS_NAME_HERE")annotation alongside with@Autowire.You might want to check this.