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
Copy file name to clipboardExpand all lines: src/main/antora/modules/ROOT/pages/repositories/custom-implementations.adoc
+91
Original file line number
Diff line number
Diff line change
@@ -238,6 +238,97 @@ XML::
238
238
======
239
239
====
240
240
241
+
[[repositories.spring-factories]]
242
+
==== Registering Fragments with spring.factories
243
+
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.
245
+
Registering fragments within `spring.factories` allows you to circumvent this restriction as explained in the following section.
246
+
247
+
Imagine you'd like to provide some custom search functionality usable across multiple repositories for your organization leveraging a text search index.
248
+
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
+
251
+
====
252
+
[source,java]
253
+
----
254
+
package com.acme.search;
255
+
256
+
public interface SearchExtension<T> {
257
+
258
+
List<T> search(String text, Limit limit);
259
+
}
260
+
----
261
+
====
262
+
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.
In the snipped above we use `RepositoryMethodMetadata.get()` 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.
300
+
301
+
[TIP]
302
+
====
303
+
For testing you can use `TransactionSynchronizationManager.bindResource(RepositoryMethodMetadata.class, metadata)` to provide repository method metadata.
304
+
====
305
+
306
+
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 good to go.
To make use of the extension simply add the interface to the repository as shown below. The infrastructure will take care placing the required `RepositoryMethodMetadata` so all that
0 commit comments