How to automatically initialize a database at Project deployment __ Database

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.