我尝试将我的Dao类使用的常用方法集中在一个Generic类中,如下所示:
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;
}
}
}
我的Dao课是这样写的:
@Repository
public class UsuarioHome extends Dao<Usuario> {
public UsuarioHome(Usuario entity) {
super(entity);
// TODO Auto-generated constructor stub
}
}
但是当我尝试运行应用程序时出现此错误:
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
任何人都可以说我的方法是否真的有效,如果是真的,我在这里错过了什么?
答案 0 :(得分:0)
您在UsuarioHome
中缺少一个无参数构造函数。添加一个,spring应该能够实例化它。
@Repository
public class UsuarioHome extends Dao<Usuario> {
public UsuarioHome() { //<---
super(null);
}
public UsuarioHome(Usuario entity) {
super(entity);
// TODO Auto-generated constructor stub
}
}
我建议的一件事是没有字段 - private final E entity;
。这真的没用。
此外,最好从DAO中取出交易设置并将其放入您的服务中。