Skip to content

Commit 22fa3c6

Browse files
mp911dechristophstrobl
authored andcommitted
Polishing.
Refine grammar, line breaks, typos. Original Pull Request: #3093
1 parent 3b667b9 commit 22fa3c6

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

Diff for: src/main/antora/modules/ROOT/pages/repositories/custom-implementations.adoc

+24-17
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,14 @@ XML::
241241
[[repositories.spring-factories]]
242242
==== Registering Fragments with spring.factories
243243

244-
As already mentioned in the <<repositories.configuration>> section, the infrastructure only auto detects fragments within the repositories base package. Therefore fragments residing in another location or maybe contributed by an external archive will not be found if they do not share a common namespace.
244+
As already mentioned in the <<repositories.configuration>> section, the infrastructure only auto-detects fragments within the repository base-package.
245+
Therefore, fragments residing in another location or want to be contributed by an external archive will not be found if they do not share a common namespace.
245246
Registering fragments within `spring.factories` allows you to circumvent this restriction as explained in the following section.
246247

247248
Imagine you'd like to provide some custom search functionality usable across multiple repositories for your organization leveraging a text search index.
248249

249-
First all you need is the fragment interface. Please note the generic `<T>` parameter to align the fragment with the repository domain type.
250+
First all you need is the fragment interface.
251+
Note the generic `<T>` parameter to align the fragment with the repository domain type.
250252

251253
====
252254
[source,java]
@@ -260,7 +262,8 @@ public interface SearchExtension<T> {
260262
----
261263
====
262264

263-
Let's assume the actual full text search is available via a `SearchService` that is registered as a `Bean` within the context so we can consume it in our `SearchExtension` implementation. All we need to run the search is the collection/index name and a object mapper that converts the search results into actual domain objects as sketched out below.
265+
Let's assume the actual full-text search is available via a `SearchService` that is registered as a `Bean` within the context so you can consume it in our `SearchExtension` implementation.
266+
All you need to run the search is the collection (or index) name and an object mapper that converts the search results into actual domain objects as sketched out below.
264267

265268
====
266269
[source,java]
@@ -273,9 +276,9 @@ import org.springframework.data.repository.core.support.RepositoryMethodContext;
273276
274277
class DefaultSearchExtension<T> implements SearchExtension<T> {
275278
276-
private SearchService service;
279+
private final SearchService service;
277280
278-
DefaultSearchExtension(@Autowired SearchService service) {
281+
DefaultSearchExtension(SearchService service) {
279282
this.service = service;
280283
}
281284
@@ -290,24 +293,28 @@ class DefaultSearchExtension<T> implements SearchExtension<T> {
290293
String indexName = domainType.getSimpleName().toLowerCase();
291294
List<String> jsonResult = service.search(indexName, text, 0, limit.max());
292295
293-
return jsonResult.stream().map( ... ).collect(toList());
296+
return jsonResult.stream().map().collect(toList());
294297
}
295298
}
296299
----
297300
====
298301

299-
In the snipped above we use `RepositoryMethodContext.currentMethod()` to get hold of metadata for the actual method invocation. In doing so we can access additional information attached to the repository. In this case we use the repositories domain type to identify the name of the index to be searched.
302+
In the example above `RepositoryMethodContext.currentMethod()` is used to retrieve metadata for the actual method invocation.
303+
`RepositoryMethodContext` exposes information attached to the repository such as the domain type.
304+
In this case we use the repository domain type to identify the name of the index to be searched.
300305

301-
Now that we've got both, the fragments declaration and implementation we can register it in the `META-INF/spring.factories` file, package things up if needed and we're almost good to go.
306+
Now that you've got both, the fragment declaration and implementation you can register it in the `META-INF/spring.factories` file, package things up if needed, and you're good to go.
302307

308+
.Registering a fragment implementation through `META-INF/spring.factories`
303309
====
304310
[source,properties]
305311
----
306312
com.acme.search.SearchExtension=com.acme.search.DefaultSearchExtension
307313
----
308314
====
309315

310-
Since we're using additional metadata, that comes with additional cost, via `RepositoryMethodContext.currentMethod()` we need to advise the repository factory responsible for creating the actual repository to expose method metadata by setting the `exposeMetadata` flag.
316+
Exposing invocation metadata is costly, hence it is disabled by default.
317+
To access `RepositoryMethodContext.currentMethod()` you need to advise the repository factory responsible for creating the actual repository to expose method metadata by setting the `exposeMetadata` flag.
311318

312319
====
313320
[source,java]
@@ -318,10 +325,10 @@ import org.springframework.data.repository.core.support.RepositoryFactoryBeanSup
318325
import org.springframework.lang.Nullable;
319326
320327
@Configuration
321-
class Cfg implements BeanPostProcessor {
328+
class MyConfiguration implements BeanPostProcessor {
322329
323330
@Bean
324-
public BeanPostProcessor exposeMethodMetadata() {
331+
static BeanPostProcessor exposeMethodMetadata() {
325332
326333
return new BeanPostProcessor() {
327334
@@ -336,12 +343,13 @@ class Cfg implements BeanPostProcessor {
336343
}
337344
}
338345
----
339-
The above snippet outlines how to set the `exposeMetadata` flag using a `BeanPostProcessor`.
340-
Please do not just copy/paste the above but think about your actual use case which may require a more fine grained approach as the above will simply enable the flag on each and every repository. You may want to have a look at our https://github.com/spring-projects/spring-data-examples/tree/main/bom[spring-data-examples] project to draw inspiration.
346+
347+
The above example outlines how to enable metadata exposure by setting the `exposeMetadata` flag using a `BeanPostProcessor`.
348+
Please do not just copy/paste the above but consider your actual use case which may require a more fine-grained approach as the above will simply enable the flag on every repository.
349+
You may want to have a look at our https://github.com/spring-projects/spring-data-examples/tree/main/bom[spring-data-examples] project to draw inspiration.
341350
====
342351

343-
Now we are ready to make use of the extension.
344-
Simply add the interface to the repository.
352+
Now you are ready to make use of your extension; Simply add the interface to your repository:
345353

346354
====
347355
[source,java]
@@ -351,8 +359,7 @@ package io.my.movies;
351359
import com.acme.search.SearchExtension;
352360
import org.springframework.data.repository.CrudRepository;
353361
354-
355-
public interface MovieRepository extends CrudRepository<Movie, String>, SearchExtension<Movie> {
362+
interface MovieRepository extends CrudRepository<Movie, String>, SearchExtension<Movie> {
356363
357364
}
358365
----

0 commit comments

Comments
 (0)