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