0

我正在尝试从 spring MVC 在我的控制器中注入一个 dao-bean。我正在为 dao 对象使用通用 dao 模式。

由于未知原因,我收到此错误:

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)}

这是通用的 dao 接口:

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) ;
}

通用 dao 类:

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);
    }
}

我尝试注入的具体DAO:

package service;
import domain.Klant;

public class KlantDao extends GenericDaoJpa<Klant>
{
    public KlantDao()
    {
        super(Klant.class);
    }
}

我试图注入 bean 的控制器类:

@Controller
public class KlantController 
{
    @Autowired
    private KlantDao klantDao;

// route methods

    public KlantDao getKlantDao() {
        return klantDao;
    }

    public void setKlantDao(KlantDao klantDao) {
        this.klantDao = klantDao;
    }
}

在调度程序 servlet 中,我像这样配置 bean:

<bean id="klantDao" class="service.KlantDao"/>

我认为问题出在自动装配上。我尝试了很多设置组合,但总是收到相同的错误。

我希望有人能帮帮忙。谢谢

4

1 回答 1

0

您的代码是正确的,但您需要添加 @Services KlantDao

于 2013-12-07T12:58:35.177 回答