A lookup method, system, device and storage medium

CN118467583BActive Publication Date: 2026-07-14湖南长银五八消费金融股份有限公司

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
湖南长银五八消费金融股份有限公司
Filing Date
2024-04-28
Publication Date
2026-07-14

AI Technical Summary

Technical Problem

In Spring Data JPA, the traditional dependency injection method does not support its use in interfaces, which makes it less flexible and elegant to obtain Bean objects registered in the container in declarative repository interfaces, increasing code complexity and maintenance difficulty.

Method used

Define the target annotation @BeanLookup, and use Spring Data JPA to scan the repository interfaces when the application starts, determine whether a method is declared with the annotation, and retrieve the Bean object from the Spring container based on the method's return type to achieve dependency lookup.

Benefits of technology

It provides a flexible and free dependency lookup method, reduces component coupling, simplifies persistence layer development, improves code quality and maintainability, and enhances system flexibility and maintainability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118467583B_ABST
    Figure CN118467583B_ABST
Patent Text Reader

Abstract

The application discloses a kind of dependency lookup method, system, equipment and storage medium, it is related to computer technical field, comprising: the target annotation is defined, so that the target annotation can declare each method in warehouse interface;When application starts, the warehouse interface is scanned by Spring Data JPA, obtains corresponding scanning result, according to the scanning result, whether the method in the warehouse interface is declared by the target annotation;If declared by the target annotation, information is sent to the Spring Data JPA notification, and according to the return type of the method in the warehouse interface and the name declared in the target annotation, corresponding registered Bean object is obtained from Spring container, and the Bean object is returned, to complete dependency lookup.Therefore, the application can more flexibly realize dependency lookup.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a search-dependent method, system, device, and storage medium. Background Technology

[0002] Spring Data JPA (Java Persistence API) is a sub-project of the Spring framework designed to simplify persistence layer development. It provides a declarative approach to creating data access objects (DAOs) without writing large amounts of repetitive code. By defining a Repository interface, developers can easily access database data, and Spring Data JPA automatically generates implementation classes, including common CRUD (Create, Read, Update, and Delete) operations. This approach significantly reduces the amount of code, making it easier to understand and maintain.

[0003] However, for some complex query operations or those requiring data aggregation (where data may be distributed across multiple database instances, such as multi-tenant databases, or other types of databases like Elasticsearch (ES) or MongoDB), implementing them using a declarative repository interface becomes difficult and inflexible. In such cases, an imperative approach (programming approach) is more necessary. A common practice is to add a Service or Manager layer between the business logic layer and the data access layer to store the imperative operation code, such as... Figure 1 As shown. However, this approach inadvertently increases the layers of the layered model, leading to increased code complexity and making the system more difficult to understand and maintain. To simplify the code hierarchy, a better implementation is to transform the persistence layer into a combination of declarative and imperative approaches. For example... Figure 2As shown, this design aims to reduce the layers of code structure, combining declarative and imperative approaches to fully leverage their advantages. While simplifying persistence layer development, it ensures system flexibility and maintainability, improving development efficiency and code quality. JDK 8 (Java SE Development Kit 8) introduced default methods for interfaces, allowing default implementations of methods within interfaces. This feature provides a fundamental guarantee for a persistence layer combining declarative and imperative approaches. However, retrieving registered beans from the container within a declarative repository interface presents another challenge. Specifically, traditional solutions (@Autowired and @Resource annotations are not supported in interfaces; currently, they can only be passed through method parameters) are somewhat cumbersome or inelegant. Therefore, enabling declarative repository interfaces to easily and elegantly retrieve registered beans from the container, and thus facilitating dependency injection within declarative repository interfaces, is a pressing issue. Summary of the Invention

[0004] In view of this, the purpose of this invention is to provide a dependency lookup method, system, device, and storage medium that enables more convenient use of dependency injection in declarative repository interfaces, providing developers with a more convenient and efficient development experience. The specific solution is as follows:

[0005] Firstly, this application discloses a dependency lookup method, including:

[0006] Define the target annotation so that it can declare the methods in the repository interface;

[0007] When the application starts, Spring Data JPA scans the repository interface to obtain the corresponding scan results, and determines whether the methods in the repository interface are declared by the target annotation based on the scan results.

[0008] If declared by the target annotation, a notification is sent to the Spring Data JPA, and the corresponding registered Bean object is retrieved from the Spring container according to the return type of the method in the repository interface and the name declared in the target annotation. The Bean object is then returned to complete the dependency lookup.

