Skip to content

Commit 2d2f67c

Browse files
committed
Prefer Java configuration over XML.
Closes #4186
1 parent e9818fe commit 2d2f67c

9 files changed

+159
-131
lines changed

src/main/asciidoc/index.adoc

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ Mark Pollack; Thomas Risberg; Oliver Gierke; Costin Leau; Jon Brisbin; Thomas Da
33
:revnumber: {version}
44
:revdate: {localdate}
55
ifdef::backend-epub3[:front-cover-image: image:epub-cover.png[Front Cover,1050,1600]]
6-
:spring-data-commons-docs: ../../../../../spring-data-commons/src/main/asciidoc
6+
:spring-data-commons-docs: ../../../../spring-data-commons/src/main/asciidoc
7+
:store: Mongo
78

89
(C) 2008-2022 The original authors.
910

src/main/asciidoc/reference/gridfs.adoc

+5-8
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
MongoDB supports storing binary files inside its filesystem, GridFS. Spring Data MongoDB provides a `GridFsOperations` interface as well as the corresponding implementation, `GridFsTemplate`, to let you interact with the filesystem. You can set up a `GridFsTemplate` instance by handing it a `MongoDatabaseFactory` as well as a `MongoConverter`, as the following example shows:
55

6-
.JavaConfig setup for a GridFsTemplate
6+
77
====
8-
[source,java]
8+
.Java
9+
[source,java,role="primary"]
910
----
1011
class GridFsConfiguration extends AbstractMongoClientConfiguration {
1112
@@ -17,13 +18,9 @@ class GridFsConfiguration extends AbstractMongoClientConfiguration {
1718
}
1819
}
1920
----
20-
====
2121
22-
The corresponding XML configuration follows:
23-
24-
.XML configuration for a GridFsTemplate
25-
====
26-
[source,xml]
22+
.XML
23+
[source,xml,role="secondary"]
2724
----
2825
<?xml version="1.0" encoding="UTF-8"?>
2926
<beans xmlns="http://www.springframework.org/schema/beans"

src/main/asciidoc/reference/mapping.adoc

+18-20
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,11 @@ calling `get()` before the actual conversion
266266

267267
Unless explicitly configured, an instance of `MappingMongoConverter` is created by default when you create a `MongoTemplate`. You can create your own instance of the `MappingMongoConverter`. Doing so lets you dictate where in the classpath your domain classes can be found, so that Spring Data MongoDB can extract metadata and construct indexes. Also, by creating your own instance, you can register Spring converters to map specific classes to and from the database.
268268

269-
You can configure the `MappingMongoConverter` as well as `com.mongodb.client.MongoClient` and MongoTemplate by using either Java-based or XML-based metadata. The following example uses Spring's Java-based configuration:
269+
You can configure the `MappingMongoConverter` as well as `com.mongodb.client.MongoClient` and MongoTemplate by using either Java-based or XML-based metadata. The following example shows the configuration:
270270

271-
.@Configuration class to configure MongoDB mapping support
272271
====
273-
[source,java]
272+
.Java
273+
[source,java,role="primary"]
274274
----
275275
@Configuration
276276
public class MongoConfig extends AbstractMongoClientConfiguration {
@@ -299,24 +299,11 @@ public class MongoConfig extends AbstractMongoClientConfiguration {
299299
return new LoggingEventListener<MongoMappingEvent>();
300300
}
301301
}
302-
----
303-
<1> The mapping base package defines the root path used to scan for entities used to pre initialize the `MappingContext`. By default the configuration classes package is used.
304-
<2> Configure additional custom converters for specific domain types that replace the default mapping procedure for those types with your custom implementation.
305-
====
306-
307-
`AbstractMongoClientConfiguration` requires you to implement methods that define a `com.mongodb.client.MongoClient` as well as provide a database name. `AbstractMongoClientConfiguration` also has a method named `getMappingBasePackage(…)` that you can override to tell the converter where to scan for classes annotated with the `@Document` annotation.
308-
309-
You can add additional converters to the converter by overriding the `customConversionsConfiguration` method.
310-
MongoDB's native JSR-310 support can be enabled through `MongoConverterConfigurationAdapter.useNativeDriverJavaTimeCodecs()`.
311-
Also shown in the preceding example is a `LoggingEventListener`, which logs `MongoMappingEvent` instances that are posted onto Spring's `ApplicationContextEvent` infrastructure.
312-
313-
NOTE: `AbstractMongoClientConfiguration` creates a `MongoTemplate` instance and registers it with the container under the name `mongoTemplate`.
314302
315-
Spring's MongoDB namespace lets you enable mapping functionality in XML, as the following example shows:
303+
----
316304
317-
.XML schema to configure MongoDB mapping support
318-
====
319-
[source,xml]
305+
.XML
306+
[source,xml,role="secondary"]
320307
----
321308
<?xml version="1.0" encoding="UTF-8"?>
322309
<beans xmlns="http://www.springframework.org/schema/beans"
@@ -353,8 +340,19 @@ Spring's MongoDB namespace lets you enable mapping functionality in XML, as the
353340
354341
</beans>
355342
----
343+
<1> The mapping base package defines the root path used to scan for entities used to pre initialize the `MappingContext`. By default the configuration classes package is used.
344+
<2> Configure additional custom converters for specific domain types that replace the default mapping procedure for those types with your custom implementation.
356345
====
357346

