A priority dependency injection method and device, electronic device, and storage medium

Through the priority dependency injection method, the problem that existing technology cannot support priority order injection is solved, priority order injection of container management objects is realized, and the maintainability and scalability of the system are improved.

CN118585272BActive Publication Date: 2024-10-15湖南长银五八消费金融股份有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411053667.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-08-02
Publication Date
2024-10-15
Estimated Expiration
2044-08-02

AI Technical Summary

Technical Problem

The existing Spring dependency injection mechanism cannot support dependency injection in priority order, and cannot meet the need to inject multiple container-managed object beans in a specific priority order in a complex system architecture.

Method used

Provide a priority dependency injection method that obtains the container management object to be scanned and its class object, traverses the target information to check whether the priority annotation is marked, and finds and injects the corresponding container management object from the Spring container according to the priority order in the annotation. .

Benefits of technology

It implements dependency injection according to the priority order of container management objects, ensuring that the container management objects with the highest priority are injected, which improves the maintainability and scalability of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118585272B_ABST
    Figure CN118585272B_ABST
Patent Text Reader

Abstract

The present application discloses a priority dependency injection method and device, an electronic device, and a storage medium, the method comprising: obtaining a container management object to be scanned; obtaining a class object of the container management object to be scanned; traversing each target information in the class object of the container management object to be scanned in turn, and checking whether the target information is marked with a pre-declared priority annotation; wherein the target information comprises attributes and methods; if it is marked with a pre-declared priority annotation, obtaining the type of the target information and the name of each container management object to be injected in the priority annotation; based on the name of each container management object to be injected, searching for each container management object to be injected in a Spring container in order from high to low priority of each container management object to be injected, until a container management object to be injected that is consistent with the type of the target information is found; and injecting the found container management object to be injected into the container management object to be scanned.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of dependency injection, and in particular to a priority dependency injection method and device, an electronic device, and a storage medium. Background Art

[0002] Spring dependency injection is one of the core features of the Spring framework, which embodies an important principle of object-oriented design: decoupling and code reuse. In Spring, dependency injection means that the dependencies between objects are not implemented by hard coding, but the Spring container dynamically injects the dependent objects into the objects that need them at runtime. This reduces the coupling between codes and improves the maintainability and scalability of the system.

[0003] Therefore, in complex system architectures, it is often necessary to inject container-managed object beans for specific functions or services. In order to achieve the injection of container-managed objects, the Spring framework currently provides some standard dependency injection mechanisms, such as annotations such as @Primary, @Qualifier, @Order, and @ConditionalOnMissingBean. Through these annotations, you can inject the only defined container-managed object bean, as well as inject a container-managed object bean selected from multiple defined container-managed object beans.

[0004] However, in some scenarios, it is hoped that multiple container-managed object beans will be injected in a specific priority order, so as to inject the container-managed object bean with the highest priority as much as possible. For example, first try to inject client A. If client A does not exist, then try to inject client B. If both client A and client B do not exist, then consider injecting client C. Obviously, the existing injection method does not support injection in priority order, so it cannot meet such requirements. Summary of the invention

[0005] Based on the above-mentioned deficiencies of the prior art, the present application provides a priority dependency injection method and device, an electronic device, and a storage medium to solve the problem that the prior art cannot support dependency injection in order of priority.

[0006] In order to achieve the above objectives, this application provides the following technical solutions:

[0007] The first aspect of the present application provides a priority dependency injection method, comprising:

[0008] Get the container management object to be scanned;

[0009] Obtain the class object of the container management object to be scanned;

[0010] Traversing each target information in the class object of the container management object to be scanned in turn, and checking whether the target information is marked with a pre-declared priority annotation; wherein the target information includes attributes and methods;

[0011] If it is checked that the target information is marked with a pre-declared priority annotation, then the type of the target information and the names of the various container management objects to be injected in the priority annotation on the target information are obtained;

[0012] Based on the names of the container-managed objects to be injected, and in descending order of priority of the container-managed objects to be injected, searching for the container-managed objects to be injected in the Spring container, until the container-managed object to be injected whose type is consistent with the type of the target information is found;

[0013] The found container management object to be injected is injected into the container management object to be scanned.

