Skip to content

Commit 1d7fc40

Browse files
committed
Refine documentation to prefer Java configuration over XML.
Closes #2666
1 parent cdcd21a commit 1d7fc40

File tree

3 files changed

+119
-82
lines changed

3 files changed

+119
-82
lines changed

src/main/asciidoc/repositories.adoc

Lines changed: 117 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
:spring-framework-docs: https://docs.spring.io/spring-framework/docs/{springVersion}/reference/html
22
:spring-framework-javadoc: https://docs.spring.io/spring/docs/{springVersion}/javadoc-api
33

4+
ifndef::store[]
5+
:store: Jpa
6+
endif::[]
7+
48
[[repositories]]
59
= Working with Spring Data Repositories
610

@@ -14,7 +18,7 @@ This chapter explains the core concepts and interfaces of Spring Data repositori
1418
The information in this chapter is pulled from the Spring Data Commons module.
1519
It uses the configuration and code samples for the Jakarta Persistence API (JPA) module.
1620
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.
1822
endif::[]
1923
"`<<repository-query-keywords>>`" covers the query method keywords supported by the repository abstraction in general.
2024
For detailed information on the specific features of your module, see the chapter on that module of this document.
@@ -144,24 +148,20 @@ interface PersonRepository extends Repository<Person, Long> {
144148
====
145149

146150
. 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:
149151
+
150152
====
151-
[source,java]
153+
.Java
154+
[source,java,subs="attributes,specialchars",role="primary"]
152155
----
153-
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
156+
import org.springframework.data..repository.config.Enable{store}Repositories;
154157
155-
@EnableJpaRepositories
158+
@Enable{store}Repositories
156159
class Config { … }
157160
----
158-
====
159161
160162
ifeval::[{include-xml-namespaces} != false]
161-
.. To use XML configuration, define a bean similar to the following:
162-
+
163-
====
164-
[source,xml]
163+
.XML
164+
[source,xml,role="secondary"]
165165
----
166166
<?xml version="1.0" encoding="UTF-8"?>
167167
<beans xmlns="http://www.springframework.org/schema/beans"
@@ -172,19 +172,21 @@ ifeval::[{include-xml-namespaces} != false]
172172
http://www.springframework.org/schema/data/jpa
173173
https://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
174174
175-
<jpa:repositories base-package="com.acme.repositories"/>
175+
<repositories base-package="com.acme.repositories"/>
176176
177177
</beans>
178178
----
179+
endif::[]
179180
====
180181
+
182+
ifeval::[{include-xml-namespaces} != false]
181183
The JPA namespace is used in this example.
182184
If you use the repository abstraction for any other store, you need to change this to the appropriate namespace declaration of your store module.
183185
In other words, you should exchange `jpa` in favor of, for example, `mongodb`.
184186
endif::[]
185187
+
186188
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.
188190
. Inject the repository instance and use it, as shown in the following example:
189191
+
190192
====
@@ -372,7 +374,7 @@ However, Spring Data can then no longer determine a unique module with which to
372374
The last way to distinguish repositories is by scoping repository base packages.
373375
Base packages define the starting points for scanning for repository interface definitions, which implies having repository definitions located in the appropriate packages.
374376
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.
376378

377379
The following example shows annotation-driven configuration of base packages:
378380

@@ -405,7 +407,7 @@ The following strategies are available for the repository infrastructure to reso
405407
ifeval::[{include-xml-namespaces} != false]
406408
With XML configuration, you can configure the strategy at the namespace through the `query-lookup-strategy` attribute.
407409
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.
409411
Some strategies may not be supported for particular datastores.
410412

411413
- `CREATE` attempts to construct a store-specific query from the query method name.
@@ -882,12 +884,36 @@ CompletableFuture<User> findOneByFirstname(String firstname); <2>
882884
== Creating Repository Instances
883885

884886
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::[]
888887

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+
890914
ifeval::[{include-xml-namespaces} != false]
915+
[[repositories.create-instances.spring]]
916+
[[repositories.create-instances.xml]]
891917
=== XML Configuration
892918

893919
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
905931
http://www.springframework.org/schema/data/jpa
906932
https://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
907933
908-
<repositories base-package="com.acme.repositories" />
934+
<jpa:repositories base-package="com.acme.repositories" />
909935
910936
</beans:beans>
911937
----
@@ -915,45 +941,29 @@ In the preceding example, Spring is instructed to scan `com.acme.repositories` a
915941
For each interface found, the infrastructure registers the persistence technology-specific `FactoryBean` to create the appropriate proxies that handle invocations of the query methods.
916942
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`.
917943
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::[]
919946

920947
[[repositories.using-filters]]
921-
==== Using Filters
948+
=== Using Filters
922949

923950
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.
924951
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.
927954
For details, see the {spring-framework-docs}/core.html#beans-scanning-filters[Spring reference documentation] for these elements.
928955

929956
For example, to exclude certain interfaces from instantiation as repository beans, you could use the following configuration:
930957

931-
.Using exclude-filter element
958+
.Using filters
932959
====
933-
[source,xml]
934-
----
935-
<repositories base-package="com.acme.repositories">
936-
<context:exclude-filter type="regex" expression=".*SomeRepository" />
937-
</repositories>
938-
----
939-
====
940-
941-
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:
950-
951-
.Sample annotation-based repository configuration
952-
====
953-
[source,java]
960+
.Java
961+
[source,java,subs="attributes,specialchars",role="primary"]
954962
----
955963
@Configuration
956-
@EnableJpaRepositories("com.acme.repositories")
964+
@Enable{store}Repositories(basePackages = "com.acme.repositories",
965+
includeFilters = { @Filter(type = FilterType.REGEX, pattern = ".*SomeRepository") },
966+
excludeFilters = { @Filter(type = FilterType.REGEX, pattern = ".*SomeOtherRepository") })
957967
class ApplicationConfiguration {
958968
959969
@Bean
@@ -962,9 +972,21 @@ class ApplicationConfiguration {
962972
}
963973
}
964974
----
975+
976+
ifeval::[{include-xml-namespaces} != false]
977+
.XML
978+
[source,xml,role="secondary"]
979+
----
980+
<repositories base-package="com.acme.repositories">
981+
<context:exclude-filter type="regex" expression=".*SomeRepository" />
982+
<context:include-filter type="regex" expression=".*SomeOtherRepository" />
983+
</repositories>
984+
----
985+
endif::[]
965986
====
966987

967-
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`.
989+
968990

969991
[[repositories.create-instances.standalone]]
970992
=== Standalone Usage
@@ -1133,24 +1155,28 @@ interface PersonRepository extends CrudRepository<Person, Long>, CustomizedSave<
11331155
==== Configuration
11341156

11351157
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+
11441160
The following example shows a repository that uses the default postfix and a repository that sets a custom value for the postfix:
11451161

11461162
.Configuration example
11471163
====
1148-
[source,xml]
1164+
.Java
1165+
[source,java,subs="attributes,specialchars",role="primary"]
1166+
----
1167+
@Enable{store}Repositories(repositoryImplementationPostfix = "MyPostfix")
1168+
class Configuration { … }
1169+
----
1170+
1171+
ifeval::[{include-xml-namespaces} != false]
1172+
.XML
1173+
[source,xml,role="secondary"]
11491174
----
11501175
<repositories base-package="com.acme.repository" />
11511176
11521177
<repositories base-package="com.acme.repository" repository-impl-postfix="MyPostfix" />
11531178
----
1179+
endif::[]
11541180
====
11551181

11561182
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:
12001226

12011227
.Manual wiring of custom implementations
12021228
====
1203-
[source,xml]
1229+
1230+
.Java
1231+
[source,java,role="primary"]
1232+
----
1233+
class MyClass {
1234+
MyClass(@Qualifier("userRepositoryImpl") UserRepository userRepository) {
1235+
1236+
}
1237+
}
1238+
----
1239+
1240+
ifeval::[{include-xml-namespaces} != false]
1241+
.XML
1242+
[source,xml,role="secondary"]
12041243
----
12051244
<repositories base-package="com.acme.repository" />
12061245
12071246
<beans:bean id="userRepositoryImpl" class="…">
12081247
<!-- further configuration -->
12091248
</beans:bean>
12101249
----
1250+
endif::[]
1251+
12111252
====
12121253

12131254
[[repositories.customize-base-repository]]
@@ -1246,30 +1287,27 @@ CAUTION: The class needs to have a constructor of the super class which the stor
12461287
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).
12471288