347+
`AbstractMongoClientConfiguration` requires you to implement methods that define a `com.mongodb.client.MongoClient` as well as provide a database name. `AbstractMongoClientConfiguration` also has a method named `getMappingBasePackage(…)` that you can override to tell the converter where to scan for classes annotated with the `@Document` annotation.
348+
349+
You can add additional converters to the converter by overriding the `customConversionsConfiguration` method.
350+
MongoDB's native JSR-310 support can be enabled through `MongoConverterConfigurationAdapter.useNativeDriverJavaTimeCodecs()`.
351+
Also shown in the preceding example is a `LoggingEventListener`, which logs `MongoMappingEvent` instances that are posted onto Spring's `ApplicationContextEvent` infrastructure.
352+
353+
NOTE: `AbstractMongoClientConfiguration` creates a `MongoTemplate` instance and registers it with the container under the name `mongoTemplate`.
354+
355+
358356
The `base-package` property tells it where to scan for classes annotated with the `@org.springframework.data.mongodb.core.mapping.Document` annotation.
359357

360358
[[mapping-usage]]
@@ -607,7 +605,7 @@ The mapping subsystem allows the customization of the object construction by ann
607605

608606
* If a parameter is annotated with the `@Value` annotation, the given expression is evaluated and the result is used as the parameter value.
609607
* If the Java type has a property whose name matches the given field of the input document, then it's property information is used to select the appropriate constructor parameter to pass the input field value to. This works only if the parameter name information is present in the java `.class` files which can be achieved by compiling the source with debug information or using the new `-parameters` command-line switch for javac in Java 8.
610-
* Otherwise a `MappingException` will be thrown indicating that the given constructor parameter could not be bound.
608+
* Otherwise, a `MappingException` will be thrown indicating that the given constructor parameter could not be bound.
611609

612610
[source,java]
613611
----

src/main/asciidoc/reference/mongo-auditing.adoc

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[[mongo.auditing]]
22
== General Auditing Configuration for MongoDB
33

4-
Since Spring Data MongoDB 1.4, auditing can be enabled by annotating a configuration class with the `@EnableMongoAuditing` annotation, as the followign example shows:
4+
Since Spring Data MongoDB 1.4, auditing can be enabled by annotating a configuration class with the `@EnableMongoAuditing` annotation, as the following example shows:
55

6-
.Activating auditing using JavaConfig
76
====
8-
[source,java]
7+
.Java
8+
[source,java,role="primary"]
99
----
1010
@Configuration
1111
@EnableMongoAuditing
@@ -17,19 +17,16 @@ class Config {
1717
}
1818
}
1919
----
20-
====
21-
If you expose a bean of type `AuditorAware` to the `ApplicationContext`, the auditing infrastructure picks it up automatically and uses it to determine the current user to be set on domain types. If you have multiple implementations registered in the `ApplicationContext`, you can select the one to be used by explicitly setting the `auditorAwareRef` attribute of `@EnableMongoAuditing`.
22-
23-
To activate auditing functionality via XML, add the Spring Data Mongo `auditing` namespace element to your configuration, as the following example shows:
2420
25-
.Activating auditing by using XML configuration
26-
====
27-
[source,xml]
21+
.XML
22+
[source,xml,role="secondary"]
2823
----
2924
<mongo:auditing mapping-context-ref="customMappingContext" auditor-aware-ref="yourAuditorAwareImpl"/>
3025
----
3126
====
3227