[0014] Optionally, in the above priority dependency injection method, before obtaining the container management object to be scanned, the method further includes:

[0015] Create a priority post-processing class that implements both the post-processing interface of the container-managed object and the object injection factory interface; wherein the post-processing method of the post-processing interface of the container-managed object is used to implement priority dependency injection; and the object creation method of the object injection factory interface is used to inject the Spring container into the priority post-processing class.

[0016] Optionally, in the above priority dependency injection method, the type of obtaining the target information includes:

[0017] If the target information is an attribute, obtaining the attribute type to which the target information belongs as the type of the target information;

[0018] If the target information is a method, the type of the parameter of the target information is obtained as the type of the target information.

[0019] Optionally, in the above-mentioned priority dependency injection method, the obtaining of the name of each container management object to be injected in the priority annotation on the target information includes:

[0020] extracting information of the priority annotation on the target information;

[0021] A defined priority array is extracted from the priority annotation information; wherein the priority array includes multiple names of the container management objects to be injected, and the smaller the subscript of the name of the container management object to be injected in the priority array, the higher the priority of the container management object to be injected.

[0022] Optionally, in the above-mentioned priority dependency injection method, based on the name of each container-managed object to be injected, searching for each container-managed object to be injected in the Spring container in descending order of priority of each container-managed object to be injected, until the container-managed object to be injected that is consistent with the type of the target information is found, includes:

[0023] The container object to be injected with the highest priority among the container management objects to be injected that are not currently being searched is used as the current search object;

[0024] According to the name of the current search object, find out whether the current search object exists in the Spring container;

[0025] If the current search object is found to exist in the Spring container, then the type of the current search object is compared with the type of the target information to see whether they are consistent;

[0026] If the type of the currently searched object is inconsistent with the type of the target information, or if it is found that the currently searched object does not exist in the Spring container, it is determined whether there is still a container-managed object to be injected that has not been searched;

[0027] If it is determined that there is still the container management object to be injected that has not been searched, then returning to execute the step of taking the container management object to be injected with the highest priority among the container management objects to be injected that have not been searched, as the current search object;

[0028] If it is determined that the container management object to be injected that has not been searched does not exist at present, the injection object search is terminated and it is determined that the injection object has not been found;

[0029] If the type of the current search object is found to be consistent with the type of the target information, the injection object search is terminated and it is determined that the injection object is found; wherein, when it is determined that the injection object is found, the step of injecting the found container management object to be injected into the target information is executed.

[0030] Optionally, in the above-mentioned priority dependency injection method, injecting the found container management object to be injected into the container management object to be scanned includes:

[0031] If the target information is an attribute, injecting the found container management object to be injected as an attribute into the container management object to be scanned;

[0032] If the target information is an attribute, the found container management object to be injected is used as a parameter to call a method and injected into the container management object to be scanned.

[0033] Optionally, in the above priority dependency injection method, it also includes:

[0034] If the container-managed object to be injected that is consistent with the type of the target information is not found in the Spring container, error handling is performed.

[0035] A second aspect of the present application provides a priority dependency injection device, comprising:

[0036] A management object acquisition unit, used to acquire the container management object to be scanned;

[0037] A class object acquisition unit, used to acquire the class object of the container management object to be scanned;

[0038] An annotation checking unit is used to sequentially traverse each target information in the class object of the container management object to be scanned, and check whether the target information is marked with a pre-declared priority annotation; wherein the target information includes attributes and methods;

[0039] a type acquisition unit, configured to acquire the type of the target information when it is detected that the target information is marked with a pre-declared priority annotation;

[0040] A name acquisition unit, configured to acquire the names of the container management objects to be injected in the priority annotation on the target information when it is detected that the target information is marked with a pre-declared priority annotation;

[0041] A matching unit, configured to search for each of the container-managed objects to be injected in the Spring container in descending order of priority based on the name of each of the container-managed objects to be injected, until a container-managed object to be injected whose type is consistent with that of the target information is found;

[0042] An injection unit is used to inject the found container management object to be injected into the container management object to be scanned.

[0043] Optionally, in the above priority dependency injection device, it also includes:

