我开发了一个带有spring mvc 4.的web应用程序,用于DAO层,我添加了一个类GenericDAO:`public class GenericDAO {
@PersistenceContext
private EntityManager entityManager;
private Class<T> parametrizedType;
public GenericDAO(Class<T> parametrizedType) {
this.parametrizedType = parametrizedType;
}
public EntityManager getEntityManager() {
return entityManager;
}
public List<T> executeQuery(String query){
TypedQuery<T> typedQuery = entityManager.createQuery(query, parametrizedType);
List<T> result = typedQuery.getResultList();
return result;
}
public List<T> listAll(){
List<T> result = getEntityManager().createQuery("Select t From " + parametrizedType.getSimpleName() + " t", parametrizedType).getResultList();
return result;
}
public T getById(Object id){
return getEntityManager().find(parametrizedType, id);
}
@SuppressWarnings("unchecked")
public List<T> executeNativeQuery(String query){
List<T> result = entityManager.createNativeQuery(query, parametrizedType).getResultList();
return result;
}
public List<T> executeNamedQuery(String query){
List<T> result = entityManager.createNamedQuery(query, parametrizedType).getResultList();
return result;
}
@Transactional
public void save(T entity){
entityManager.persist(entity);
}
@Transactional
public void modify(T entity){
entityManager.merge(entity);
}
@Transactional
public T find(Object id){
if(id!=null){
return entityManager.find(this.parametrizedType, id);
}
return null;
}
@Transactional
public void delete(Object id){
T t = find(id);
if(t!=null){
entityManager.remove(t);
}
}
}`
声明我的DAO如下所示在启动我的应用程序时产生错误:
@Autowired
@Qualifier(value="partenaireDao")
private GenericDAO<VscPartenaire> partenaireDao;
错误是:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency ....: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=partenaireDao)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1463) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1094) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056) 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
但是没关系:
@Repository("partenaireDao")
公共类PartenaireDao扩展了GenericDAO {
public PartenaireDao() {
super(VscPartenaire.class);
}
}
你可以告诉我第一次申报的错误吗? 我不希望每个实体创建dao。由于
答案 0 :(得分:0)
你可以告诉我第一次申报的错误吗?
你是不带注释的genericDao类,因此该类不会被spring实例化。
我不想为每个实体创建dao。
然后将LoggedInGuard
添加到@Repository("partenaireDao")
类。