Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the Repository of Spring Data JPA framework

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to customize the Repository of the Spring Data JPA framework", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to customize the Repository of the Spring Data JPA framework" can help you solve your doubts.

1. Spring Data Repository custom implementation

Spring Data provides a variety of options to create query methods with only a small amount of coding. But when these options do not meet your needs, you can also provide your own custom implementation for the repository method. This section focuses on how to do this.

1.1 Custom Special repository

To enrich the repository library with custom functional implementations, you must first define a snippet interface and an implementation of custom functionality, as shown below.

Example 1. Custom interface

Public interface CustomUserRepository {void customMethod (User user);}

Example 2. Custom interface implementation class

Public class CustomUserRepositoryImpl implements CustomUserRepository {public void customMethod (User user) {/ / Your custom implementation}}

The implementation class itself does not depend on Spring Data, it can be a normal Spring Bean object. Therefore, you can use standard dependency injection behavior to inject references to other Bean (such as JdbcTemplate), participate in aspects, and so on.

You can then have your repository interface extend the fragment interface, as shown below.

Example 3. Modify your repository interface definition to extend your custom interface

Public interface UserRepository extends CrudRepository, CustomUserRepository {/ / Declare query methods here}

This extends the custom interface with your repository interface, combines CRUD and custom functions, and enables it to provide services to clients.

Spring Data repositories is achieved by using fragments that form a combination of repository. Fragments are the basic repository, functional aspects (such as QueryDsl), and custom interfaces and their implementations. Every time you add an interface to your repository interface, you enhance the composition by adding a fragment. The implementation of the basic repository and repository is provided by each Spring Data module.

The following example shows custom interfaces and their implementation.

Example 4. Fragments and their implementation

Public interface HumanRepository {void humanMethod (User user);} public class HumanRepositoryImpl implements HumanRepository {public void humanMethod (User user) {/ / Your custom implementation}} public interface ContactRepository {void contactMethod1 (User user); User contactMethod2 (User user);} public class ContactRepositoryImpl implements ContactRepository {public void contactMethod1 (User user) {/ / Your custom implementation} public User contactMethod2 (User user) {/ / Your custom implementation}}

The following example shows an interface that extends CrudRepository's custom repository.

Example 5. Modify your repository interface definition to extend multiple of your custom interfaces

Public interface UserRepository extends CrudRepository, HumanRepository, ContactRepository {/ / Declare query methods here}

Repository can consist of multiple custom implementations that are imported in the order in which they are declared. Custom implementations take precedence over basic implementations and repository aspects. This sort allows you to override the underlying repository and aspect methods and resolve ambiguities when two fragments contribute the same method signature. Repository fragments are not limited to use in a single repository interface. Multiple repository can use a fragment interface that allows you to reuse customized content in different repository.

The following example shows a repository fragment and its implementation.

Example 6. Rewrite save (…) The fragment code of the method

Public interface CustomSave {S save (S entity);} public class CustomSaveImpl implements CustomSave {public S save (S entity) {/ / Your custom implementation}}

Example 7 extends the interface defined in example 6 in the repository interface

Interface UserRepository extends CrudRepository, CustomSave {} interface PersonRepository extends CrudRepository, CustomSave {} 1.2 configuration class

If you use a namespace configuration, the repository infrastructure will try to automatically detect custom implementation fragments by scanning the classes under the package of repository. These classes need to follow the naming convention of attaching the repository-impl-postfix attribute of the namespace element to the fragment interface name. This suffix defaults to Impl. The following example shows a repository that uses the default suffix and a repository that sets a custom value for the suffix.

Configuration example for XML file

The first configuration in the previous example attempts to find a class called com.kkatma.repository.CustomUserRepositoryImpl as a custom repository implementation. The second example attempts to find com.kkarma.repository.CustomUserRepositoryMyImpl.

1.3 resolve ambiguity

If multiple implementations of matching class names are found in different packages, Spring Data uses the name of the bean object to determine which one to use.

Considering the following two custom implementations of CustomUserRepository shown earlier, the first implementation is used. Its bean is customUserRepositoryImpl, which matches the name of the fragment interface (CustomUserRepository) with the suffix Impl.

Example 8 implementation of ambiguity resolution

Package com.kkarma.impl.one;class CustomUserRepositoryImpl implements CustomUserRepository {/ / Your custom implementation} package com.kkarma.impl.two;@Component ("specialCustomImpl") class CustomUserRepositoryImpl implements CustomUserRepository {/ / Your custom implementation}

If you annotate the UserRepository interface with @ Component ("specialCustom"), then the name of Bean plus Impl matches the name defined for the repository implementation in com.kkarma.impl.two and is used instead of the first interface.

1.4 Manual assembly

If your custom implementation uses only annotation-based configuration and auto-assembly, the method shown above works well because it is treated as any other Spring Bean. If your implementation fragment Bean needs to be assembled into a container, you can declare Bean and name it according to the convention described above. The infrastructure then refers to the manually defined Bean definition by name, rather than creating one itself. The following example shows how to manually assemble a custom implementation.

Example 9 manually assemble custom implementation class objects to containers

1.5 Custom Base Repository

When you want to customize the behavior of base repository, the method described in the previous section needs to customize the interface of each repository so that all repository is affected. To change the behavior of all repository, you can create an implementation that extends the persistence technology-specific repository base class. This class then serves as a custom base class for the repository agent, as shown in the following example.

Example 10 customizes the base class of repository

Class MyRepositoryImpl extends SimpleJpaRepository {private final EntityManager entityManager; MyRepositoryImpl (JpaEntityInformation entityInformation, EntityManager entityManager) {super (entityInformation, entityManager); / / Keep the EntityManager around to used from the newly introduced methods. This.entityManager = entityManager;} @ Transactional public S save (S entity) {/ / implementation goes here}}

The final step is to make the Spring Data infrastructure aware of the custom repository base class. In the Java configuration, you can do this by using the repositoryBaseClass attribute of the @ Enable$ {store} Repositories annotation, as shown in the following example.

Example 11 uses JavaConfig to configure a custom repository base class

@ Configuration@EnableJpaRepositories (repositoryBaseClass = MyRepositoryImpl.class) class ApplicationConfiguration {… }

There is a corresponding attribute in the XML namespace, as shown in the following example.

Example 12 uses XML to configure a custom repository base class

Read here, this "how to customize the Repository of the Spring Data JPA framework" article has been introduced, want to master the knowledge of this article also need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report