You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -14,7 +18,7 @@ This chapter explains the core concepts and interfaces of Spring Data repositori
14
18
The information in this chapter is pulled from the Spring Data Commons module.
15
19
It uses the configuration and code samples for the Jakarta Persistence API (JPA) module.
16
20
ifeval::[{include-xml-namespaces} != false]
17
-
You should adapt the XML namespace declaration and the types to be extended to the equivalents of the particular module that you use. "`<<repositories.namespace-reference>>`" covers XML configuration, which is supported across all Spring Data modules that support the repository API.
21
+
If you want to use XML configuration you should adapt the XML namespace declaration and the types to be extended to the equivalents of the particular module that you use. "`<<repositories.namespace-reference>>`" covers XML configuration, which is supported across all Spring Data modules that support the repository API.
18
22
endif::[]
19
23
"`<<repository-query-keywords>>`" covers the query method keywords supported by the repository abstraction in general.
20
24
For detailed information on the specific features of your module, see the chapter on that module of this document.
. Set up Spring to create proxy instances for those interfaces, either with <<repositories.create-instances.java-config,JavaConfig>> or with <<repositories.create-instances,XML configuration>>.
147
-
148
-
.. To use Java configuration, create a class similar to the following:
If you use the repository abstraction for any other store, you need to change this to the appropriate namespace declaration of your store module.
183
185
In other words, you should exchange `jpa` in favor of, for example, `mongodb`.
184
186
endif::[]
185
187
+
186
188
Note that the JavaConfig variant does not configure a package explicitly, because the package of the annotated class is used by default.
187
-
To customize the package to scan, use one of the `basePackage…` attributes of the data-store-specific repository's `@Enable${store}Repositories`-annotation.
189
+
To customize the package to scan, use one of the `basePackage…` attributes of the data-store-specific repository's `@Enable{store}Repositories`-annotation.
188
190
. Inject the repository instance and use it, as shown in the following example:
189
191
+
190
192
====
@@ -372,7 +374,7 @@ However, Spring Data can then no longer determine a unique module with which to
372
374
The last way to distinguish repositories is by scoping repository base packages.
373
375
Base packages define the starting points for scanning for repository interface definitions, which implies having repository definitions located in the appropriate packages.
374
376
By default, annotation-driven configuration uses the package of the configuration class.
375
-
The <<repositories.create-instances.spring,base package in XML-based configuration>> is mandatory.
377
+
The <<repositories.create-instances.xml,base package in XML-based configuration>> is mandatory.
376
378
377
379
The following example shows annotation-driven configuration of base packages:
378
380
@@ -405,7 +407,7 @@ The following strategies are available for the repository infrastructure to reso
405
407
ifeval::[{include-xml-namespaces} != false]
406
408
With XML configuration, you can configure the strategy at the namespace through the `query-lookup-strategy` attribute.
407
409
endif::[]
408
-
For Java configuration, you can use the `queryLookupStrategy` attribute of the `Enable${store}Repositories` annotation.
410
+
For Java configuration, you can use the `queryLookupStrategy` attribute of the `Enable{store}Repositories` annotation.
409
411
Some strategies may not be supported for particular datastores.
410
412
411
413
- `CREATE` attempts to construct a store-specific query from the query method name.
This section covers how to create instances and bean definitions for the defined repository interfaces.
885
-
ifeval::[{include-xml-namespaces} != false]
886
-
One way to do so is by using the Spring namespace that is shipped with each Spring Data module that supports the repository mechanism, although we generally recommend using Java configuration.
887
-
endif::[]
888
887
889
-
[[repositories.create-instances.spring]]
888
+
[[repositories.create-instances.java-config]]
889
+
=== Java Configuration
890
+
891
+
Use the store-specific `@Enable{store}Repositories` annotation on a Java configuration class to define a configuration for repository activation.
892
+
For an introduction to Java-based configuration of the Spring container, see {spring-framework-docs}/core.html#beans-java[JavaConfig in the Spring reference documentation].
893
+
894
+
A sample configuration to enable Spring Data repositories resembles the following:
895
+
896
+
.Sample annotation-based repository configuration
897
+
====
898
+
[source,java]
899
+
----
900
+
@Configuration
901
+
@EnableJpaRepositories("com.acme.repositories")
902
+
class ApplicationConfiguration {
903
+
904
+
@Bean
905
+
EntityManagerFactory entityManagerFactory() {
906
+
// …
907
+
}
908
+
}
909
+
----
910
+
====
911
+
912
+
NOTE: The preceding example uses the JPA-specific annotation, which you would change according to the store module you actually use. The same applies to the definition of the `EntityManagerFactory` bean. See the sections covering the store-specific configuration.
913
+
890
914
ifeval::[{include-xml-namespaces} != false]
915
+
[[repositories.create-instances.spring]]
916
+
[[repositories.create-instances.xml]]
891
917
=== XML Configuration
892
918
893
919
Each Spring Data module includes a `repositories` element that lets you define a base package that Spring scans for you, as shown in the following example:
@@ -905,7 +931,7 @@ Each Spring Data module includes a `repositories` element that lets you define a
@@ -915,45 +941,29 @@ In the preceding example, Spring is instructed to scan `com.acme.repositories` a
915
941
For each interface found, the infrastructure registers the persistence technology-specific `FactoryBean` to create the appropriate proxies that handle invocations of the query methods.
916
942
Each bean is registered under a bean name that is derived from the interface name, so an interface of `UserRepository` would be registered under `userRepository`.
917
943
Bean names for nested repository interfaces are prefixed with their enclosing type name.
918
-
The `base-package` attribute allows wildcards so that you can define a pattern of scanned packages.
944
+
The base package attribute allows wildcards so that you can define a pattern of scanned packages.
945
+
endif::[]
919
946
920
947
[[repositories.using-filters]]
921
-
==== Using Filters
948
+
=== Using Filters
922
949
923
950
By default, the infrastructure picks up every interface that extends the persistence technology-specific `Repository` sub-interface located under the configured base package and creates a bean instance for it.
924
951
However, you might want more fine-grained control over which interfaces have bean instances created for them.
925
-
To do so, use `<include-filter />` and `<exclude-filter />` elements inside the `<repositories />` element.
926
-
The semantics are exactly equivalent to the elements in Spring's context namespace.
952
+
To do so, use filter elements inside the repository declaration.
953
+
The semantics are exactly equivalent to the elements in Spring's component filters.
927
954
For details, see the {spring-framework-docs}/core.html#beans-scanning-filters[Spring reference documentation] for these elements.
928
955
929
956
For example, to exclude certain interfaces from instantiation as repository beans, you could use the following configuration:
The preceding example excludes all interfaces ending in `SomeRepository` from being instantiated.
942
-
endif::[]
943
-
944
-
[[repositories.create-instances.java-config]]
945
-
=== Java Configuration
946
-
947
-
You can also trigger the repository infrastructure by using a store-specific `@Enable${store}Repositories` annotation on a Java configuration class. For an introduction to Java-based configuration of the Spring container, see {spring-framework-docs}/core.html#beans-java[JavaConfig in the Spring reference documentation].
948
-
949
-
A sample configuration to enable Spring Data repositories resembles the following:
NOTE: The preceding example uses the JPA-specific annotation, which you would change according to the store module you actually use. The same applies to the definition of the `EntityManagerFactory` bean. See the sections covering the store-specific configuration.
988
+
The preceding example excludes all interfaces ending in `SomeRepository` from being instantiated and includes those ending with `SomeOtherRepository`.
The repository infrastructure tries to autodetect custom implementation fragments by scanning for classes below the package in which it found a repository.
1136
-
These classes need to follow the naming convention of appending
1137
-
ifeval::[{include-xml-namespaces} != false]
1138
-
the namespace element's `repository-impl-postfix` attribute to the fragment interface name.
1139
-
This postfix defaults to `Impl`.
1140
-
endif::[]
1141
-
ifeval::[{include-xml-namespaces} == false]
1142
-
`Impl` to the fragment interface name.
1143
-
endif::[]
1158
+
These classes need to follow the naming convention of appending a postfix defaulting to `Impl`.
1159
+
1144
1160
The following example shows a repository that uses the default postfix and a repository that sets a custom value for the postfix:
The first configuration in the preceding example tries to look up a class called `com.acme.repository.CustomizedUserRepositoryImpl` to act as a custom repository implementation.
@@ -1200,14 +1226,29 @@ The following example shows how to manually wire a custom implementation:
@@ -1246,30 +1287,27 @@ CAUTION: The class needs to have a constructor of the super class which the stor
1246
1287
If the repository base class has multiple constructors, override the one taking an `EntityInformation` plus a store specific infrastructure object (such as an `EntityManager` or a template class).
1247
1288
1248
1289
The final step is to make the Spring Data infrastructure aware of the customized repository base class.
1249
-
In Java configuration, you can do so by using the `repositoryBaseClass` attribute of the `@Enable${store}Repositories` annotation, as shown in the following example:
1290
+
In configuration, you can do so by using the `repositoryBaseClass`, as shown in the following example:
1250
1291
1251
-
.Configuring a custom repository base class using JavaConfig
A corresponding attribute is available in the XML namespace, as shown in the following example:
1263
-
1264
-
.Configuring a custom repository base class using XML
1265
-
====
1266
-
[source,xml]
1303
+
.XML
1304
+
[source,xml,role="secondary"]
1267
1305
----
1268
1306
<repositories base-package="com.acme.repository"
1269
1307
base-class="….MyRepositoryImpl" />
1270
1308
----
1271
-
====
1272
1309
endif::[]
1310
+
====
1273
1311
1274
1312
[[core.domain-events]]
1275
1313
== Publishing Events from Aggregate Roots
@@ -1370,24 +1408,17 @@ In general, the integration support is enabled by using the `@EnableSpringDataWe
1370
1408
1371
1409
.Enabling Spring Data web support
1372
1410
====
1373
-
[source,java]
1411
+
.Java
1412
+
[source,java,role="primary"]
1374
1413
----
1375
1414
@Configuration
1376
1415
@EnableWebMvc
1377
1416
@EnableSpringDataWebSupport
1378
1417
class WebConfiguration {}
1379
1418
----
1380
-
====
1381
1419
1382
-
The `@EnableSpringDataWebSupport` annotation registers a few components.
1383
-
We discuss those later in this section.
1384
-
It also detects Spring HATEOAS on the classpath and registers integration components (if present) for it as well.
1385
-
1386
-
Alternatively, if you use XML configuration, register either `SpringDataWebConfiguration` or `HateoasAwareSpringDataWebConfiguration` as Spring beans, as the following example shows (for `SpringDataWebConfiguration`):
Copy file name to clipboardExpand all lines: src/main/asciidoc/repository-namespace-reference.adoc
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
5
5
[[populator.namespace-dao-config]]
6
6
== The `<repositories />` Element
7
-
The `<repositories />` element triggers the setup of the Spring Data repository infrastructure. The most important attribute is `base-package`, which defines the package to scan for Spring Data repository interfaces. See "`<<repositories.create-instances.spring>>`". The following table describes the attributes of the `<repositories />` element:
7
+
The `<repositories />` element triggers the setup of the Spring Data repository infrastructure. The most important attribute is `base-package`, which defines the package to scan for Spring Data repository interfaces. See "`<<repositories.create-instances.xml>>`". The following table describes the attributes of the `<repositories />` element:
Copy file name to clipboardExpand all lines: src/main/asciidoc/repository-populator-namespace-reference.adoc
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
5
5
[[namespace-dao-config]]
6
6
== The <populator /> element
7
-
The `<populator />` element allows to populate the a data store via the Spring Data repository infrastructure.footnote:[see <<repositories.create-instances.spring>>]
7
+
The `<populator />` element allows to populate a data store via the Spring Data repository infrastructure.footnote:[see <<repositories.create-instances.xml>>]
0 commit comments