Galera criei um DAo Generico conforme abaixo e ele dá o seguinte erro:
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@16477d9] to prepare test instance [com.diebold.saude.db.dao.test.TestUnidadeDao@f864fe]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.diebold.saude.db.dao.test.TestUnidadeDao': Injection of resource fields failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.diebold.saude.db.dao.imp.UnidadeDao] is defined: Unsatisfied dependency of type [class com.diebold.saude.db.dao.imp.UnidadeDao]: expected at least 1 matching bean
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessAfterInstantiation(CommonAnnotationBeanPostProcessor.java:262)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:978)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:291)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:127)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:36)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:515)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1031)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:888)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.diebold.saude.db.dao.imp.UnidadeDao] is defined: Unsatisfied dependency of type [class com.diebold.saude.db.dao.imp.UnidadeDao]: expected at least 1 matching bean
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveDependency(AbstractAutowireCapableBeanFactory.java:417)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:384)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:463)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:123)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:61)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessAfterInstantiation(CommonAnnotationBeanPostProcessor.java:259)
... 16 more
IGenericDao:
public interface IGenericDao <T, PK extends Serializable> {
public Class<T> getObjectClass();
public List<T> findAll();
public T findByKey(PK id);
}
GenericDao
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public abstract class GenericDao<T, PK extends Serializable> extends HibernateDaoSupport implements IGenericDao<T, PK>{
private EntityManager entityManager;
private final Class<T> oClass;//object class
@SuppressWarnings("unchecked")
public GenericDao() {
this.oClass = (Class<T>)
( (ParameterizedType) getClass().getGenericSuperclass() ).
getActualTypeArguments()[0];
}
public Class<T> getObjectClass() {
return this.oClass;
}
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.entityManager = em;
}
protected EntityManager getEntityManager() {
if (entityManager == null)
throw new IllegalStateException("Erro ");
return entityManager;
}
@SuppressWarnings("unchecked")
public List<T> findAll(){
List<T> lstT = null;
try {
String queryS = "SELECT obj FROM "+oClass.getSimpleName()+" obj";
Query query = getEntityManager().createQuery(queryS);
lstT = query.getResultList();
}catch(IllegalStateException e){
System.out.println("["+this.getClass().getName()+"](findAll):[IllegalStateException]=" + e.getMessage());
}catch(IllegalArgumentException e){
System.out.println("["+this.getClass().getName()+"](findAll):[IllegalArgumentException]=" + e.getMessage());
}catch(QueryException e){
System.out.println("["+this.getClass().getName()+"](findAll):[QueryException]=" + e.getMessage());
}catch(Exception e){
System.out.println("["+this.getClass().getName()+"](findAll):[Exception]=" + e.getMessage());
}
return lstT;
}
@Override
public T findByKey(PK id) {
T obj = null;
try{
obj = (T) getEntityManager().find(oClass, id);
} catch(IllegalStateException e){
System.out.println("["+this.getClass().getName()+"](findByPK):[IllegalStateException]=" + e.getMessage());
}catch(IllegalArgumentException e){
System.out.println("["+this.getClass().getName()+"](findByPK):[IllegalArgumentException]=" + e.getMessage());
}catch(Exception e){
System.out.println("["+this.getClass().getName()+"](findByPK):[Exception]=" + e.getMessage());
}
return obj;
}
}
IUnidadeDao
public interface IUnidadeDao extends IGenericDao<Unidade, Integer>{
}
UnidadeDao
public class UnidadeDao extends GenericDao<Unidade,Integer> implements IUnidadeDao {
}
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/jdbc.properties"/>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SaudeDBPU" />
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false"/>
<property name="generateDdl" value="false"/>
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/>
</bean>
</property>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven />
<bean id="unidadeDao" class="com.diebold.saude.db.dao.imp.UnidadeDao" autowire="byName"/>
</beans>
Alguém pode me ajduar, please ???