[0009] Optionally, after determining whether the method in the repository interface is declared by the target annotation based on the scan result, the method further includes:

[0010] If the method in the repository interface is not declared by the target annotation, and the method in the repository interface is not a default method, then Spring Data JPA will automatically generate a JPA query or operation method according to the naming rules of the method in the repository interface.

[0011] The Spring Data JPA parses the JPA query or operation method to obtain the corresponding parsing result, and maps the parsing result to the corresponding target statement.

[0012] Optionally, the target statement includes one or a combination of JPA query, add, modify, and delete statements.

[0013] Optionally, after determining whether the method in the repository interface is declared by the target annotation based on the scan result, the method further includes:

[0014] If the method in the repository interface is not declared by the target annotation, and the method in the repository interface is a preset default method, then no operation is performed.

[0015] Optionally, the step of sending a notification to Spring Data JPA if declared by the target annotation includes:

[0016] If declared by the target annotation, a notification is sent to the Spring Data JPA indicating that the method in the repository interface is not a database query or operation method.

[0017] Optionally, retrieving the corresponding registered Bean object from the Spring container based on the return type of the method in the repository interface and the name declared in the target annotation includes:

[0018] Based on the type and name of the Bean registered in the Spring container corresponding to the method in the repository interface, retrieve the corresponding registered Bean object from the Spring container.

[0019] Secondly, this application discloses a dependency lookup system, including:

[0020] The annotation definition module is used to define target annotations so that the target annotations can declare the methods in the repository interface;

[0021] The judgment module is used to scan the repository interface using Spring Data JPA when the application starts, obtain the corresponding scan results, and determine whether the methods in the repository interface are declared by the target annotation based on the scan results;

[0022] The Bean object retrieval module is used to send a notification to the Spring Data JPA if the target annotation is declared, and retrieve the corresponding registered Bean object from the Spring container according to the return type of the method in the repository interface and the name declared in the target annotation, and return the Bean object to complete the dependency lookup.

[0023] Optionally, the system further includes:

[0024] The method generation unit is used to automatically generate JPA query or operation methods according to the naming rules of the methods in the repository interface if the methods in the repository interface are not declared by the target annotation and the methods in the repository interface are not preset default methods.

[0025] The parsing result mapping unit is used to parse the JPA query or operation method through the Spring Data JPA, obtain the corresponding parsing result, and map the parsing result to the corresponding target statement.

[0026] Thirdly, this application discloses an electronic device, including:

[0027] Memory, used to store computer programs;

[0028] A processor for executing the computer program to implement the aforementioned dependency lookup method.

[0029] Fourthly, this application discloses a computer-readable storage medium for storing a computer program, which, when executed by a processor, implements the aforementioned dependency lookup method.

[0030] This application first defines a target annotation so that it can declare methods in the repository interface. When the application starts, Spring Data JPA scans the repository interface, obtains the scan results, and determines whether a method in the repository interface is declared by the target annotation based on the scan results. If it is, a notification is sent to Spring Data JPA, and the corresponding registered Bean object is retrieved from the Spring container based on the return type of the method in the repository interface and the name declared in the target annotation. The Bean object is then returned to complete the dependency lookup. Thus, this application defines an annotation that can be placed on methods in the repository interface. This annotation tells Spring Data JPA not to treat methods with this annotation as data queries, but to retrieve and return registered Bean objects from the Spring container. This provides a more flexible and free dependency lookup method, extending the capabilities of Spring Data JPA and enabling imperative data querying and aggregation in the repository interface, offering greater freedom and flexibility. On the other hand, this application uses custom annotations to implement dependency lookup in the repository interface, instead of hard-coding dependency retrieval methods in the service classes. This flexibility makes the code more modular and maintainable, reducing coupling between components. This annotation-based approach makes the code clearer, easier to understand and maintain. By combining declarative and imperative approaches, the advantages of both are fully utilized, simplifying persistence layer development while ensuring system flexibility and maintainability, improving development efficiency and code quality. Attached Figure Description

[0031] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.

[0032] Figure 1 This is a schematic diagram of a method for implementing aggregated data query in an imperative manner as disclosed in this application;

[0033] Figure 2 This is a schematic diagram illustrating a method for writing imperative code in a declarative persistence layer as disclosed in this application;

[0034] Figure 3 Here is a flowchart of a dependency lookup method disclosed in this application;

