Spring boot initializes the MONGO database (the. json file is persisted to the MONGO database) __oracle

Source: Internet
Author: User

This article teaches you how to store the JSON content that you configure under the project in the MongoDB database when the project is started.
As we all know, Spring Boo Project T is started with a main function. So we want to be able to do something at the start (that is, listen for startup events). By looking at the Spring Boot document (http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/) we know that spring The boot provides an interface to monitor the startup of Spring boot:

@Component
class Systeminitializer implements applicationlistener<contextrefreshedevent> {

    @Override Public
    void Onapplicationevent (Contextrefreshedevent arg0) {
        //TODO auto-generated stub
        System.out.println ("Spring boot Boot.") ");
    }

}

And for our persistent JSON data to the MONGO database, my idea at first was to implement the interface on my own, and later found that spring specifically provided such a class:

Jackson2repositorypopulatorfactorybean

How to use:
First we look at the. json file:

[
{
  "_class": "Com.wang.domain.Student", "
  name": "Wang",
  "code": "Java",
  "email": " Alibaba@gmail.com "
 },
 {
  " _class ":" Com.wang.domain.Student ",
  " name ":" XI ",
  " code ":" Android ", "
  Email": "123456@qq.com"
 }
]

In addition to _class automatic fields are more special. The rest of us understand that this _class field is essential to define which class the data belongs to.

Package com.wang.domain;

Import Org.springframework.data.annotation.Id;

public class Student {
    @Id
    private String _id;
    private String name;
    private String Email;
    Private String code;

    Getter Setter Province
}

This is a clear glance of the bar. Now there is also an essential operation is to give student this class to define a repository (that is, we often say DAO), if not configured will throw no repository exception.

Public interface Studentrepository extends Mongorepository<student, string>{

}

Now the last step is to configure this. json file for Jackson2repositorypopulatorfactorybean.

Initialize data
    @Bean public
    Jackson2repositorypopulatorfactorybean repositorypopulator () {
        Jackson2repositorypopulatorfactorybean factory = new Jackson2repositorypopulatorfactorybean ();
        Determine if the database content needs to be regenerated
        if (db_rebuildmongodata) {
            //load. json file
            Resource InitData = new Classpathresource ("Init /inittestdata.json ");
            Factory.setresources (New Resource[]{initdata});
        else{
            factory.setresources (New resource[]{});
        return factory;
    }

You may have questions about what this db_rebuildmongodata variable is. Because this class is executed when spring boot is started and is used to initialize the database, it is not possible to initialize the database every time the project is republished in the production environment without special requirements. So we added this line to the configuration file application.properties of Spring boot:

Db_rebuild=true

In the main class:

@Value ("${db_rebuild}")
private Boolean db_rebuildmongodata;

It is not difficult to see the meaning of the above code, so whether the initialization of the database in their own hands.

If you are unfamiliar with spring boot, refer to my other blogs about spring boot:
http://blog.csdn.net/canot/article/details/51449589
http ://blog.csdn.net/canot/article/details/51571343

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.