[0044] A creation unit is used to create a priority post-processing class that implements both the post-processing interface of a container-managed object and the object injection factory interface; wherein the post-processing method of the post-processing interface of the container-managed object is used to implement priority dependency injection; and the object creation method of the object injection factory interface is used to inject a Spring container into the priority post-processing class.

[0045] Optionally, in the above-mentioned priority dependency injection device, the type acquisition unit includes:

[0046] an attribute type acquiring unit, configured to acquire, when the target information is an attribute, the attribute type to which the target information belongs as the type of the target information;

[0047] The method type unit is used to obtain the type of the parameter of the target information as the type of the target information when the target information is a method.

[0048] Optionally, in the above-mentioned priority dependency injection device, the name acquisition unit includes:

[0049] An information extraction unit, configured to extract information of the priority annotation on the target information;

[0050] An array extraction unit is used to extract a defined priority array from the priority annotation information; wherein the priority array includes multiple names of the container management objects to be injected, and the smaller the subscript of the name of the container management object to be injected in the priority array, the higher the priority of the container management object to be injected.

[0051] Optionally, in the above-mentioned priority dependency injection device, the matching unit includes:

[0052] An object determination unit, used for taking the container object to be injected with the highest priority among the container management objects to be injected that are not currently being searched as the current search object;

[0053] An object search unit, used to search whether the current search object exists in the Spring container according to the name of the current search object;

[0054] A type comparison unit, used for comparing whether the type of the current search object is consistent with the type of the target information when finding out that the current search object exists in the Spring container;

[0055] An object judgment unit, used for judging whether there is still a container-managed object to be injected that has not been searched, when it is found that the type of the currently searched object is inconsistent with the type of the target information, or when it is found that the currently searched object does not exist in the Spring container;

[0056] A returning unit, configured to return to the object determining unit when it is determined that there is still the container management object to be injected that has not been searched;

[0057] A first result determination unit, configured to terminate the injection object search and determine that the injection object has not been found when it is determined that there is no container management object to be injected that has not been searched;

[0058] The second result determination unit is used to end the injection object search and determine that the injection object has been found when the type of the current search object is compared to the type of the target information; wherein, when it is determined that the injection object has been found, the injection unit executes the step of injecting the found container management object to be injected into the target information.

[0059] Optionally, in the above-mentioned priority dependency injection device, the injection unit includes:

[0060] A first injection unit, configured to inject the found container management to be injected as an attribute into the container management object to be scanned when the target information is an attribute;

[0061] The second injection unit is used to inject the found container management object to be injected into the container management object to be scanned by calling a method using the found container management object to be injected as a parameter when the target information is an attribute.

[0062] Optionally, in the above priority dependency injection device, it also includes:

[0063] The error handling unit is used to perform error handling when the container-managed object to be injected that is consistent with the type of the target information is not found in the Spring container.

[0064] A third aspect of the present application provides an electronic device, including:

[0065] Memory and processor;

[0066] Wherein, the memory is used to store programs;

[0067] The processor is used to execute the program, and when the program is executed, it is specifically used to implement the priority dependency injection method as described in any one of the above.

[0068] A fourth aspect of the present application provides a computer storage medium for storing a computer program, which, when executed, is used to implement the priority dependency injection method as described in any one of the above.

[0069] The embodiment of the present application provides a priority dependency injection method, which pre-introduces and declares a priority annotation so that injection can be performed according to the priority. Specifically, when injection is required, first obtain the container management object to be scanned, and obtain the class object of the container management object to be scanned. Then, traverse each target information in the class object of the container management object to be scanned in turn, and check whether the target information is marked with a pre-declared priority annotation. Among them, the target information includes attributes and methods. If it is checked that the target information is marked with a pre-declared priority annotation, the type of the target information and the name of each container management object to be injected in the priority annotation on the target information are obtained, so as to determine the container management object to be injected that can be injected based on the type of the target information and the name of the container management object to be injected. Therefore, based on the name of each container management object to be injected, in the order of the priority of each container management object to be injected from high to low, each container management object to be injected is searched from the Spring container in turn until the container management object to be injected that is consistent with the type of the target information is found, thereby finding the container management object to be injected with the highest priority among the container management objects to be injected. Finally, the found container management object to be injected is injected into the container management object to be scanned, thereby realizing the method of injecting according to the priority order of each container management object. BRIEF DESCRIPTION OF THE DRAWINGS