[0035] Figure 4 This is a schematic diagram of an annotation definition code disclosed in this application;

[0036] Figure 5 This is a schematic diagram of an annotation definition code disclosed in this application;

[0037] Figure 6 This is a schematic diagram of a dependency lookup system structure disclosed in this application;

[0038] Figure 7 This is a structural diagram of an electronic device disclosed in this application. Detailed Implementation

[0039] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0040] For complex queries or queries requiring data aggregation (data may be distributed across multiple database instances, such as multi-tenant databases, or other types of databases like Elasticsearch (ES, a search server) and MongoDB), implementing these using declarative repository interfaces becomes difficult and inflexible. Traditional solutions (@Autowired and @Resource annotations are not supported in interfaces; currently, they can only be passed through method parameters) are cumbersome or inelegant. To address these technical issues, this application discloses a dependency lookup method that more conveniently enables dependency injection within declarative repository interfaces.

[0041] See Figure 3 As shown, an embodiment of the present invention discloses a dependency lookup method, including:

[0042] Step S11: Define the target annotation so that the target annotation can declare the methods in the repository interface.

[0043] In this embodiment, the @BeanLookup annotation is first defined, such as Figure 4 , Figure 5As shown in the code, the `@BeanLookup` annotation declares that it can be used on methods and is retained at runtime. When the `@BeanLookup` annotation is used on a method of the repository interface, Spring notices it at startup and performs appropriate processing based on the presence of this annotation. In this way, using a custom `@Lookup` annotation, dependency lookup can be implemented in the repository interface, rather than hard-coding the dependency retrieval method in the service class. This flexibility makes the code more modular and maintainable, and reduces the coupling between components.

[0044] Step S12: When the application starts, scan the repository interface using Spring Data JPA to obtain the corresponding scan results, and determine whether the methods in the repository interface are declared by the target annotation based on the scan results.

[0045] In this embodiment, Spring Data JPA scans the FooRepository interface when the application starts and dynamically generates its implementation class based on the interface definition. After obtaining the corresponding scan results, if the methods in the repository interface are not declared by the target annotation and are not default methods, Spring Data JPA automatically generates JPA query or operation methods according to the naming rules of the methods in the repository interface. Spring Data JPA parses the JPA query or operation methods, obtains the corresponding parsing results, and maps the parsing results to the corresponding target statements. That is, when the methods in the repository interface are not declared by the @BeanLookup annotation and these methods are not default methods, Spring Data JPA will automatically generate the actual JPA query or operation methods according to the method naming rules. These methods cover common query operations, such as querying entities by ID (Identity document), saving entities, deleting entities, etc. Spring Data JPA will parse these methods and map them to the corresponding JPA query, add, modify, and delete statements. If a method in the repository interface is not declared by the target annotation, and the method in the repository interface is a default method, then no operation is performed. That is, Spring Data JPA does not process default methods. These methods can call JPA methods declared in the repository or methods annotated with @BeanLookup, providing an imperative (programmatic) way to implement various data query aggregations.

[0046] Step S13: If declared by the target annotation, send a notification to the Spring Data JPA, retrieve the corresponding registered Bean object from the Spring container according to the return type of the method in the repository interface and the name declared in the target annotation, and return the Bean object to complete the dependency lookup.

[0047] In this embodiment, when a method in the repository interface is declared with the `@BeanLookup` annotation, Spring Data JPA needs to be notified that this method is not a database query or operation method. Furthermore, the method's implementation retrieves the corresponding registered Bean object from the Spring container and returns it based on the method's return type and the name declared in the annotation (corresponding to the type and name of a Bean already registered in the container). This provides a more flexible and customizable dependency lookup method, extending the capabilities of Spring Data JPA. It enables imperative data querying and aggregation in the repository interface, offering greater freedom and flexibility.

[0048] In summary, this application first defines a target annotation so that it can declare methods in the repository interface. When the application starts, Spring Data JPA scans the repository interface to obtain the corresponding scan results. Based on the scan results, it determines whether the methods in the repository interface are declared by the target annotation. If declared by the target annotation, a notification is sent to Spring Data JPA, and the corresponding registered Bean object is retrieved from the Spring container based on the return type of the method in the repository interface and the name declared in the target annotation. The Bean object is then returned to complete the dependency lookup. As can be seen, this application defines an annotation that can be placed on methods in the repository interface. This annotation tells Spring Data JPA not to treat methods with this annotation as data queries, but to retrieve and return registered Bean objects from the Spring container. This provides a more flexible and free dependency lookup method, extending the capabilities of Spring Data JPA and enabling imperative data querying and aggregation in the repository interface, offering greater freedom and flexibility. On the other hand, this application uses custom annotations to implement dependency lookup in the repository interface, instead of hard-coding dependency retrieval methods in the service classes. This flexibility makes the code more modular and maintainable, reducing coupling between components. This annotation-based approach makes the code clearer, easier to understand and maintain. By combining declarative and imperative approaches, the advantages of both are fully utilized, simplifying persistence layer development while ensuring system flexibility and maintainability, improving development efficiency and code quality.