28+
If you expose a bean of type `AuditorAware` to the `ApplicationContext`, the auditing infrastructure picks it up automatically and uses it to determine the current user to be set on domain types. If you have multiple implementations registered in the `ApplicationContext`, you can select the one to be used by explicitly setting the `auditorAwareRef` attribute of `@EnableMongoAuditing`.
29+
3330
To enable auditing, leveraging a reactive programming model, use the `@EnableReactiveMongoAuditing` annotation. +
3431
If you expose a bean of type `ReactiveAuditorAware` to the `ApplicationContext`, the auditing infrastructure picks it up automatically and uses it to determine the current user to be set on domain types. If you have multiple implementations registered in the `ApplicationContext`, you can select the one to be used by explicitly setting the `auditorAwareRef` attribute of `@EnableReactiveMongoAuditing`.
3532

src/main/asciidoc/reference/mongo-repositories-aggregation.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Use the `@Meta` annotation to set those options via `maxExecutionTimeMs`, `comme
8282

8383
[source,java]
8484
----
85-
public interface PersonRepository extends CrudReppsitory<Person, String> {
85+
interface PersonRepository extends CrudReppsitory<Person, String> {
8686
8787
@Meta(allowDiskUse = true)
8888
@Aggregation("{ $group: { _id : $lastname, names : { $addToSet : $firstname } } }")
@@ -99,7 +99,7 @@ Or use `@Meta` to create your own annotation as shown in the sample below.
9999
@Meta(allowDiskUse = true)
100100
@interface AllowDiskUse { }
101101
102-
public interface PersonRepository extends CrudReppsitory<Person, String> {
102+
interface PersonRepository extends CrudReppsitory<Person, String> {
103103
104104
@AllowDiskUse
105105
@Aggregation("{ $group: { _id : $lastname, names : { $addToSet : $firstname } } }")

src/main/asciidoc/reference/mongo-repositories.adoc

+10-14
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ Right now this interface serves only to provide type information, but we can add
5151
To start using the repository, use the `@EnableMongoRepositories` annotation.
5252
That annotation carries the same attributes as the namespace element.
5353
If no base package is configured, the infrastructure scans the package of the annotated configuration class.
54-
The following example shows how to use Java configuration for a repository:
54+
The following example shows how to configuration your application to use MongoDB repositories:
5555

56-
.Java configuration for repositories
5756
====
58-
[source,java]
57+
.Java
58+
[source,java,role="primary"]
5959
----
6060
@Configuration
61-
@EnableMongoRepositories
61+
@EnableMongoRepositories("com.acme.*.repositories")
6262
class ApplicationConfig extends AbstractMongoClientConfiguration {
6363
6464
@Override
@@ -68,17 +68,13 @@ class ApplicationConfig extends AbstractMongoClientConfiguration {
6868
6969
@Override
7070
protected String getMappingBasePackage() {
71-
return "com.oreilly.springdata.mongodb";
71+
return "com.acme.*.repositories";
7272
}
7373
}
7474
----
75-
====
7675
77-
If you would rather go with XML based configuration add the following content:
78-
79-
.General MongoDB repository Spring XML configuration
80-
====
81-
[source,xml]
76+
.XML
77+
[source,xml,role="secondary"]
8278
----
8379
<?xml version="1.0" encoding="UTF-8"?>
8480
<beans xmlns="http://www.springframework.org/schema/beans"
@@ -113,14 +109,14 @@ Consequently, accessing the second page of `Person` objects at a page size of 10
113109
====
114110
[source,java]
115111
----
116-
@RunWith(SpringRunner.class)
112+
@ExtendWith(SpringExtension.class)
117113
@ContextConfiguration
118-
public class PersonRepositoryTests {
114+
class PersonRepositoryTests {
119115
120116
@Autowired PersonRepository repository;
121117
122118
@Test
123-
public void readsFirstPageCorrectly() {
119+
void readsFirstPageCorrectly() {
124120
125121
Page<Person> persons = repository.findAll(PageRequest.of(0, 10));
126122
assertThat(persons.isFirstPage()).isTrue();

0 commit comments

Comments
 (0)