|
| 1 | +[[core.repository-populators]] |
| 2 | += Repository Populators |
| 3 | + |
| 4 | +If you work with the Spring JDBC module, you are probably familiar with the support for populating a `DataSource` with SQL scripts. |
| 5 | +A similar abstraction is available on the repositories level, although it does not use SQL as the data definition language because it must be store-independent. |
| 6 | +Thus, the populators support XML (through Spring's OXM abstraction) and JSON (through Jackson) to define data with which to populate the repositories. |
| 7 | + |
| 8 | +Assume you have a file called `data.json` with the following content: |
| 9 | + |
| 10 | +.Data defined in JSON |
| 11 | +[source,javascript] |
| 12 | +---- |
| 13 | +[ { "_class" : "com.acme.Person", |
| 14 | + "firstname" : "Dave", |
| 15 | + "lastname" : "Matthews" }, |
| 16 | + { "_class" : "com.acme.Person", |
| 17 | + "firstname" : "Carter", |
| 18 | + "lastname" : "Beauford" } ] |
| 19 | +---- |
| 20 | + |
| 21 | +You can populate your repositories by using the populator elements of the repository namespace provided in Spring Data Commons. |
| 22 | +To populate the preceding data to your `PersonRepository`, declare a populator similar to the following: |
| 23 | + |
| 24 | +.Declaring a Jackson repository populator |
| 25 | +[source,xml] |
| 26 | +---- |
| 27 | +<?xml version="1.0" encoding="UTF-8"?> |
| 28 | +<beans xmlns="http://www.springframework.org/schema/beans" |
| 29 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 30 | + xmlns:repository="http://www.springframework.org/schema/data/repository" |
| 31 | + xsi:schemaLocation="http://www.springframework.org/schema/beans |
| 32 | + https://www.springframework.org/schema/beans/spring-beans.xsd |
| 33 | + http://www.springframework.org/schema/data/repository |
| 34 | + https://www.springframework.org/schema/data/repository/spring-repository.xsd"> |
| 35 | +
|
| 36 | + <repository:jackson2-populator locations="classpath:data.json" /> |
| 37 | +
|
| 38 | +</beans> |
| 39 | +---- |
| 40 | + |
| 41 | +The preceding declaration causes the `data.json` file to be read and deserialized by a Jackson `ObjectMapper`. |
| 42 | + |
| 43 | +The type to which the JSON object is unmarshalled is determined by inspecting the `_class` attribute of the JSON document. |
| 44 | +The infrastructure eventually selects the appropriate repository to handle the object that was deserialized. |
| 45 | + |
| 46 | +To instead use XML to define the data the repositories should be populated with, you can use the `unmarshaller-populator` element. |
| 47 | +You configure it to use one of the XML marshaller options available in Spring OXM. |
| 48 | +See the {spring-framework-docs}/data-access/oxm.html[Spring reference documentation] for details. |
| 49 | +The following example shows how to unmarshall a repository populator with JAXB: |
| 50 | + |
| 51 | +.Declaring an unmarshalling repository populator (using JAXB) |
| 52 | +[source,xml] |
| 53 | +---- |
| 54 | +<?xml version="1.0" encoding="UTF-8"?> |
| 55 | +<beans xmlns="http://www.springframework.org/schema/beans" |
| 56 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 57 | + xmlns:repository="http://www.springframework.org/schema/data/repository" |
| 58 | + xmlns:oxm="http://www.springframework.org/schema/oxm" |
| 59 | + xsi:schemaLocation="http://www.springframework.org/schema/beans |
| 60 | + https://www.springframework.org/schema/beans/spring-beans.xsd |
| 61 | + http://www.springframework.org/schema/data/repository |
| 62 | + https://www.springframework.org/schema/data/repository/spring-repository.xsd |
| 63 | + http://www.springframework.org/schema/oxm |
| 64 | + https://www.springframework.org/schema/oxm/spring-oxm.xsd"> |
| 65 | +
|
| 66 | + <repository:unmarshaller-populator locations="classpath:data.json" |
| 67 | + unmarshaller-ref="unmarshaller" /> |
| 68 | +
|
| 69 | + <oxm:jaxb2-marshaller contextPath="com.acme" /> |
| 70 | +
|
| 71 | +</beans> |
| 72 | +---- |
0 commit comments