我正在使用Spring 3和Hibernate 4

我有以下 class 结构

public interface GenericDAO<T> {

    public void create(T entity);
    public void update(T entity);
    public void delete(T entity);
}

DAO类
public interface EmployeeDAO extends GenericDAO<Employee>  {

    public void findEmployee(EmployeeQueryData data);
}

DAO实现类
@Repository("employeeDAO")
public abstract class EmployeeDAOImpl implements EmployeeDAO {

protected EntityManager entityManager;

@Override
public void findEmployee(EmployeeQueryData data) {

...... code

}

我面临的问题是当我尝试部署时,出现以下异常。
如果我从abstract中删除EmployeeDAOImpl并从extends GenericDAO<Employee>中删除EmployeeDAO,则应用程序将被正确部署。因此,不可能有abstractEmployeeDAOImpl类,或者我需要在没有GenericDAO的DAO实现中实现abstract的所有方法?
Error creating bean with
name 'employeeService': Injection of autowired dependencies failed; \
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: test.dao.EmployeeDAO
test.service.EmployeeServiceImpl.employeeDAO; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [test.dao.EmployeeDAO] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for
this dependency. Dependency annotations:
{@javax.inject.Inject()}.

编辑1

通用DAOImpl
public class GenericDAOImpl<T> implements GenericDAO<T> {

    public void create(T entity) {
    }
    public void update(T entity) {
    }
    public void delete(T entity) {
    }

EmployeeDAOImpl
public class EmployeeDAOImpl extends GenericDAOImpl<Employee> implements EmployeeDAO {

最佳答案

Java(因此是Spring)无法创建抽象类的实例:每个方法都必须具有一个实现,然后Java才能让您创建实例,否则在尝试调用该方法时会出现运行时错误。您需要从EmployeeDAOImpl中删除“摘要”,并实现从GenericDAO继承的方法。

关于java - 自动连接的依赖项的注入(inject)对于抽象类失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15269520/

10-10 13:55