[0070] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.

[0071] Figure 1 A flowchart of a priority dependency injection method provided in an embodiment of the present application;

[0072] Figure 2 A flowchart of a method for obtaining the names of each container management object to be injected in the priority annotation on the target information provided by an embodiment of the present application;

[0073] Figure 3 A flowchart of a method for searching for a container management object to be injected provided in an embodiment of the present application;

[0074] Figure 4 A schematic diagram of the architecture of a priority dependency injection device provided in an embodiment of the present application;

[0075] Figure 5 A schematic diagram of the architecture of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0076] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0077] In this application, relational terms such as first and second, etc. are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "comprise", "include" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the statement "comprises a ..." does not exclude the presence of other identical elements in the process, method, article or device including the element.

[0078] The present application embodiment provides a priority dependency injection method, such as Figure 1 As shown, the following steps are included:

[0079] S101: Obtain a container management object to be scanned.

[0080] Specifically, a corresponding method may be called to receive a Bean object to be processed as a parameter as a container management object to be scanned. The container management object to be scanned is the last operation before initialization, so it needs to be processed.

[0081] Optionally, in order to implement priority dependency injection, some preparation work needs to be done first. Therefore, in another embodiment of the present application, before executing step S101, the following steps are further performed:

[0082] Create a priority postprocessing class that implements both the container-managed object postprocessing interface and the object injection factory interface.

[0083] Among them, the post-processing method of the post-processing interface of the container management object is used to implement priority dependency injection. The object creation method of the object injection factory interface is used to inject the Spring container into the priority post-processing class.

[0084] Optionally, a class named "PriorityQualifierPostProcessor" can be created, i.e., a priority post-processing class. This type implements both the container-managed object post-processing interface BeanPostProcessor and the object injection factory interface BeanFactoryAware.

[0085] Among them, by implementing the BeanFactoryAware interface, the setBeanFactory method provided by the interface can be used to inject the Spring container into the PriorityQualifierPostProcessor class, that is, inject the created priority post-processing class, thereby facilitating subsequent operations.

[0086] The core logic of the priority injection function is implemented in the postProcessBeforeInitialization method of the BeanPostProcessor interface. Therefore, the specific way to implement step S101 is to trigger the postProcessBeforeInitialization method of the BeanPostProcessor during the Spring startup process, so as to execute step S101 and then execute subsequent steps through this method.

[0087] S102: Obtain a class object of a container management object to be scanned.

[0088] Specifically, the Java class object corresponding to the container management object to be scanned can be obtained by calling the "getClass()" method of the container management object to be scanned.

[0089] S103: traverse each target information in the class object of the container management object to be scanned in turn, and check whether the target information is marked with a pre-declared priority annotation.

[0090] The target information includes properties and methods. The priority annotation is a pre-declared custom annotation used to implement injection according to priority.

[0091] Specifically, the Java reflection API can be used to sequentially traverse all the attributes and methods in the class object of the container management object to be scanned, and check whether the traversed attributes and methods are marked with the pre-declared priority annotation to determine whether the next step of processing is required. For example, if the pre-declared priority annotation is @PriorityQualifier, then the @PriorityQualifier annotation is checked to see whether it is standardized on the attributes and methods. Therefore, if it is checked that the target information is marked with the pre-declared priority annotation, step S104 is executed.

[0092] Therefore, in the embodiment of the present application, it is necessary to introduce a priority annotation in advance, for example, @PriorityQualifier, and the annotation can receive a string as a parameter, so the names of multiple container-managed objects to be injected that have a priority relationship are stored in the annotation. Usually, the annotation is sorted according to the priority, so the order of the names of the container-managed objects to be injected is the priority of the container-managed objects to be injected. For example, "mqRedisTemplate" and "globalRedisTemplate" are the names of two container-managed objects of the RedisTemplate type in the Spring container. We hope that mqRedisTemplate has a higher priority, then the priority annotation can be specifically defined as: @PriorityQualifier ({"mqRedisTemplate", "globalRedisTemplate"}). Of course, other methods can also be used to indicate the priority of the container-managed object to be injected.

