java - Accessing autowired beans in multiple classes -


my question how can access bean in class x, autowired in class y?

i'm using spring boot, have 2 controllers:

@controller public class ownercontroller {      @autowired     standarddaoaction<owner> ownerdao; 

and one

@controller public class visitcontroller {      @autowired     standarddaoaction<pet> petdao; 

now need add standarddaoaction ownerdao bean in visitcontroller class, when use autowired, exception, because multiple beans of same class defined , spring not know do. tried somehow distinguish them @qualifier, did not worked or messed something.

it's pretty basic question im stuck , cant find solution

ok ran conclusion core problem implementation of dao's , interface aka wrong usage of generics:

my interface :

public interface standarddaoaction<t> {     public t get(int id);     public void remove(int id);     public void add(t type);     public void update(t type);     public list<t> getall(); } 

implementation of interface both classes:

@repository public class petdaoimpl implements standarddaoaction<pet> {      @persistencecontext     entitymanager em;      @transactional     public pet get(int id) {         return em.find(pet.class, id);     }      @transactional     public void remove(int id) {         em.remove(id);     }      @transactional     public void add(pet pet) {         em.persist(pet);     }      @transactional     public void update(pet pet) {         em.merge(pet);     }      @suppresswarnings("unchecked")     @transactional     public list<pet> getall() {         return em.createquery("from pet").getresultlist();     }  }   @repository public class ownerdaoimpl implements standarddaoaction<owner> {      @persistencecontext     entitymanager em;      @transactional     public owner get(int id) {         return em.find(owner.class, id);     }      @transactional     public void remove(int id) {          em.remove(get(id));     }      @transactional     public void add(owner owner) {         em.persist(owner);     }      @transactional     public void update(owner owner) {         em.merge(owner);     }      @suppresswarnings("unchecked")     @transactional     public list<owner> getall() {         return em.createquery("from owner").getresultlist();     }  } 

when add:

@controller public class visitcontroller {      @autowired     standarddaoaction<pet> petdao;      @autowired     standarddaoaction<owner> ownerdao; 

this error:

org.springframework.beans.factory.beancreationexception: error creating bean name 'visitcontroller': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: pl.kaczynski.dao.standarddaoaction pl.kaczynski.controller.visitcontroller.ownerdao; nested exception org.springframework.beans.factory.nouniquebeandefinitionexception: no qualifying bean of type [pl.kaczynski.dao.standarddaoaction] defined: expected single matching bean found 3: ownerdaoimpl,petdaoimpl,vetdaoimpl 

there vetdaoimpl because there controller:

@controller public class vetlistcontroller {       @autowired     standarddaoaction<vet> vetdao; 

which implements interface, can see above.

so long have 1 bean implements standarddaoactions interface , it's working because goes autowired type, when add autowired bean in different controller , error occurs.

because of type erasure have 2 beans of type standarddaoaction.

  • inject either concrete type @autowired private petdaoimpl petdao
  • or use @autowired @qualifier("petdaoimpl") private standarddaoaction<pet> petdao

Comments