12481289
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:
12501291

1251-
.Configuring a custom repository base class using JavaConfig
1292+
.Configuring a custom repository base class
12521293
====
1253-
[source,java]
1294+
.Java
1295+
[source,java,subs="attributes,specialchars",role="primary"]
12541296
----
12551297
@Configuration
1256-
@EnableJpaRepositories(repositoryBaseClass = MyRepositoryImpl.class)
1298+
@Enable{store}Repositories(repositoryBaseClass = MyRepositoryImpl.class)
12571299
class ApplicationConfiguration { … }
12581300
----
1259-
====
12601301
12611302
ifeval::[{include-xml-namespaces} != false]
1262-
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"]
12671305
----
12681306
<repositories base-package="com.acme.repository"
12691307
base-class="….MyRepositoryImpl" />
12701308
----
1271-
====
12721309
endif::[]
1310+
====
12731311

12741312
[[core.domain-events]]
12751313
== Publishing Events from Aggregate Roots
@@ -1370,24 +1408,17 @@ In general, the integration support is enabled by using the `@EnableSpringDataWe
13701408

13711409
.Enabling Spring Data web support
13721410
====
1373-
[source,java]
1411+
.Java
1412+
[source,java,role="primary"]
13741413
----
13751414
@Configuration
13761415
@EnableWebMvc
13771416
@EnableSpringDataWebSupport
13781417
class WebConfiguration {}
13791418
----
1380-
====
13811419
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`):
1387-
1388-
.Enabling Spring Data web support in XML
1389-
====
1390-
[source,xml]
1420+
.XML
1421+
[source,xml,role="secondary"]
13911422
----
13921423
<bean class="org.springframework.data.web.config.SpringDataWebConfiguration" />
13931424
@@ -1396,6 +1427,12 @@ Alternatively, if you use XML configuration, register either `SpringDataWebConfi
13961427
----
13971428
====
13981429

1430+
The `@EnableSpringDataWebSupport` annotation registers a few components.
1431+
We discuss those later in this section.
1432+
It also detects Spring HATEOAS on the classpath and registers integration components (if present) for it as well.
1433+
1434+
.Enabling Spring Data web support in XML
1435+
13991436
[[core.web.basic]]
14001437
==== Basic Web Support
14011438

src/main/asciidoc/repository-namespace-reference.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
[[populator.namespace-dao-config]]
66
== 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:
88

99
.Attributes
1010
[options="header", cols="1,3"]

src/main/asciidoc/repository-populator-namespace-reference.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
[[namespace-dao-config]]
66
== 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>>]
88

99
.Attributes
1010
[options="header", cols="1,3"]

0 commit comments

Comments
 (0)