[0093] Later, you can adjust the defined priority annotation on properties and methods to achieve priority injection.

[0094] S104: Acquire the type of the target information and the name of each container management object to be injected in the priority annotation of the target information.

[0095] The container-managed object to be injected refers to the container-managed object Bean to be injected.

[0096] It should be noted that the priority annotation of a target information is for the target information, so the container management object to be injected needs to be of the same type as the target information in order to inject the container management object. Therefore, it is necessary to obtain the type of the target information to determine whether the type of the container management object to be injected is consistent with the type of the target information.

[0097] Specifically, the type of the target information and the names of the container management objects to be injected in the priority annotation on the target information can also be obtained by reflection.

[0098] Optionally, in another embodiment of the present application, a specific implementation of obtaining the type of target information includes:

[0099] If the target information is an attribute, the attribute type to which the target information belongs is obtained as the type of the target information.

[0100] If the target information is a method, the type of the parameter of the target information is obtained as the type of the target information.

[0101] It should be noted that since the attribute itself has its own type, when the target information is an attribute, the attribute type of the attribute is the type of the target information. The method needs to use the type of its adopted number as the type of the method, so the type of the method parameter is used as the type of the target information.

[0102] Optionally, in another embodiment of the present application, a specific implementation method of obtaining the names of each container management object to be injected in the priority annotation on the target information is as follows: Figure 2 As shown, including:

[0103] S201. Extract priority annotation information on target information.

[0104] S202. Extract a defined priority array from the priority annotation information.

[0105] It should be noted that, since there are multiple container management objects to be injected, and each container management object with injection has a priority, and the array is used to store multiple data, and each data stored in the array will have a corresponding subscript, so in the embodiment of the present application, an array is used to store the name of each container management object to be injected, and the subscript of the name of each container management object to be injected in each array is the priority of the container management object to be injected.

[0106] Therefore, the priority array includes the names of multiple container management objects to be injected, so obtaining the priority array means obtaining the names of each container management object to be injected. Moreover, the smaller the subscript of the name of the container management object to be injected in the priority array, the higher the priority of the container management object to be injected. From left to right in the array, the subscript increases continuously, so the name to the left in the array, that is, the name to the front in the array, corresponds to the container management object to be injected with a higher priority.

[0107] S105. Based on the names of the container-managed objects to be injected, search for the container-managed objects to be injected in the Spring container in descending order of priority, until a container-managed object to be injected that is consistent with the type of the target information is found.

[0108] It should be noted that since the container-managed object to be injected defined in the priority annotation does not necessarily exist in the Spring container, and the type of the existing container-managed object to be injected is inconsistent with the type of the target information, it is necessary to search the Spring container for a matching container-managed object to be injected, that is, to find out whether there is a container-managed object to be injected whose name is consistent with the name in the priority annotation and whose type is consistent with the type of the target information, so as to inject the matching container-managed object to be injected.

[0109] Since only one container management object to be injected can be injected, and the container management object to be injected with the highest priority needs to be given priority, it is necessary to search for each container management object to be injected in order from high to low priority. When a container management object to be injected that is consistent with the type of the target information is found, it means that the container management object to be injected with the highest priority has been found, so it is not necessary to operate on the subsequent container management objects to be injected, and step S106 can be executed at this time.

[0110] Optionally, in another embodiment of the present application, a specific implementation of step S105 is as follows: Figure 3 As shown, including:

[0111] S301: Use the container object to be injected with the highest priority among the container management objects to be injected that are not currently being searched as the current search object.

[0112] S302: According to the name of the current search object, search whether the current search object exists in the Spring container.

[0113] If the Spring container finds that the current search object exists, it is necessary to further determine whether it can be injected, so step S303 is executed at this time. If the Spring container finds that the current search object does not exist, it is necessary to search for the container management object to be injected with the next priority, so step S304 is executed.

[0114] S303: Compare the type of the current search object with the type of the target information to see if they are consistent.

[0115] If the type of the current search object is inconsistent with the type of the target information, it means that the current search object cannot be injected, so it is necessary to search for the next priority container management object to be injected, so step S304 is executed at this time. If the type of the current search object is consistent with the type of the target information, it means that it can be injected, so step S306 is executed at this time.

