0
当
我尽量集中在一个通用类使用我的DAO类常用的方法,如下所示:错误在DAO类中使用泛型
public class Dao<E> {
private final E entity;
@Autowired
SessionFactory sessionFactory;
protected Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}
public Dao(E entity) {
this.entity = entity;
}
public E getEntity() {
return this.entity;
}
@Transactional
public boolean persist(E transientInstance) {
try {
sessionFactory.getCurrentSession().persist(transientInstance);
return true;
} catch (RuntimeException re) {
return false;
}
}
@Transactional
public boolean remove(E transientInstance) {
try {
sessionFactory.getCurrentSession().delete(transientInstance);
return true;
} catch (RuntimeException re) {
return false;
}
}
@SuppressWarnings("unchecked")
@Transactional
public E merge(E detachedInstance) {
try {
E result = (E) sessionFactory.getCurrentSession().merge(detachedInstance);
return result;
} catch (RuntimeException re) {
return null;
}
}
@SuppressWarnings("unchecked")
@Transactional
public E findById(int id) {
try {
E instance = (E) sessionFactory.getCurrentSession().get(entity.getClass(), id);
return instance;
} catch (RuntimeException re) {
return null;
}
}
@SuppressWarnings("unchecked")
@Transactional
public E findByUsername(String username) {
try {
E instance = (E) sessionFactory.getCurrentSession().createCriteria(entity.getClass(), username).add(Restrictions.like("login", username)).list().get(0);
return instance;
} catch (RuntimeException re) {
return null;
}
}
@SuppressWarnings("unchecked")
@Transactional
public List<E> findAll() {
try {
List<E> instance = sessionFactory.getCurrentSession().createCriteria(entity.getClass()).list();
return instance;
} catch (RuntimeException re) {
return null;
}
}
}
吾道类是这样写的:
@Repository
public class UsuarioHome extends Dao<Usuario> {
public UsuarioHome(Usuario entity) {
super(entity);
// TODO Auto-generated constructor stub
}
}
但是当我尝试运行应用程序我得到这个错误:如果我的做法确实WO
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.spring.webapp.lojavirtual.acesso.persistence.UsuarioHome com.spring.webapp.lojavirtual.acesso.service.AuthenticationService.accountDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usuarioHome' defined in file [/home/kleber/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webapp2/WEB-INF/classes/com/spring/webapp/lojavirtual/acesso/persistence/UsuarioHome.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.spring.webapp.lojavirtual.acesso.persistence.UsuarioHome]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.spring.webapp.lojavirtual.acesso.persistence.UsuarioHome.<init>()
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
... 48 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usuarioHome' defined in file [/home/kleber/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webapp2/WEB-INF/classes/com/spring/webapp/lojavirtual/acesso/persistence/UsuarioHome.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.spring.webapp.lojavirtual.acesso.persistence.UsuarioHome]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.spring.webapp.lojavirtual.acesso.persistence.UsuarioHome.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1007)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:912)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
... 50 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.spring.webapp.lojavirtual.acesso.persistence.UsuarioHome]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.spring.webapp.lojavirtual.acesso.persistence.UsuarioHome.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1000)
... 61 more
Caused by: java.lang.NoSuchMethodException: com.spring.webapp.lojavirtual.acesso.persistence.UsuarioHome.<init>()
at java.lang.Class.getConstructor0(Class.java:2810)
at java.lang.Class.getDeclaredConstructor(Class.java:2053)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)
... 62 more
任何人都可以说,如果这是真的,我在这里错过了什么?