Jacob
Jacob

Reputation: 14741

Injection of autowired dependencies failed for abstract class

I am using Spring 3 and Hibernate 4

I have the following class structure

public interface GenericDAO<T> {

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

DAO class

public interface EmployeeDAO extends GenericDAO<Employee>  {

    public void findEmployee(EmployeeQueryData data);
}

DAO Implementation class

@Repository("employeeDAO")
public abstract class EmployeeDAOImpl implements EmployeeDAO {

protected EntityManager entityManager;

@Override
public void findEmployee(EmployeeQueryData data) {

...... code

}

The problem I am facing is when I try to deploy, I am getting the following exception. If I remove abstract from EmployeeDAOImpl and remove extends GenericDAO<Employee> from EmployeeDAO then application gets deployed without errors. So it is not possible to have abstract class for EmployeeDAOImpl or I have need to implement all methods of GenericDAO in DAO implementation without 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()}.

Edit 1

GenericDAOImpl

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 {

Upvotes: 4

Views: 3391

Answers (3)

maurocchi
maurocchi

Reputation: 688

Why do you want to declare a class implementation as abstract? Conceptually it's a contradiction. Obviously Spring cannot instantiate it and fails.

Upvotes: 2

Stephen Nelson
Stephen Nelson

Reputation: 939

Java (and consequently Spring) cannot create instances of abstract classes: every method must have an implementation before Java will let you create an instance, otherwise you would get a runtime error when you tried to call the method. You need to remove "abstract" from EmployeeDAOImpl and implement the methods inherited from GenericDAO.

Upvotes: 3

Jeevan Patil
Jeevan Patil

Reputation: 6089

Confirm if your EmployeeDAOImpl or other annotated class packages are mentioned in spring context xml in following tag. Unless this is done, annotations won't get read and will not be initialized.

<context:component-scan base-package="com.app.service" />

Upvotes: 1

Related Questions