Probuje napisac helloroldowy mvc pet clinic.
Stworzylem sobie kilka encji: Owner, Pet, Vet, Visit, Phone.
Dla tych encji stworzyłem 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();
}
kazda z tych encji implementuje w/w interface, na przykladzie Owner.class:
@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();
}
}
pozniej stworzylem controllery:
@Controller
public class OwnerController {
@Autowired
StandardDaoAction<Owner> ownerDao;
@Controller
public class VisitController {
@Autowired
StandardDaoAction<Pet> petDao;
I teraz gdy do kontrollera VisitController dodam
@Autowired
StandardDaoAction<Owner> ownerDao;
dostaje Exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'visitController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: pl.kaczynski.dao.StandardDaoAction pl.kaczynski.controller.VisitController.ownerDao; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [pl.kaczynski.dao.StandardDaoAction] is defined: expected single matching bean but found 3: ownerDaoImpl,petDaoImpl,vetDaoImpl
dopoki istnieja tylko jedne definicje @Autowired dla beanow kazdej implementacji interfejsu, dopoty wszystko dziala, wiec myslalem ze to autowired by type, jest problemem i próbowalem to roziwazac @Qualifier, jednak to nie bangla.
W prostszym projekcie z jedna klasa encji , w kontrolerze moge sobie definiowac dowolna ilosc beanow tej samej klasy i smiga wszystko, wiec tutaj zaczalem podejrzewac przyczyne w generycznym interfejsie, koniec koncow zglupialem juz totalnie i prosze Was o pomoc z tym.