[0116] S304: Determine whether there is any container management object to be injected that has not been searched.

[0117] If it is determined that there is still a container management object to be injected that has not been searched, the process returns to step S301 to match the container management object to be injected of the next priority.

[0118] If it is determined that there is no container management object to be injected that has not been searched, it means that all injection container management objects have been searched and injection is not possible, so only step S305 can be executed at this time.

[0119] S305: End the injection object search and determine that no injection object is found.

[0120] Since all container management objects to be injected have been searched and no further search is needed, the injection of the object needs to be ended at this time, and in order to feedback the search situation, it is determined that the injection object has not been found.

[0121] S306: End the injection object search and confirm that the injection object has been found.

[0122] Since the injection object is found at this time, there is no need to perform subsequent searches, so the injection object search is ended. In order to be able to continue to execute step S106, it is determined that the injection object is found, so as to continue to execute step S106.

[0123] Optionally, in order to enable the user to timely understand or take corresponding measures when the container management object to be injected is not found, another embodiment of the present application further includes:

[0124] If no container-managed object to be injected that is consistent with the type of the target information is found in the Spring container, error handling is performed.

[0125] Error handling may include recording error logs, throwing exceptions, or executing fallback strategies.

[0126] S106: Inject the found container management object to be injected into the container management object to be scanned.

[0127] Specifically, the found container management object to be injected is obtained, and then injected into the container management object to be scanned.

[0128] Optionally, in another embodiment of the present application, a specific implementation of step S106 includes:

[0129] If the target information is an attribute, the found container management to be injected is injected as an attribute into the container management object to be scanned.

[0130] If the target information is an attribute, the found container management object to be injected is used as a parameter to call a method and injected into the container management object to be scanned.

[0131] The embodiment of the present application provides a priority dependency injection method, which pre-introduces and declares a priority annotation so that injection can be performed according to the priority. Specifically, when injection is required, first obtain the container management object to be scanned, and obtain the class object of the container management object to be scanned. Then, traverse each target information in the class object of the container management object to be scanned in turn, and check whether the target information is marked with a pre-declared priority annotation. Among them, the target information includes attributes and methods. If it is checked that the target information is marked with a pre-declared priority annotation, the type of the target information and the name of each container management object to be injected in the priority annotation on the target information are obtained, so as to determine the container management object to be injected that can be injected based on the type of the target information and the name of the container management object to be injected. Therefore, based on the name of each container management object to be injected, in the order of the priority of each container management object to be injected from high to low, each container management object to be injected is searched from the Spring container in turn until the container management object to be injected that is consistent with the type of the target information is found, thereby finding the container management object to be injected with the highest priority among the container management objects to be injected. Finally, the found container management object to be injected is injected into the container management object to be scanned, thereby realizing the method of injecting according to the priority order of each container management object.

[0132] Another embodiment of the present application provides a priority dependency injection device, such as Figure 4 As shown, including:

[0133] The management object acquisition unit 401 is used to acquire the container management object to be scanned.

[0134] The class object acquisition unit 402 is used to acquire the class object of the container management object to be scanned.

[0135] The annotation checking unit 403 is used to sequentially traverse each target information in the class object of the container management object to be scanned, and check whether the target information is marked with a pre-declared priority annotation. The target information includes attributes and methods.

[0136] The type acquisition unit 404 is used to acquire the type of the target information when it is detected that the target information is marked with a pre-declared priority annotation.

[0137] The name acquisition unit 405 is used to acquire the names of the container management objects to be injected in the priority annotation on the target information when it is detected that the target information is marked with a pre-declared priority annotation.

[0138] The matching unit 406 is used to search for each container-managed object to be injected from the Spring container in order of priority from high to low based on the name of each container-managed object to be injected, until a container-managed object to be injected that is consistent with the type of the target information is found.

[0139] The injection unit 407 is used to inject the found container management object to be injected into the container management object to be scanned.

[0140] Optionally, in another embodiment of the present application, the priority dependency injection device further includes:

[0141] The creation unit is used to create a priority post-processing class that implements both the container-managed object post-processing interface and the object injection factory interface. The post-processing method of the container-managed object post-processing interface is used to implement priority dependency injection. The object creation method of the object injection factory interface is used to inject the Spring container into the priority post-processing class.

[0142] Optionally, in a priority dependency injection device provided in another embodiment of the present application, the type acquisition unit includes:

[0143] The attribute type acquisition unit is used to acquire the attribute type to which the target information belongs when the target information is an attribute, as the type of the target information.

[0144] The method type unit is used to obtain the type of the parameter of the target information as the type of the target information when the target information is a method.

[0145] Optionally, in a priority dependency injection device provided in another embodiment of the present application, the name acquisition unit includes:

[0146] The information extraction unit is used to extract information of priority annotation on target information.

[0147] The array extraction unit is used to extract a defined priority array from the priority annotation information. The priority array includes the names of multiple container management objects to be injected, and the smaller the subscript of the name of the container management object to be injected in the priority array, the higher the priority of the container management object to be injected.

[0148] Optionally, in a priority dependency injection device provided in another embodiment of the present application, the matching unit includes:

[0149] The object determination unit is used to use the container object to be injected with the highest priority among the container management objects to be injected that are not currently being searched as the current search object.

[0150] The object search unit is used to search whether the current search object exists in the Spring container according to the name of the current search object.

[0151] The type comparison unit is used to compare whether the type of the current search object is consistent with the type of the target information when finding out that the current search object exists in the Spring container.

[0152] The object judgment unit is used to judge whether there is a container-managed object to be injected that has not been found when the type of the current search object is inconsistent with the type of the target information, or when it is found that the current search object does not exist in the Spring container.

[0153] The returning unit is used to return to the object determining unit when it is determined that there is still a container management object to be injected that has not been searched.

[0154] The first result determination unit is used to terminate the injection object search and determine that the injection object has not been found when it is determined that there is no container management object to be injected that has not been searched.

[0155] The second result determination unit is used to end the injection object search and determine that the injection object is found when the type of the current search object is consistent with the type of the target information. When it is determined that the injection object is found, the injection unit executes the step of injecting the found container management object to be injected into the target information.

[0156] Optionally, in a priority dependency injection device provided in another embodiment of the present application, the injection unit includes:

[0157] The first injection unit is used to inject the found container management to be injected as an attribute into the container management object to be scanned when the target information is an attribute.

[0158] The second injection unit is used to inject the found container management object to be injected into the container management object to be scanned by calling a method using the found container management object to be injected as a parameter when the target information is an attribute.

[0159] Optionally, in another embodiment of the present application, the priority dependency injection device further includes:

[0160] The error handling unit is used to perform error handling when no container-managed object to be injected that is consistent with the type of the target information is found in the Spring container.

[0161] It should be noted that the specific working process of each unit provided in the above embodiments of the present application can refer to the implementation process of the corresponding steps in the above method embodiments, and will not be repeated here.

[0162] Another embodiment of the present application provides an electronic device, such as Figure 5 As shown, including:

[0163] Memory 501 and processor 502 .

[0164] The memory 501 is used to store programs.

[0165] The processor 502 is used to execute the program stored in the memory 501. When the program is executed, it is specifically used to implement the priority dependency injection method provided by any one of the above embodiments.

[0166] Another embodiment of the present application provides a computer storage medium for storing a computer program. When the computer program is executed, it is used to implement the priority dependency injection method provided in any of the above embodiments.

[0167] Computer storage media includes permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include temporary computer-readable media (transitory media), such as modulated data signals and carrier waves.

[0168] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0169] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.

Claims

1. A priority dependency injection method, characterized in that: include: Get the container management object to be scanned; Obtain the class object of the container management object to be scanned; Traversing each target information in the class object of the container management object to be scanned in turn, and checking whether the target information is marked with a pre-declared priority annotation; wherein the target information includes attributes and methods; If it is checked that the target information is marked with a pre-declared priority annotation, then the type of the target information and the names of the various container management objects to be injected in the priority annotation on the target information are obtained; Based on the names of the container-managed objects to be injected, and in descending order of priority of the container-managed objects to be injected, searching for the container-managed objects to be injected in the Spring container, until the container-managed object to be injected whose type is consistent with the type of the target information is found; The found container management object to be injected is injected into the container management object to be scanned.

