I'm trying to inject a dao-bean in my controller from spring MVC. I'm using the generic dao pattern for the dao-objects.
For an unknown reason I receive this error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'klantController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private service.KlantDao controllers.KlantController.klantDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [service.KlantDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
This is the generic dao interface:
package service;
import java.util.List;
public interface GenericDao<T> {
public List<T> findAll();
public T update(T object);
public T get(Long id);
public void delete(T object);
public void insert(T object);
public boolean exists(Long id) ;
}
The generic dao class:
package service;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class GenericDaoJpa<T> implements GenericDao<T> {
private Class<T> type;
private EntityManager em;
public GenericDaoJpa(Class<T> type) {
super();
this.type = type;
}
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
@Transactional(readOnly = true)
@Override
public T get(Long id) {
T entity = this.em.find(this.type, id);
return entity;
}
@Transactional(readOnly = true)
@Override
public List<T> findAll() {
return this.em.createQuery(
"select entity from " + this.type.getName() + " entity").getResultList();
}
//@Transactional
@Override
public void insert(T object) {
em.persist(object);
}
//@Transactional
@Override
public void delete(T object) {
em.remove(em.merge(object));
}
@Transactional(readOnly = true)
@Override
public boolean exists(Long id) {
T entity = this.em.find(this.type, id);
return entity != null;
}
@Override
public T update(T object) {
return em.merge(object);
}
}
The concrete DAO I'm try to inject:
package service;
import domain.Klant;
public class KlantDao extends GenericDaoJpa<Klant>
{
public KlantDao()
{
super(Klant.class);
}
}
The controller class where I'm trying to inject the bean:
@Controller
public class KlantController
{
@Autowired
private KlantDao klantDao;
// route methods
public KlantDao getKlantDao() {
return klantDao;
}
public void setKlantDao(KlantDao klantDao) {
this.klantDao = klantDao;
}
}
In the dispatcher servlet I configure the bean like this:
<bean id="klantDao" class="service.KlantDao"/>
I think the problem is something with the autowiring. I've tried a lot of combinations of settings but I always receive the same error.
I hope someone can help. Thanks
您的代码正确,但是您需要添加@Services KlantDao
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.