[0049] See Figure 6 As shown, an embodiment of the present invention discloses a dependency lookup system, comprising:

[0050] Annotation definition module 11 is used to define target annotations so that the target annotations can declare the methods in the repository interface;

[0051] The judgment module 12 is used to scan the repository interface through Spring Data JPA when the application starts, obtain the corresponding scan results, and determine whether the method in the repository interface is declared by the target annotation based on the scan results;

[0052] The Bean object retrieval module 13 is used to send a notification to the Spring Data JPA if the Bean is declared by the target annotation, and retrieve the corresponding registered Bean object from the Spring container according to the return type of the method in the repository interface and the name declared in the target annotation, and return the Bean object to complete the dependency lookup.

[0053] This application first defines a target annotation so that it can declare methods in the repository interface. When the application starts, Spring Data JPA scans the repository interface, obtains the scan results, and determines whether a method in the repository interface is declared by the target annotation based on the scan results. If it is, a notification is sent to Spring Data JPA, and the corresponding registered Bean object is retrieved from the Spring container based on the return type of the method in the repository interface and the name declared in the target annotation. The Bean object is then returned to complete the dependency lookup. Thus, this application defines an annotation that can be placed on methods in the repository interface. This annotation tells Spring Data JPA not to treat methods with this annotation as data queries, but to search for and return registered Bean objects from the Spring container. This provides a more flexible and free dependency lookup method, expanding the capabilities of Spring Data JPA and enabling imperative data querying and aggregation in the repository interface, offering greater freedom and flexibility. On the other hand, this application uses custom annotations to implement dependency lookup in the repository interface, instead of hard-coding dependency retrieval methods in the service classes. This flexibility makes the code more modular and maintainable, reducing coupling between components. This annotation-based approach makes the code clearer, easier to understand and maintain. By combining declarative and imperative approaches, the advantages of both are fully utilized, simplifying persistence layer development while ensuring system flexibility and maintainability, improving development efficiency and code quality.

[0054] In some specific embodiments, the system may further include:

[0055] The method generation unit is used to automatically generate JPA query or operation methods according to the naming rules of the methods in the repository interface if the methods in the repository interface are not declared by the target annotation and the methods in the repository interface are not preset default methods.

[0056] The parsing result mapping unit is used to parse the JPA query or operation method through the Spring Data JPA, obtain the corresponding parsing result, and map the parsing result to the corresponding target statement.

[0057] In some specific embodiments, the system may further include:

[0058] The judgment result execution module is used to perform no operation if the method in the repository interface is not declared by the target annotation and the method in the repository interface is a preset default method.

[0059] In some specific embodiments, the Bean object acquisition module 13 may specifically include:

[0060] The information notification sending unit is used to send a notification to the Spring Data JPA that the method in the repository interface is not a database query or operation method if declared by the target annotation.

[0061] In some specific embodiments, the Bean object acquisition module 13 may specifically include:

[0062] The Bean object retrieval unit is used to retrieve the corresponding registered Bean object from the Spring container based on the type and name of the Bean registered in the Spring container corresponding to the method in the repository interface.

[0063] Furthermore, embodiments of this application also disclose an electronic device, Figure 7 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content of the diagram should not be construed as limiting the scope of this application.

[0064] Figure 7 This is a schematic diagram of the structure of an electronic device 20 provided in an embodiment of this application. Specifically, the electronic device 20 may include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 stores a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the dependency lookup method disclosed in any of the foregoing embodiments. Furthermore, the electronic device 20 in this embodiment may specifically be an electronic computer.

[0065] In this embodiment, the power supply 23 is used to provide operating voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and external devices, and the communication protocol it follows can be any communication protocol applicable to the technical solution of this application, and is not specifically limited here; the input / output interface 25 is used to acquire external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs, and is not specifically limited here.