2. The method according to claim 1, characterized in that: Before obtaining the container management object to be scanned, the method further includes: Create a priority post-processing class that implements both the post-processing interface of the container-managed object and the object injection factory interface; wherein the post-processing method of the post-processing interface of the container-managed object is used to implement priority dependency injection; and the object creation method of the object injection factory interface is used to inject the Spring container into the priority post-processing class.

3. The method according to claim 1, characterized in that The type of obtaining the target information includes: If the target information is an attribute, obtaining the attribute type to which the target information belongs as the type of the target information; If the target information is a method, the type of the parameter of the target information is obtained as the type of the target information.

4. The method according to claim 1, characterized in that: The obtaining the name of each to-be-injected container management object in the priority annotation on the target information includes: extracting information of the priority annotation on the target information; A defined priority array is extracted from the priority annotation information; wherein the priority array includes multiple names of the container management objects to be injected, and the smaller the subscript of the name of the container management object to be injected in the priority array, the higher the priority of the container management object to be injected.

5. The method according to claim 1, characterized in that Based on the names of the container-managed objects to be injected, searching for the container-managed objects to be injected in the Spring container in descending order of priority until the container-managed object to be injected that is consistent with the type of the target information is found, including: The container management object to be injected with the highest priority among the container management objects to be injected that are not currently being searched is used as the current search object; According to the name of the current search object, find out whether the current search object exists in the Spring container; If the current search object is found to exist in the Spring container, then the type of the current search object is compared with the type of the target information to see whether they are consistent; If the type of the currently searched object is inconsistent with the type of the target information, or if it is found that the currently searched object does not exist in the Spring container, it is determined whether there is still a container-managed object to be injected that has not been searched; If it is determined that there is still the container management object to be injected that has not been searched, then returning to execute the step of taking the container management object to be injected with the highest priority among the container management objects to be injected that have not been searched, as the current search object; If it is determined that the container management object to be injected that has not been searched does not exist at present, the injection object search is terminated and it is determined that the injection object has not been found; If the type of the current search object is found to be consistent with the type of the target information, the injection object search is terminated and it is determined that the injection object is found; wherein, when it is determined that the injection object is found, the step of injecting the found container management object to be injected into the target information is executed.

6. The method according to claim 1, characterized in that Injecting the found container management object to be injected into the container management object to be scanned includes: If the target information is an attribute, injecting the found container management object to be injected as an attribute into the container management object to be scanned; If the target information is a method, the found container management object to be injected is used as a parameter to call a method and inject it into the container management object to be scanned.

7. The method according to claim 1, characterized in that Also includes: If the container-managed object to be injected that is consistent with the type of the target information is not found in the Spring container, error handling is performed.

8. A priority dependency injection device, characterized in that: include: A management object acquisition unit, used to acquire the container management object to be scanned; A class object acquisition unit, used to acquire the class object of the container management object to be scanned; An annotation checking unit is used to sequentially traverse each target information in the class object of the container management object to be scanned, and check whether the target information is marked with a pre-declared priority annotation; wherein the target information includes attributes and methods; a type acquisition unit, configured to acquire the type of the target information when it is detected that the target information is marked with a pre-declared priority annotation; A name acquisition unit, configured to acquire the names of the container management objects to be injected in the priority annotation on the target information when it is detected that the target information is marked with a pre-declared priority annotation; A matching unit, configured to search for each of the container-managed objects to be injected in the Spring container in descending order of priority based on the name of each of the container-managed objects to be injected, until a container-managed object to be injected whose type is consistent with that of the target information is found; An injection unit is used to inject the found container management object to be injected into the container management object to be scanned.

9. An electronic device, characterized in that: include: Memory and processor; Wherein, the memory is used to store programs; The processor is used to execute the program, and when the program is executed, it is specifically used to implement the priority dependency injection method as described in any one of claims 1 to 7.

10. A computer storage medium, characterized in that: Used to store a computer program, which, when executed, is used to implement the priority dependency injection method as described in any one of claims 1 to 7.