问题
I am try centralize common methods used by my Dao classes in one Generic class, listed below:
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;
}
}
}
My Dao class are written this way:
@Repository
public class UsuarioHome extends Dao<Usuario> {
public UsuarioHome(Usuario entity) {
super(entity);
// TODO Auto-generated constructor stub
}
}
But I am getting this error when I try run the application:
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
Anyone can say if my approach really works, and if true, what I missing here??
回答1:
You are missing a no-arg constructor in your UsuarioHome. Add one and spring should be able to instantiate it.
@Repository
public class UsuarioHome extends Dao<Usuario> {
public UsuarioHome() { //<---
super(null);
}
public UsuarioHome(Usuario entity) {
super(entity);
// TODO Auto-generated constructor stub
}
}
One thing I would suggest is to not have the field - private final E entity;. It's of really no use.
Also, it's better to take the transaction settings out of your DAOs and put them in your services.
来源:https://stackoverflow.com/questions/23072015/error-in-dao-class-when-using-generics