I created a simple web app using hibernate and spring and i want to implement an abstract class that contains the crud operations but I have this error :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientService' defined in class path resource [applicationContext.xml]:
Cannot resolve reference to bean 'clientDao' while setting bean property 'clientDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'clientDao' defined in class path resource [applicationContext.xml]:
Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.Class]:
GenericDao
public interface GenericDao<T, ID extends Serializable> {
T save(T entity);
T update(T entity);
void delete(T entity);
T findById(ID id);
List<T> findAll();
void flush();
}
GenericDaoImpl
@Transactional
public class GenericDaoImpl<T, ID extends Serializable> implements GenericDao<T, ID> {
@Autowired
SessionFactory sessionFactory ;
private Class<T> persistentClass;
public GenericDaoImpl() {
super();
}
public GenericDaoImpl(Class<T> persistentClass) {
super();
this.persistentClass = persistentClass;
}
@Transactional
public T save(T entity) {
this.sessionFactory.getCurrentSession().save(entity);
return null;
}
@Transactional
public T update(T entity) {
this.sessionFactory.getCurrentSession().update(entity);
return null;
}
@Transactional
public void delete(T entity) {
this.sessionFactory.getCurrentSession().delete(entity);
}
@SuppressWarnings("unchecked")
@Transactional
public T findById(ID id) {
return (T) this.sessionFactory.getCurrentSession().load(this.getPersistentClass(), id);
}
@SuppressWarnings("unchecked")
@Transactional
public List<T> findAll() {
return this.sessionFactory.getCurrentSession().createQuery("* from"+this.getPersistentClass().getSimpleName()).list();
}
@Transactional
public void flush() {
this.sessionFactory.getCurrentSession().flush();
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Class<T> getPersistentClass() {
return persistentClass;
}
public void setPersistentClass(Class<T> persistentClass) {
this.persistentClass = persistentClass;
}
}
ClientDao
public interface ClientDao extends GenericDao<Client,Integer> {
}
ClientDaoImpl
@Transactional
@Repository("clientDao")
public class ClientDaoImpl extends GenericDaoImpl<Client,Integer> implements ClientDao {
public ClientDaoImpl(Class<Client> persistentClass) {
super(persistentClass);
}
application context.xml
<bean id="client" class="com.webapp.model.Client"/>
<bean id="genericDao" class="com.webapp.dao.GenericDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="clientDao" class="com.webapp.dao.ClientDaoImpl" parent="genericDao">
<constructor-arg ref="client" />
</bean>
<bean id="clientService" class="com.webapp.service.ClientServiceImpl">
<property name="clientDao" ref="clientDao" />
</bean>
Use:
<bean id="clientDao" class="com.webapp.dao.ClientDaoImpl" parent="genericDao">
<constructor-arg >com.xxx.Client</constructor-arg >
Spring will "cast" the string to the class. Then you can remove the client bean from the XML.
Or remove this parameter from your ClientDaoImpl
, because it is useless (It can be only this type, so there is no reason to make it a parameter)
public ClientDaoImpl() {
super(com.xxx.Client.class);
}
WEB-INF/XXX-XX.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.security.web.context.SecurityContextRepository]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
Solution is to remove the name property from constructor argument(if it is there). Only keep the reference. It will work.
The constructor defined in the ClientDaoImpl
class expects a parameter of type Class<Client>
. But in the applicationContext.xml you set the instance client object to be passed to the constructor.
Change the constructor to receive the object and pass the class to the super, example:
public ClientDaoImpl(Client client) {
super(client.getClass());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With