Saturday, 15 August 2015

spring dependency interface injection with two implemented classes -



spring dependency interface injection with two implemented classes -

consider scenario of interface injection in spring, have interface implemented 2 class. if inject interface in class using @autowired. if phone call method in interface class implemented method called? consider not using @qualifier annotation.

enter code here public interface employeedaoi{ void save(); } public class emp1 implements employeedaoi{ public void save(){ //some logic } } public class emp2 implements employeedaoi{ public void save(){ //some logic } }

now inject employeedaoi class

public class iterfaceex{ @autowired private employeedaoi edaoi; public void setemployeedaoi(employeedaoi edaoi){ this.edaoi=edaoi; } edaoi.save(); // question here class method called ? }

none. exception:

org.springframework.beans.factory.nosuchbeandefinitionexception: no unique bean of type [employeedaoi] defined: expected single matching bean found 2: [emp1 , emp2]

spring expects 1 instance, unless injection done collection of instances or utilize way of differentiating (@qualifier).

spring dependency-injection

No comments:

Post a Comment