In my application I am using generic DAO pattern. My class structure looks like this:
A domain class - pojo
package com.test.abc.def; @NamedQueries({------a couple of queries go here .......}) @Entity @Table(name = "NAME") public class Name implements Serializable { private String myName; ........ @Column(name = "MY_NAME", nullable = false, length = 30) public String getMyName() { return this.myName; } public void setName(String myName) { this.myName= myName; } ........ }A DAO interface that corresponds to the NamedQueries in the above Domain class
package com.test.abc.h; public interface INameDao extends IGenericDAO<...., ....> { ....method signature...... }A IGenericDAO interface
package com.test.abc.h; public interface IGenericDAO<T, PK extends Serializable> { ......... }GenericDAO implementation class
package com.test.abc.h.impl; @Repository @Scope( BeanDefinition.SCOPE_PROTOTYPE ) public class GenericDAO<T, PK extends Serializable> implements IGenericDAO<T, PK> { ....method implemnetations ........ }application context:
<context:component-scan base-package="com.test.abc" /> <context:annotation-config /> <mvc:annotation-driven /> <mvc:default-servlet-handler />A service class has the following autowire declarations:
@Autowired private Name name; @Autowired private INameDAO nameDAO; ...... nameDAO.callToNamedQuery() - here I would like to call the named query present in Name class.
I see the below error when I try to startup the server:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type [com.test.abc.h.INameDAO] 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)}
What am I missing here? I tried adding the @Repository annotation to the INameDAO - and to the Name domain class as well. That did not help. Also added @Component annotation to the Name domain class - that did not help either.
It looks like your
GenericDAOis of typeIGenericDAObut you're error suggests you are injecting a bean of typeINameDAO. WhilstINameDAOis of typeIGenericDAO, yourGenericDAOis not of typeINameDAO. You need to either fix the inheritance tree of yourGenericDAO, or by changing the type of the bean to be injected to include something in it's tree.EDIT
To provide an example. This is similar to defining java.util.Collection, java.util.List and java.util.Set. You then declare a bean that has a List property and try to inject a Set. Whilst they have a common ancestor, they are incompatible. You could change your bean to accept a Collection property, but you will be restricted to the methods of Collection.
With the structure below you would inject
MyActualDAOImplinto a property of typeMyActualDAO(I've left off the generics). Put yourNamedQueries on yourEntity`.Public interfaces:
Implementation