Beginner spring, when consolidating hibernate in spring, you encounter the following exceptions:
Org.springframework.beans.factory.BeanCreationException:Error creating Bean with Name ' Bookdao ' defined in class path re SOURCE [Applicationcontext.xml]: Initialization of Bean failed; Nested exception is java.lang.IllegalArgumentException:sessionFactory or hibernatetemplate is required
I have defined a Bookdao class in the program, inherits the Hibernatedaosupport, uses the hibernatetemplate to carry on the deletion to the database data additions and deletions to check the operation.
public class Bookdao extends Hibernatedaosupport ... {
public void Addbook (book book) ... {
Gethibernatetemplate (). Save (book);
Gethibernatetemplate (). Flush ();
}
public void Delbook (book book) ... {
Gethibernatetemplate (). Delete (book);
Gethibernatetemplate (). Flush ();
}
public void Update (book book) ... {
Gethibernatetemplate (). Update (book);
Gethibernatetemplate (). Flush ();
}
Public book GetBook (Long oid) ... {
Return (book) gethibernatetemplate (). Get (Book.class, OID);
}
}
In addition, I define a class booksrv, which relies on Bookdao, and injects Bookdao objects using the dependency injection of the setter injection.
public class booksrv ... {
Private Bookdao Bdao=null;
public void Setbdao (Bookdao bdao) ... {
This.bdao = Bdao;
}
public void Add (book book) ... {
Bdao.addbook (book);
}
public void del (book book) ... {
Bdao.delbook (book);
}
public void Update (book book) ... {
Bdao.update (book);
}
Public book get (Long oid) ... {
Return Bdao.getbook (OID);
}
}
My original error profile is as follows (omit other parts):
< Bean id = "Bookdao" class = DAO. Bookdao ">
</Bean >
< Bean id = "BOOKSRV" class = srv. Booksrv ">
< property name = ' Bdao ' ref = ' Bookdao ' ></property >
</Bean >
Thought for a long time, also can not find unusual reason. And by that sentence sessionfactory or hibernatetemplate is required, has been in DataSource, Sessionfactory, Hibernatetemplate's configuration is desperately chasing the wrong place.
It's time to find out where the problem lies: Beancreationexception:error creating Bean with Name ' Bookdao ' ...
Because Bookdao inherits Hibernatedaosupport and uses Hibernatedaosupport's gethibernatetemplate () to get a Hibernatetemplate object, Then call the object's Save (), flush (), and other methods. Therefore, Bookdao relies on hibernatetemplate.
Then, adding Bookdao to Hibernatetemplate in the configuration file solves the problem.
The correct configuration file is:
< Bean id = "Bookdao" class = DAO. Bookdao ">
< property name = ' hibernatetemplate ' ref = ' hibernatetemplate ' ></property >
</Bean >
< Bean id = "BOOKSRV" class = srv. Booksrv ">
< property name = ' Bdao ' ref = ' Bookdao ' ></property >
</Bean >