A method for injecting a service instance and related device
By automatically injecting service instances using annotation files in bytecode files within the JVM environment, the problem of high coupling in the service instantiation process of existing technologies is solved, realizing a service instantiation method with low coupling and low dependency, thus improving development efficiency.
Patent Information
- Application Number
- CN202210647672.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-08
- Publication Date
- 2026-01-30
- Estimated Expiration
- 2042-06-08
AI Technical Summary
Existing technologies require manual mapping of interfaces and service layers during service instantiation in a JVM environment, and rely on source code-level reflection, resulting in high coupling and impacting the efficiency of parallel development.
By obtaining the annotation files in the bytecode file, the target interface and service are determined based on the variable information, and the service instance is automatically instantiated, avoiding manual mapping and third-party library dependencies, and automatic injection is performed using the information in the annotation files.
It reduces interface coupling, decreases runtime overhead, supports parallel development of multiple modules, simplifies the development process, and improves compilation and debugging speed.
Smart Images

Figure CN115129298B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a method and related equipment for injecting service instances. Background Technology
[0002] The JVM (Java Virtual Machine) is a specification for computing devices. It's a virtual computer that simulates various computer functions on a real computer to enable program execution. Large-scale JVM-based functionalities often involve parallel development by multiple developers, modules, and components. Therefore, interactions exist between these modules and components. To reduce coupling and ensure parallel development isn't affected, these interactions are typically implemented through API calls. Since each service provider module may be independent and reused in multiple places, and have different types of input sources, service instantiation is required at the interface variable level.
[0003] To achieve service instantiation, manual mapping is typically established for the interface layer, service layer, and business layer. Manually establishing mappings requires creating all mappings during initialization, which is a rather crude approach. Alternatively, auto-service or Dagger can be used to register and map services using libraries, but these only support source-level service collection and rely on runtime reflection. Summary of the Invention
[0004] The technical problem to be solved by the present invention is to provide a service instance injection method and related equipment to address the shortcomings of the existing technology.
[0005] To solve the above-mentioned technical problems, the technical solution adopted by the present invention is as follows:
[0006] A method for injecting a service instance, the method comprising:
[0007] Obtain a bytecode file, wherein the bytecode file includes a first annotation file, a second annotation file, and a third annotation file, the first annotation file includes several marker interfaces, the second annotation file includes several service information, and the third annotation file includes several variable information;
[0008] For each variable information, based on the interface type corresponding to the variable information, determine whether there is a target interface in the marked interface that corresponds to the variable information;
[0009] If a target interface corresponding to the variable information exists, then the target service in the service information is determined based on the target interface;
[0010] The target service is instantiated to obtain a service instance corresponding to the target interface.
[0011] The service instance injection method includes a second annotation file that further includes an annotation interface file and a fourth annotation file. The annotation interface file includes an annotation interface and the object type corresponding to the annotation interface. The fourth annotation file includes several implementation classes and the implementation number corresponding to each implementation class.
[0012] The service instance injection method, wherein the third annotation file further includes a variable number corresponding to each variable information; before determining whether there is a target interface corresponding to the variable information in the marked interface according to the interface type corresponding to the variable information for each variable information, the method includes:
[0013] For each variable information, the target interface corresponding to the variable information in the annotation interface file and the target implementation class corresponding to the variable information in the fourth annotation file are determined according to the variable number of the variable information;
[0014] Determine whether the first generic type of the target interface belongs to the interface type of the first annotation file;
[0015] If so, the object of the target implementation class is instantiated to obtain the target service corresponding to the variable information.
[0016] The service instance injection method, wherein, for each variable information, determining the target interface corresponding to the variable information in the annotation interface file and the target implementation class corresponding to the variable information in the fourth annotation file based on the variable number of the variable information includes:
[0017] Determine whether the variable number is equal to the preset default number value;
[0018] If the variable number is not equal to the default number value, then the candidate implementation class corresponding to the variable information in the fourth annotation file is determined based on the variable number and the implementation number.
[0019] Based on the annotation interfaces in the annotation interface file, determine the target interface and target implementation class corresponding to the variable information.
[0020] The service instance injection method, wherein determining the target interface and target implementation class corresponding to the variable information based on the annotation interface in the annotation interface file includes:
[0021] The annotation interface corresponding to the candidate implementation class in the annotation interface file is used as the comparison interface to determine whether the interface type of the comparison interface is the same as the interface type of the variable information.
[0022] If they are the same, the candidate implementation class is taken as the target implementation class, and the comparison interface is taken as the target interface.
[0023] The service instance injection method, wherein determining the target service in the service information based on the target interface includes:
[0024] Based on the target interface, determine the service data corresponding to the variable information in the service information;
[0025] When the number of service data is equal to one, the service data is determined to be the target service.
[0026] When the number of service data is not equal to one, the target service corresponding to the target interface is determined according to the number of services corresponding to the target interface.
[0027] The service instance injection method, wherein determining the target service corresponding to the target interface based on the number of services corresponding to the target interface includes:
[0028] If the number of services corresponding to the target interface is zero, then the preset default service will be used as the target service corresponding to the target interface.
[0029] A service instance injection apparatus, the apparatus comprising:
[0030] The acquisition module is used to acquire bytecode files, wherein the bytecode files include a first annotation file, a second annotation file, and a third annotation file. The first annotation file includes several marker interfaces, the second annotation file includes several service information, and the third annotation file includes several variable information.
[0031] The first determining module is used to determine, for each variable information, whether there is a target interface in the marked interface that corresponds to the variable information, based on the interface type corresponding to the variable information;
[0032] The second determining module is used to determine the target service in the service information based on the target interface if there is a target interface corresponding to the variable information.
[0033] The instance module is used to instantiate the target service to obtain a service instance corresponding to the target interface.
[0034] A computer-readable storage medium storing one or more programs that can be executed by one or more processors to implement the steps in the service instance injection method as described above.
[0035] A terminal device includes: a processor, a memory, and a communication bus; the memory stores a computer-readable program that can be executed by the processor;
[0036] The communication bus enables communication between the processor and the memory;
[0037] When the processor executes the computer-readable program, it implements the steps in the service instance injection method as described above.
[0038] Beneficial Effects: This invention provides a service instance injection method and related equipment. The method includes obtaining a bytecode file containing a first annotation file recording a marked interface, a second annotation file containing service information, and a third annotation file containing variable information. The method iterates through the variable information in the third annotation file, searching for the target interface corresponding to each variable information based on its interface type within the marked interfaces. After obtaining the target interface, the method searches for the corresponding target service from the service information, and finally instantiates the target service to obtain the service instance corresponding to the variable information. This process requires no third-party libraries and has no requirements for the input source; simply adding the marked interface, service information, and variable information to the corresponding annotation file is sufficient. It is simple and convenient, and does not incur runtime overhead or affect the call stack. Furthermore, it eliminates the need to manually map the relationship between interfaces and implementations, and does not require framework initialization. The entire process has low coupling requirements, and the dependencies between the interface user and the interface implementer are low, facilitating parallel development of multiple modules and components. Attached Figure Description
[0039] Figure 1 This is a flowchart of a typical interface layer, basic service layer, and business layer.
[0040] Figure 2 A flowchart of the injection method for the service instance provided by the present invention.
[0041] Figure 3 The code example diagram shows the method for injecting service instances provided by the present invention, which determines the target service based on the first annotation file and the second annotation file.
[0042] Figure 4 Another code example diagram is provided for the injection method of the service instance provided by the present invention, which determines the target service based on the first annotation file and the second annotation file.
[0043] Figure 5 The first flowchart of the service instance injection method provided by the present invention is for determining the target service.
[0044] Figure 6 The second flowchart in the service instance injection method provided by the present invention is for determining the target service in the service instance provided by the present invention.
[0045] Figure 7 A schematic diagram of an injection device for a service example provided by the present invention.
[0046] Figure 8 The structural schematic diagram of the terminal device provided by the present invention. Detailed Implementation
[0047] This invention provides a method for injecting service instances. To make the objectives, technical solutions, and effects of this invention clearer and more explicit, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only for explaining the invention and are not intended to limit the invention.
[0048] Those skilled in the art will understand that, unless specifically stated otherwise, the singular forms “a,” “an,” “the,” and “the” used herein may also include the plural forms. It should be further understood that the term “comprising” as used in this specification means the presence of the stated features, integers, steps, operations, elements, and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and / or groups thereof. It should be understood that when we say an element is “connected” or “coupled” to another element, it can be directly connected or coupled to the other element, or there may be intermediate elements. Furthermore, “connected” or “coupled” as used herein can include wireless connections or wireless coupling. The term “and / or” as used herein includes all or any units and all combinations of one or more associated listed items.
[0049] It will be understood by those skilled in the art that, unless otherwise defined, all terms used herein (including technical and scientific terms) have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains. It should also be understood that terms such as those defined in general dictionaries should be understood to have the same meaning as in the context of the prior art, and should not be interpreted in an idealized or overly formal sense unless specifically defined as herein.
[0050] like Figure 2 As shown, this implementation provides a method for injecting service instances. For ease of explanation, a common server is used as the execution subject for description. The server here can be replaced by a device with data processing capabilities, such as a tablet or computer. The method for injecting service instances includes the following steps:
[0051] S10. Obtain the bytecode file.
[0052] Specifically, the first step is to obtain the bytecode files, which are JVM executable files processed by the compiler. These bytecode files include the first annotation file (class A), the second annotation file (class B), and the third annotation file (class C).
[0053] like Figure 3 As shown, the first annotation file is used to record and mark the interface; therefore, the interface marked by the first annotation file is called the marked interface. The second annotation file is used to mark the implementation of the interface, i.e., the specific service information. The third annotation file is used to mark the variables to be injected, i.e., variable information. It is worth noting that the annotation files here include not only the content of the annotations themselves, but also the object that each annotation content points to, so as to facilitate the direct determination of the correspondence based on the annotations later.
[0054] When compiling a project, all source code is contained within it. In a JVM environment, all source code is ultimately compiled into bytecode files. Therefore, analyzing and modifying the bytecode only needs to be done at the end of the compilation process; no additional steps are required during the preceding compilation. Filtering can be achieved by specifying the directory of each bytecode file, specifying files to include, and specifying files to exclude. Based on the user's specified storage paths and filenames for the first, second, and third annotation files, filtering can be performed on all bytecode files to obtain the first, second, and third annotation files.
[0055] Furthermore, since bytecode files can be compiled from multiple languages, the obtained bytecode files, in addition to the aforementioned first, second, and third annotation files, also include other types and functions of files. After obtaining the bytecode files, they can be filtered to obtain the first, second, and third annotation files.
[0056] S20. For each variable information, determine whether there is a target interface in the marked interface that corresponds to the variable information, based on the interface type corresponding to the variable information.
[0057] Specifically, the third annotation file contains multiple variable information that needs to be injected. For each variable, its corresponding service instance needs to be determined. This embodiment uses a loop to traverse the variable information to avoid parallel processing and improve the accuracy of subsequent steps, until all variable information has been traversed and processed. During the traversal, if there are any abnormal situations such as missing variable information or errors, the variable information can be skipped and the next variable information can be processed.
[0058] For example, for the Nth (N is a positive integer) variable information, each variable information has a pre-defined corresponding interface type. In this embodiment, the interface type here is named "interface type". Correspondingly, the first annotation file contains a marker interface. Based on whether the interface type of the marker interface is the same as the interface type, it can be determined whether there exists a target interface in the marker interface that corresponds to the variable information. The target interface refers to the marker interface in the marker interface that corresponds to the variable information.
[0059] If the target interface does not exist in the first annotation file, it is directly determined that there is no service that can correspond to the variable. Therefore, the preset error message is sent directly, and the process jumps to the (N+1)th variable information for another judgment.
[0060] Error messages can be sent to project personnel via various communication software or methods such as email, telephone, and SMS, so that they can quickly correct the first or second annotation file.
[0061] Furthermore, the service creation process may require multiple parameters. For example, the service instance may need to be a singleton, making the service creation method relatively simple. Therefore, in this embodiment, the bytecode file also includes an annotation interface file and a fourth annotation file. The annotation interface file includes annotation interfaces, which are used to describe the object type, creation method, and / or destruction method of the annotation interface.
[0062] The fourth annotation file is used to mark several implementation classes of the annotated interface in the fourth annotation, and the implementation number corresponding to each implementation class.
[0063] Compared to the fourth annotation file, the third annotation file includes the variable number corresponding to each variable information. Therefore, for each variable information, the target interface corresponding to that variable information in the annotation interface file and the target implementation class corresponding to that variable information in the fourth annotation file are first determined.
[0064] Determine whether the variable number corresponding to the variable information is a preset default number value, where the default number value is not equal to the implementation number. For example... Figure 3 and Figure 4 As shown, the default preset number is -1, so we check if the variable number is -1. If they are equal, the variable information can be processed according to the normal procedure, that is, the target interface is determined directly based on the variable information as mentioned above. If they are not equal, the variable number and the implementation number are compared to determine if there is an implementation number that is equal to the variable. If there is, the implementation class corresponding to the implementation number in the fourth annotation file is determined, and this implementation class is used as a candidate implementation class corresponding to the variable information; if there is no such implementation class, the preset error message is sent to the administrator via the email or other methods mentioned above.
[0065] In the first implementation, after obtaining the candidate implementation class, since the fourth annotation file is for the annotation interface file, the candidate implementation class is mapped to the interface in the annotation interface file to determine the annotation interface corresponding to the variable information, which is then used as the target interface, and the candidate implementation class is used as the target implementation class.
[0066] In another implementation, even if the implementation class corresponding to the variable information can be determined based on the variable number and implementation number, the fourth annotation file is for annotation interface files. Therefore, it is uncertain whether the annotation interface in the fourth annotation file can be used for the variable information. Figure 5 As shown, the implementation class corresponding to the variable information is selected as the candidate implementation class. The interface type of the annotation interface corresponding to the candidate implementation class in the annotation interface file is compared with the interface type of the variable information. If they are the same, the candidate implementation class is selected as the target implementation class; otherwise, a preset error message is sent.
[0067] After obtaining the target implementation class and the target interface, it is determined whether the first generic type of the target interface belongs to the interface type of the first annotation file, that is, whether the interface type in the first annotation file contains the first generic type of the target interface. If so, an object of the target implementation class is instantiated to obtain the target service corresponding to the variable information.
[0068] This approach allows development to focus solely on compiling services relevant to the core business logic, eliminating the need to adjust other services. Consequently, the amount of code requiring compilation is significantly reduced, dramatically improving compilation and debugging speed. Furthermore, the service delivery method is highly customizable and supports a wider range of scenarios.
[0069] S30. If there is a target interface corresponding to the variable information, then determine the target service in the service information based on the target interface.
[0070] Specifically, if a target interface corresponding to this variable information exists, it is necessary to further determine whether a corresponding service exists in the virtual machine's bytecode file.
[0071] Service information is stored in a third annotation file. After obtaining the target interface, the third annotation file is used to determine whether there is service information corresponding to the target interface, i.e., the target service, based on the function and other parameters of the target interface.
[0072] Generally speaking, for a variable and a target interface, there should be a unique object to implement it. Therefore, in the usual selection, the criterion is whether there is a unique object in the third annotation file that implements the target interface of the first annotation.
[0073] First, based on the target interface, it can be determined whether service data corresponding to the variable information exists in the second annotation file. If the number of service data corresponding to the variable information is one, then the uniquely corresponding service data is directly used as the target service. If the number of service data corresponding to the variable information is not equal to one, it indicates that there are multiple service information corresponding to the target interface or no service information corresponding to the target interface. In this case, a preset error message can be displayed, which can be sent to administrators via the aforementioned email or other methods.
[0074] Furthermore, in actual business development, if a service is under development, it may be unable to be integrated into the project and packaged together, which will affect the flexibility of debugging other services to some extent. To solve this problem, parameters for the project construction that the service needs to participate in can be added to the first annotation file, and dynamic proxies can be used when calling the parameters. The project construction parameters can include the number of services corresponding to each marked interface, where the number of services refers to the number of service implementations that the marked interface can subsequently connect to and call.
[0075] Therefore, the number of services can be set to a preset null value, such as... Figure 6 As shown, an empty value indicates that the target interface does not need to obtain service information from the second annotation file. Therefore, when the number of service data is not equal to one, the framework's internal dynamic proxy is invoked to create a default proxy implementation for the target interface, using the preset default service as the target service corresponding to the target interface. At this time, the target service is internally preset. During the modification, development, or packaging of the service information that should correspond to the target interface, the preset default service can temporarily replace that service information to provide certain services, thus avoiding the inability to serve the current variable information. Furthermore, when the number of services is a positive integer greater than one, such as two, it means that the target interface can select two service information from the corresponding service information as objects for subsequent instantiation. This selection can be random, or the service information can be filtered based on its size, function, or other information to obtain the target service.
[0076] S40. Instantiate the target service to obtain a service instance corresponding to the target interface.
[0077] Specifically, after identifying the target service, it is instantiated, and then the instantiated target service is assigned to the variables annotated in the target interface, thus obtaining the service instance of the variable information being traversed. Since the target interface corresponds to the variable information being traversed, the service instance obtained after instantiating the target service can provide services for the variable information when the variable information is injected through the target interface.
[0078] Based on the above-described service instance injection method, this invention also provides a service instance injection apparatus, such as... Figure 7 As shown, the device includes:
[0079] The acquisition module is used to acquire bytecode files, wherein the bytecode files include a first annotation file, a second annotation file, and a third annotation file. The first annotation file includes several marker interfaces, the second annotation file includes several service information, and the third annotation file includes several variable information.
[0080] The first determining module is used to determine, for each variable information, whether there is a target interface in the marked interface that corresponds to the variable information, based on the interface type corresponding to the variable information;
[0081] The second determining module is used to determine the target service in the service information based on the target interface if there is a target interface corresponding to the variable information.
[0082] The instance module is used to instantiate the target service to obtain a service instance corresponding to the target interface.
[0083] The second annotation file further includes an annotation interface file and a fourth annotation file. The annotation interface file includes an annotation interface and the object type corresponding to the annotation interface. The fourth annotation file includes several implementation classes and the implementation number corresponding to each implementation class.
[0084] The service instance injection device further includes a service modification module, which includes:
[0085] The first determining unit is used to determine, for each variable information, the target interface corresponding to the variable information in the annotation interface file and the target implementation class corresponding to the variable information in the fourth annotation file, based on the variable number of the variable information;
[0086] The judgment unit is used to determine whether the first generic type of the target interface belongs to the interface type of the first annotation file;
[0087] An instance unit is used to instantiate an object of the target implementation class if the condition is met, thereby obtaining the target service corresponding to the variable information.
[0088] The determining unit includes:
[0089] The judgment subunit is used to determine whether the variable number is equal to the preset default number value;
[0090] The first determining subunit is used to determine the candidate implementation class corresponding to the variable information in the fourth annotation file based on the variable number and the implementation number if the variable number is not equal to the default number value.
[0091] The second determining subunit is used to determine the target interface and target implementation class corresponding to the variable information based on the annotation interface in the annotation interface file.
[0092] Specifically, the second determining subunit is used for:
[0093] The annotation interface corresponding to the candidate implementation class in the annotation interface file is used as the comparison interface to determine whether the interface type of the comparison interface is the same as the interface type of the variable information.
[0094] If they are the same, the candidate implementation class is taken as the target implementation class, and the comparison interface is taken as the target interface.
[0095] The determining module includes:
[0096] Based on the target interface, determine the service data corresponding to the variable information in the service information;
[0097] The second determining unit is used to determine that the service data is the target service when the number of service data is equal to one.
[0098] The third determining unit is used to determine the target service corresponding to the target interface based on the number of services corresponding to the target interface when the number of service data is not equal to one.
[0099] Specifically, the third determining unit is used for:
[0100] If the number of services corresponding to the target interface is zero, then the preset default service will be used as the target service corresponding to the target interface.
[0101] Based on the injection method of the above service instance, the present invention also provides a terminal device, such as... Figure 8 As shown, it includes at least one processor 20; a display screen 21; and a memory 22, and may also include a communications interface 23 and a bus 24. The processor 20, display screen 21, memory 22, and communications interface 23 can communicate with each other via the bus 24. The display screen 21 is configured to display a preset user guide interface in the initial setup mode. The communications interface 23 can transmit information. The processor 20 can invoke logical commands stored in the memory 22 to execute the methods described in the above embodiments.
[0102] Furthermore, the logical commands in the aforementioned memory 22 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium.
[0103] The memory 22, as a computer-readable storage medium, can be configured to store software programs, computer-executable programs, such as program commands or modules corresponding to the methods in the embodiments of this disclosure. The processor 20 executes functional applications and data processing by running the software programs, commands, or modules stored in the memory 22, thereby implementing the methods in the above embodiments.
[0104] The memory 22 may include a program storage area and a data storage area. The program storage area may store the operating system and application programs required for at least one function; the data storage area may store data created based on the use of the terminal device. Furthermore, the memory 22 may include high-speed random access memory (RAM) and non-volatile memory. Examples include various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks; it may also be a transient computer-readable storage medium.
[0105] Furthermore, the specific process of loading and executing multiple command processors in the aforementioned computer-readable storage medium and terminal device has been described in detail in the above method, and will not be repeated here.
[0106] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for injecting a service instance, characterized in that, The method comprises: acquiring a bytecode file, wherein the bytecode file comprises a first annotation file, a second annotation file and a third annotation file, the first annotation file comprises a plurality of marker interfaces, the second annotation file comprises a plurality of service information, and the third annotation file comprises a plurality of variable information; for each variable information, determining whether a target interface corresponding to the variable information exists in the marker interfaces according to an interface type corresponding to the variable information; if the target interface corresponding to the variable information exists, determining a target service in the service information according to the target interface; instantiating the target service to obtain a service instance corresponding to the target interface; wherein the bytecode file further comprises an annotation interface file and a fourth annotation file, wherein the annotation interface file comprises a plurality of annotation interfaces and object types corresponding to the annotation interfaces, and the fourth annotation file comprises a plurality of implementation classes and implementation numbers corresponding to each implementation class; wherein the third annotation file further comprises a variable number corresponding to each variable information; before the step of determining whether a target interface corresponding to the variable information exists in the marker interfaces according to an interface type corresponding to the variable information for each variable information, the method comprises: for each variable information, determining a target interface corresponding to the variable information in the annotation interface file and a target implementation class corresponding to the variable information in the fourth annotation file according to a variable number of the variable information; determining whether a first generic type of the target interface belongs to an interface type of the first annotation file; if yes, instantiating an object of the target implementation class to obtain a target service corresponding to the variable information; wherein the step of determining a target interface corresponding to the variable information in the annotation interface file and a target implementation class corresponding to the variable information in the fourth annotation file according to a variable number of the variable information for each variable information comprises: determining whether the variable number is equal to a preset default number value; if the variable number is not equal to the default number value, determining a candidate implementation class corresponding to the variable information in the fourth annotation file according to the variable number and the implementation number; determining a target interface and a target implementation class corresponding to the variable information according to an annotation interface in the annotation interface file.
2. The method of claim 1, wherein the service instance is injected into the virtual machine. the step of determining a target interface and a target implementation class corresponding to the variable information according to an annotation interface in the annotation interface file comprises: taking an annotation interface corresponding to a candidate implementation class in the annotation interface file as a comparison interface, and determining whether an interface type of the comparison interface is the same as an interface type of the variable information; if yes, taking the candidate implementation class as the target implementation class, and taking the comparison interface as the target interface.
3. The method of claim 1 or 2, wherein the service instance is injected by, the step of determining a target service in the service information according to the target interface comprises: determining service data corresponding to the variable information in the service information according to the target interface; when a quantity of the service data is equal to one, determining the service data as the target service; When the number of the service data is not equal to one, a target service corresponding to the target interface is determined according to a number of services corresponding to the target interface.
4. The method of claim 3, wherein the service instance is injected by, The determining the target service corresponding to the target interface according to the number of services corresponding to the target interface comprises: When the number of services corresponding to the target interface is zero, a preset default service is taken as the target service corresponding to the target interface.
5. An injection apparatus of a service instance, characterized by, The apparatus comprises: The obtaining module is configured to obtain a bytecode file, wherein the bytecode file comprises a first annotation file, a second annotation file and a third annotation file, the first annotation file comprises a plurality of marker interfaces, the second annotation file comprises a plurality of service information, and the third annotation file comprises a plurality of variable information; The first determining module is configured to, for each variable information, determine whether a target interface corresponding to the variable information exists in the marker interfaces according to an interface type corresponding to the variable information. The second determining module is configured to, if the target interface corresponding to the variable information exists, determine a target service in the service information according to the target interface. The instance module is configured to instantiate the target service to obtain a service instance corresponding to the target interface. The bytecode file further comprises an annotation interface file and a fourth annotation file, wherein the annotation interface file comprises an annotation interface and an object type corresponding to the annotation interface, and the fourth annotation file comprises a plurality of implementation classes and an implementation number corresponding to each implementation class. The service modifying module comprises: The first determining unit is configured to, for each variable information, determine a target interface corresponding to the variable information in the annotation interface file and a target implementation class corresponding to the variable information in the fourth annotation file according to a variable number of the variable information. The judging unit is configured to determine whether a first generic type of the target interface belongs to an interface type of the first annotation file. The instance unit is configured to, if yes, instantiate an object of the target implementation class to obtain a target service corresponding to the variable information. The determining unit comprises: The judging subunit is configured to determine whether the variable number is equal to a preset default number value. The first determining subunit is configured to, if the variable number is not equal to the default number value, determine a candidate implementation class corresponding to the variable information in the fourth annotation file according to the variable number and the implementation number. The second determining subunit is configured to determine a target interface and a target implementation class corresponding to the variable information according to the annotation interface in the annotation interface file.
6. A computer-readable storage medium, characterized in that, The computer readable storage medium stores one or more programs, which can be executed by one or more processors to implement the steps in the service instance injection method according to any one of claims 1-4.
7. A terminal device, characterized by, Comprise: A processor, a memory and a communication bus; The memory stores a computer readable program that can be executed by the processor; The communication bus realizes the connection communication between the processor and the memory; The processor, when executing the computer readable program, implements the steps in the service instance injection method as claimed in any one of claims 1 to 4.
Citation Information
Patent Citations
Code calling method and device, terminal and storage medium
CN110764748A
Bytecode modification method and device
CN113791767A