[0066] In addition, the memory 22, as a carrier for resource storage, can be a read-only memory, random access memory, disk, or optical disk, etc. The resources stored thereon can include an operating system 221, computer programs 222, etc., and the storage method can be temporary storage or permanent storage.

[0067] The operating system 221 is used to manage and control the various hardware devices on the electronic device 20 and the computer program 222, which may be Windows Server, Netware, Unix, Linux, etc. In addition to including a computer program capable of performing the dependency lookup method executed by the electronic device 20 as disclosed in any of the foregoing embodiments, the computer program 222 may further include a computer program capable of performing other specific tasks.

[0068] Furthermore, this application also discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, it implements the aforementioned disclosed dependency lookup method. Specific steps of this method can be found in the corresponding content disclosed in the foregoing embodiments, and will not be repeated here.

[0069] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to in the method section.

[0070] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0071] The steps of the methods or algorithms described in conjunction with the embodiments disclosed herein can be implemented directly by hardware, a software module executed by a processor, or a combination of both. The software module can be located in random access memory (RAM), main memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium known in the art.

[0072] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0073] The technical solutions provided in this application have been described in detail above. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the methods and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.

Claims

1. A dependency lookup method, characterized in that, include: Define the target annotation so that it can declare the methods in the repository interface; When the application starts, Spring Data JPA scans the repository interface to obtain the corresponding scan results, and determines whether the methods in the repository interface are declared by the target annotation based on the scan results. If declared by the target annotation, a notification is sent to the Spring Data JPA, and the corresponding registered Bean object is retrieved from the Spring container according to the return type of the method in the repository interface and the name declared in the target annotation. The Bean object is then returned to complete the dependency lookup.

2. The dependency lookup method according to claim 1, characterized in that, After determining whether the method in the repository interface is declared by the target annotation based on the scan results, the process further includes: If the method in the repository interface is not declared by the target annotation, and the method in the repository interface is not a default method, then Spring Data JPA will automatically generate a JPA query or operation method according to the naming rules of the method in the repository interface. The Spring Data JPA parses the JPA query or operation method to obtain the corresponding parsing result, and maps the parsing result to the corresponding target statement.

3. The dependency lookup method according to claim 2, characterized in that, The target statement includes one or more of the following: JPA query, add, modify, and delete statements.

4. The dependency lookup method according to claim 1, characterized in that, After determining whether the method in the repository interface is declared by the target annotation based on the scan results, the process further includes: If the method in the repository interface is not declared by the target annotation, and the method in the repository interface is a preset default method, then no operation is performed.

5. The dependency lookup method according to claim 1, characterized in that, If declared by the target annotation, a notification is sent to the Spring Data JPA, including: If declared by the target annotation, a notification is sent to the Spring Data JPA indicating that the method in the repository interface is not a database query or operation method.

6. The dependency lookup method according to any one of claims 1 to 5, characterized in that, The step of retrieving the corresponding registered Bean object from the Spring container based on the return type of the method in the repository interface and the name declared in the target annotation includes: Based on the type and name of the Bean registered in the Spring container corresponding to the method in the repository interface, retrieve the corresponding registered Bean object from the Spring container.

7. A dependency lookup system, characterized in that, include: The annotation definition module is used to define target annotations so that the target annotations can declare the methods in the repository interface; The judgment module is used to scan the repository interface using Spring Data JPA when the application starts, obtain the corresponding scan results, and determine whether the methods in the repository interface are declared by the target annotation based on the scan results; The Bean object retrieval module is used to send a notification to the Spring Data JPA if the target annotation is declared, and retrieve the corresponding registered Bean object from the Spring container according to the return type of the method in the repository interface and the name declared in the target annotation, and return the Bean object to complete the dependency lookup.

8. The dependency lookup system according to claim 7, characterized in that, Also includes: The method generation unit is used to automatically generate JPA query or operation methods according to the naming rules of the methods in the repository interface if the methods in the repository interface are not declared by the target annotation and the methods in the repository interface are not preset default methods. The parsing result mapping unit is used to parse the JPA query or operation method through the Spring Data JPA, obtain the corresponding parsing result, and map the parsing result to the corresponding target statement.

9. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor for executing the computer program to implement the dependency lookup method as described in any one of claims 1 to 6.

10. A computer-readable storage medium, characterized in that, Used to store a computer program, which, when executed by a processor, implements the dependency lookup method as described in any one of claims 1 to 6.