Today, there is a need to insert a data into the database when the project is started.
First: Inherit Spring's Beanpostprocessor class with the following code
public class Beanpostprcessorimpl implements Beanpostprocessor {
private icompanyservice companyservice;
Public Icompanyservice Getcompanyservice () {return
companyservice;
}
public void Setcompanyservice (Icompanyservice companyservice) {
this.companyservice = Companyservice;
}
Processing public
Object Postprocessbeforeinitialization (Object bean, String beanname) before the Bean is instantiated
throws beansexception {
System.out.println ("Object" + Beanname + "start instantiation");
return bean;
Processing public
Object Postprocessafterinitialization (Object bean, String beanname) after the Bean is instantiated
throws beansexception {
System.out.println ("Object" + Beanname + "instantiation complete");
list<user> list = Companyservice.findall ("from User");
if (list.size () <= 0) {
User user = new user ();
User.setname ("Ahhffl");
User.setpassword ("Ahhffl");
Companyservice.addobject (user);
}
return bean;
}
}
But the method of this class is invoked when each bean is initialized, and I just want to insert a single piece of data, so it's not efficient.
Reference: Http://blog.csdn.net/chensugang/archive/2008/12/01/3423650.aspx Second: Remember that you can use spring's init-method this way, he can again bean Initializes a specified method after instantiating the
But this method must have no parameters, no return value. The code is as follows:
public void Init () throws Exception {
System.out.println ("JavaBean class Init method");
list<user> list = Basicdao.findall ("from User");
if (list.size () <= 0) {
User user = new user ();
User.setname ("Ahhffl");
User.setpassword ("Ahhffl");
Basicdao.saveuser (user);
System.out.println (User.getname ());
System.out.println (User.getpassword ());
System.out.println (User.getid ());
}
However, this hibernate transaction is always uncommitted, so the result is that the console has a print SQL statement when it is started, but the transaction is not committed and the database cannot see the data. Set (<property name= "Connection.autocommit" >true </property>)
Can be successful, but setting up autocommit is not good for spring management hibernate.
Third: Inherit spring's Applicationlistener this interface. Initializes the data for the database with the spring listener interface, as follows:
public class Applicationlistenerimpl implements Applicationlistener {
private icompanyservice companyservice;
Public Icompanyservice Getcompanyservice () {return
companyservice;
}
public void Setcompanyservice (Icompanyservice companyservice) {
this.companyservice = Companyservice;
}
@Override public
void Onapplicationevent (Applicationevent arg0) {
list<user> List = Companyservice.findall ("from User");
if (list.size () <= 0) {
User user = new user ();
User.setname ("Ahhffl");
User.setpassword ("Ahhffl");
Companyservice.addobject (user);}}
Reprint Address
Click to open the link