Spring无法在控制器中设置dao bean

时间:2013-12-07 12:18:46

标签: spring hibernate dependency-injection hibernate-generic-dao

我正在尝试从spring MVC在我的控制器中注入一个dao-bean。我正在为dao-objects使用泛型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"/>

我认为这个问题与自动装配有关。我尝试了很多设置组合,但总是收到同样的错误。

我希望有人可以提供帮助。 感谢

1 个答案:

答案 0 :(得分:0)

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