Dependency injection method, device, electronic device, and computer-readable storage medium

By using decorators to automatically register and inject dependency classes in the IOC container, it solves the problems of difficult code configuration and limited applicable scenarios during the dependency injection process, achieving a simplified dependency injection process and wide applicability.

CN114443116BActive Publication Date: 2024-10-22LINKAGE TECHNOLOGY (NANJING) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202011232518.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-11-06
Publication Date
2024-10-22
Estimated Expiration
2040-11-06

AI Technical Summary

Technical Problem

In application development, dependency injection based on IOC containers requires a large amount of code configuration, which is difficult and has limited applicable scenarios.

Method used

Set the service type of the class to the injection service through the preset decorator, obtain the data information of the dependent class from the inversion of control container, instantiate and inject the dependent class based on the data information, and use the second decorator to add the service of the dependent class The type is set to registered service, and service registration and injection are automatically performed.

Benefits of technology

It simplifies the dependency injection process, reduces user code configuration requirements, is suitable for front-end pages, expands applicable scenarios, and improves operational convenience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114443116B_ABST
    Figure CN114443116B_ABST
Patent Text Reader

Abstract

The present application provides a dependency injection method, device, electronic device and computer-readable storage medium, which relate to the field of computer technology. The method comprises: when a configuration operation for a class input by a user is obtained, the service type of the class is set to an injection service through a preset first decorator; in response to the injection service, data information of a dependent class corresponding to the class is obtained from a first storage pool of a control inversion container; wherein the data information is stored in the first storage pool in response to the registration service when the service type of the dependent class is detected to be a registration service; the service type of the dependent class is set to a registration service through a second decorator; based on the data information, instantiation data of the dependent class is obtained, and the instantiation data is injected into the class indicated by the injection service.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a dependency injection method, device, electronic device, and computer-readable storage medium. Background Art

[0002] With the continuous development of Internet technology, various types of applications have also received widespread attention. In the process of application development, developers can manage and control the dependencies between classes in the application based on the inversion of control (IOC) container, thereby performing dependency injection.

[0003] Currently, when performing dependency injection based on the IOC container, developers need to perform a lot of code configuration to complete the dependency injection process, which is difficult to implement and has limited applicable scenarios. Summary of the invention

[0004] A first aspect of the present application provides a dependency injection method, comprising:

[0005] When obtaining the configuration operation for the class input by the user, the service type of the class is set to the injection service through the preset first decorator;

[0006] In response to the injection service, obtaining data information of a dependent class corresponding to the class from a first storage pool of the inversion of control container;

[0007] The data information is stored in the first storage pool in response to the registration service when it is detected that the service type of the dependent class is a registration service; the service type of the dependent class is a registration service set by the second decorator;

[0008] Based on the data information, the instantiation data of the dependent class is obtained, and the instantiation data is injected into the class indicated by the injection service.

[0009] Optionally, if the data information is a constructor, then based on the data information, instantiation data of the dependent class is obtained, including:

[0010] Generate instantiation data of dependent classes based on constructors.

[0011] Optionally, if the data information is an instance corresponding to a dependent class, then based on the data information, instantiation data of the dependent class is obtained, including:

[0012] Use the data information as instantiation data of the dependent class.

[0013] Optionally, in response to the injection service, before obtaining data information of a dependent class corresponding to the class from the first storage pool of the inversion of control container, the method includes:

[0014] When it is detected that the service type of the dependent class is a registration service, in response to the registration service, a decorator factory function corresponding to the second decorator and data information of the dependent class are obtained;

[0015] Based on the decorator factory function corresponding to the second decorator, determine whether data information is stored in the first storage pool;

[0016] If the data information is not stored, the data information is stored in the first storage pool of the control inversion container.

[0017] Optionally, in response to the injection service, obtaining data information of a dependent class corresponding to the class from the first storage pool of the inversion of control container includes:

[0018] In response to the injection service, determining whether instantiated data is stored in the second storage pool of the inversion of control container;

[0019] If no instantiation data is stored, a constructor is obtained from the first storage pool.

[0020] Optionally, in response to the injection service, determining whether instantiated data is stored in the second storage pool of the inversion of control container includes:

[0021] In response to the injection service, obtain the decorator factory function and the key value of the class corresponding to the first decorator;

[0022] Based on the key value and the decorator factory function corresponding to the first decorator, it is determined whether instantiated data is stored in the second storage pool of the inversion of control container.

[0023] Optionally, after obtaining instantiation data of the dependent class based on the data information, the method further includes:

[0024] The instantiated data is stored in the second storage pool of the Inversion of Control container.

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

[0026] A first acquisition module is used to set the service type of the class to an injection service through a preset first decorator when acquiring a configuration operation for the class input by a user;

[0027] A second acquisition module is used to obtain data information of a dependent class corresponding to the class from a first storage pool of the inversion of control container in response to the injection service;

[0028] The data information is stored in the first storage pool in response to the registration service when it is detected that the service type of the dependent class is a registration service; the service type of the dependent class is a registration service set by the second decorator;

[0029] The instantiation module is used to obtain the instantiation data of the dependent class based on the data information, and inject the instantiation data into the class indicated by the injection service.

[0030] Optional, if the data information is a constructor; instantiate the module, specifically used for:

[0031] Generate instantiation data of dependent classes based on constructors.

[0032] Optionally, if the data information is an instance corresponding to a dependent class, the module is instantiated, specifically for:

[0033] Use the data information as instantiation data of the dependent class.

[0034] Optionally, the device further includes a third acquisition module, a judgment module and a first storage module;

[0035] Before the second acquisition module obtains data information of a dependent class corresponding to the class from the first storage pool of the inversion of control container in response to the injection service;

[0036] A third acquisition module is used to detect that the service type of the dependent class is a registration service, and in response to the registration service, obtain the decorator factory function corresponding to the second decorator and the data information of the dependent class;

[0037] A judgment module, used for judging whether data information is stored in the first storage pool based on a decorator factory function corresponding to the second decorator;

[0038] The first storage module is used to store the data information in the first storage pool of the control inversion container if the data information is not stored.

[0039] Optionally, the second acquisition module is specifically used to:

[0040] In response to the injection service, determining whether instantiated data is stored in the second storage pool of the inversion of control container;

[0041] If no instantiation data is stored, a constructor is obtained from the first storage pool.

[0042] Optionally, when the second acquisition module determines whether instantiated data is stored in the second storage pool of the inversion of control container in response to the injection service, it is specifically configured to:

[0043] In response to the injection service, obtain the decorator factory function and the key value of the class corresponding to the first decorator;

[0044] Based on the key value and the decorator factory function corresponding to the first decorator, it is determined whether instantiated data is stored in the second storage pool of the inversion of control container.

[0045] Optionally, the device further includes a second storage module, and after the instantiation module obtains the instantiation data of the dependent class based on the data information, the second storage module is used to:

[0046] The instantiated data is stored in the second storage pool of the Inversion of Control container.

[0047] According to a third aspect of an embodiment of the present application, an electronic device is provided, comprising a memory and a processor; a computer program is stored in the memory; and the processor is used to execute the method of the first aspect and any one of its optional implementations when running the computer program.

[0048] According to a fourth aspect of an embodiment of the present application, a computer-readable storage medium is provided, on which a computer program is stored. When the program is executed by a processor, the method of the first aspect and any one of its optional implementations is implemented.

[0049] The beneficial effects of the technical solution provided by this application are:

[0050] The present application can set the service type of the dependent class to a registered service through the second decorator, thereby storing the data information of the dependent class in the first storage pool. In this way, when the configuration operation for the class input by the user is obtained, the service type of the class can be set to an injected service through the preset first decorator; in response to the injected service, the data information of the dependent class corresponding to the class can be obtained from the first storage pool of the inversion of control container, so as to obtain the instantiation data of the dependent class based on the data information, and inject the instantiation data into the class to complete the dependency injection. It can be seen that the dependency injection process of the present application only needs to set the service type through the first decorator and the second decorator to automatically perform service registration and service injection. The dependency injection process can be completed without the user performing a large amount of code configuration, which simplifies the difficulty of dependency injection based on the IOC container. At the same time, the method of the present application is suitable for front-end pages. Compared with the existing solutions that can only perform code configuration in the background, the application scenarios of the present application are more extensive and more convenient for users to operate. BRIEF DESCRIPTION OF THE DRAWINGS

[0051] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in describing the embodiments of the present application are briefly introduced below.

[0052] Figure 1 This is a schematic diagram of an embodiment of the dependency injection method of the present application;

[0053] Figure 2 This is another schematic diagram of an embodiment of the dependency injection method of the present application;

[0054] Figure 3 This is a scenario diagram of the dependency injection method for this application;

[0055] Figure 4 This is a schematic diagram of the structure of the dependency injection device of this application;

[0056] Figure 5 This is a schematic diagram of the structure of the electronic device of this application. DETAILED DESCRIPTION

[0057] The embodiments of the present application are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present application, and cannot be interpreted as limiting the present invention.

[0058] It will be understood by those skilled in the art that, unless expressly stated, the singular forms "one", "an", and "the" used herein may also include plural forms. It should be further understood that the term "comprising" used in the specification of the present application refers to the presence of 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 their groups. It should be understood that when we refer to an element as being "connected" or "coupled" to another element, it may be directly connected or coupled to the other element, or there may also be an intermediate element. In addition, the "connection" or "coupling" used herein may include wireless connection or wireless coupling. The term "and / or" used herein includes all or any unit and all combinations of one or more associated listed items.

[0059] In order to make the objectives, technical solutions and advantages of the present application clearer, the implementation methods of the present application will be further described in detail below in conjunction with the accompanying drawings.

[0060] First, several terms involved in this application are introduced and explained:

[0061] Inversion of control (IOC) is an idea that uses an IOC container to create and control dependencies between classes. Compared with the way a class manages and creates its dependent classes, the IOC container of this application is equivalent to a third party between a class and its dependent classes. Any class that obtains an instance of its dependent class needs to request and obtain it from the IOC container, which can reduce the coupling of computer code.

[0062] A common way of inversion of control is dependency injection (DI), which is to pass objects that meet the dependency relationship to the required objects through the constructor.

[0063] Decorator: A decorator can add, monitor or modify the attribute definition of a class, for example, define the service type of a class through a decorator. A decorator is essentially a python function.

[0064] A class is a collection of objects with the same properties and services, providing a unified abstract description for all objects belonging to the class. Two classes are dependent on each other when one class depends on the definition of another class, that is, when a class is used, the use of the class depends on its dependent class.

[0065] Based on this, please refer to Figure 1 The present application proposes a dependency injection method, which is executed by an electronic device, which may be a terminal device or a server, and the method may include:

[0066] Step S101: when a configuration operation for a class input by a user is obtained, the service type of the class is set to an injection service through a preset first decorator;

[0067] In this embodiment, the configuration operation input by the user for the class can be: the user adds a first decorator to the header of the class, and the first decorator is essentially a python function, which is used to define the service type of the class as an injection service. The electronic device parses the python function corresponding to the first decorator based on the user's configuration operation, and can set the service type of the class to an injection service.

[0068] The injection service refers to injecting the instantiation data of the dependent class of the class into the class. The set injection service carries the identifier of the class, and the identifier is used to determine which class the instantiation data is injected into.

[0069] Step S102, in response to the injection service, obtaining data information of the dependent class corresponding to the class from the first storage pool of the control inversion container; wherein the data information is stored in the first storage pool in response to the registration service when it is detected that the service type of the dependent class is a registration service; the service type of the dependent class is a registration service set by the second decorator;

[0070] In response to the injection service, the electronic device detects whether there is data information of the dependency class in the first storage pool of the control inversion container, and if so, obtains the data information of the dependency class from the first storage pool of the control inversion container.

[0071] In this embodiment, the control inversion container includes multiple storage pools. The first container pool is used to store data information of the dependency class. Of course, in addition to the data information of the dependency class, the first container pool can also store data information of other dependency classes.

[0072] In this embodiment, the data information is used to obtain instantiation data of the dependent class.

[0073] It can be seen that the data information is stored in the first container pool in advance. The storage of data information in the first container pool is realized by the second decorator. Specifically:

[0074] The user can add a second decorator to the header of the dependency class. The second decorator is essentially a Python function, which is used to define the service type of the dependency class as a registered service. When the electronic device detects that the user has added the second decorator to the header of the dependency class, it can parse the Python function corresponding to the second decorator, thereby setting the service type of the dependency class to a registered service.

[0075] The registration service indicates that the data information of the dependency class is registered in the control inversion container. Then, the electronic device responds to the registration service, obtains the data information of the dependency class, and can store the data information of the dependency class in the first storage pool of the control inversion container.

[0076] Step S103: Based on the data information, obtain the instantiation data of the class, and inject the instantiation data into the target class indicated by the injection service.

[0077] Based on the data information, the electronic device can obtain the instantiation data of the dependent class. After determining the class corresponding to the dependent class, the electronic device injects the instantiation data into the class.

[0078] In this embodiment, the electronic device determines the class corresponding to the dependency class by: determining the class corresponding to the dependency class by looking up a mapping relationship table between the class and its dependency class, or determining the class corresponding to the dependency class based on the identifier of the class carried in the injection service.

[0079] It can be seen that the present application can set the service type of the dependent class to a registered service through the second decorator, thereby storing the data information of the dependent class in the first storage pool. In this way, when the configuration operation for the class input by the user is obtained, the service type of the class can be set to an injected service through the preset first decorator; in response to the injected service, the data information of the dependent class corresponding to the class can be obtained from the first storage pool of the inversion of control container, so as to obtain the instantiation data of the dependent class based on the data information, and inject the instantiation data into the class to complete the dependency injection. It can be seen that the dependency injection process of the present application only needs to set the service type through the first decorator and the second decorator to automatically perform service registration and service injection, without the user having to perform a large amount of code configuration, which simplifies the difficulty of dependency injection based on the IOC container. At the same time, the method of the present application is suitable for front-end pages. Compared with the existing solutions that can only perform code configuration in the background, the application scenarios of the present application are more extensive and more convenient for user operation.

[0080] Furthermore, the data information of the dependency class stored in the first storage pool may be an instance of the dependency class or a constructor of the dependency class. In this case, the present application includes the following two possible situations:

[0081] A possible situation A is: if the data information is a constructor; then step S103 obtains instantiation data of the dependent class based on the data information, which may include: generating instantiation data of the dependent class based on the constructor.

[0082] In this embodiment, during the registration service phase, the constructor of the dependent class may be stored in the first container pool, and the data information of the dependent class refers to the constructor of the dependent class. During the injection service phase, after the electronic device obtains the constructor of the dependent class, it may call the constructor of the dependent class to instantiate the dependent class and generate instantiation data.

[0083] Another possible situation B is that if the data information is an instance corresponding to the dependent class, then step S103 obtains instantiation data of the dependent class based on the data information, which may include:

[0084] Use the data information as instantiation data of the dependent class.

[0085] In this embodiment, during the registration service phase, the instantiation number of the dependent class may be stored in the first container pool, and the data information of the dependent class refers to the instantiation data of the dependent class. During the injection service phase, the electronic device may directly obtain the instantiation data of the dependent class without the need to perform the instantiation process.

[0086] In this embodiment, the memory space occupied by the constructor is significantly less than that of the instantiation data. Compared with case B, case A can save the storage space of the inversion of control container. From another perspective, the solution of case B can directly obtain the instantiation data from the inversion of control container, which also speeds up the efficiency of dependency injection.

[0087] Furthermore, the service registration phase and service injection phase are explained below:

[0088] (I) In the embodiment of the present application, the specific process of the service registration phase is:

[0089] Before step S102, in response to the injection service, obtains data information of a dependent class corresponding to the class from the first storage pool of the inversion of control container, the method may further include:

[0090] When it is detected that the service type of the dependent class is a registration service, in response to the registration service, a decorator factory function corresponding to the second decorator and data information of the dependent class are obtained;

[0091] Based on the decorator factory function corresponding to the second decorator, determine whether data information is stored in the first storage pool;

[0092] If the data information is not stored, the data information is stored in the first storage pool of the control inversion container.

[0093] If the above-mentioned electronic device can set the service type of a dependency class to a registered service through the second decorator, then in response to the registration service, the electronic device can obtain the decorator factory function corresponding to the second decorator and the dependency class. After the electronic device obtains the dependency class, it can perform parameter decomposition on the dependency class to obtain data information of the dependency class.

[0094] In this embodiment, the specific process of parameter decomposition is: the electronic device parses the dependent class to obtain a unique identifier of the dependent class, and reads the constructor of the dependent class. The constructor can actually be regarded as an uninitialized instance of the dependent class. The electronic device obtains the data information of the dependent class based on the constructor of the dependent class. Specifically: the constructor of the dependent class can be used as the data information of the dependent class (that is, corresponding to the case where the above data information is the constructor of the dependent class) or after obtaining the instantiated data of the dependent class based on the constructor of the dependent class, the instantiated data is used as the data information of the dependent class (that is, corresponding to the case where the above data information is the instantiated data of the dependent class).

[0095] In this embodiment, the unique identifier of the dependency class may be the name of the dependency class. The dependency class may exist in the form of a key-value pair, and the unique identifier of the dependency class may also be the key value of the dependency class.

[0096] After the electronic device obtains the decorator factory function corresponding to the second decorator, the data information of the dependency class and the unique identifier of the dependency class, the electronic device can call the identification function in the decorator factory function corresponding to the second decorator, and judge whether the data information of the dependency class is stored in the first storage pool based on the unique identifier of the dependency class; if the data information of the dependency class is not stored in the first storage pool, call the storage function in the decorator factory function corresponding to the second decorator to store the data information of the dependency class in the first storage pool; if the data information of the dependency class is stored in the first storage pool, then based on the unique identifier of the dependency class, display to the user a prompt message that the dependency class has been registered with the control inversion container to avoid repeated registration of the dependency class.

[0097] (II) In the case where the data information of the above class is the constructor of the class, the injection service phase is specifically as follows:

[0098] Step S102 obtains data information of dependent classes corresponding to the class from the first storage pool of the control inversion container in response to the injection service, which may include: in response to the injection service, determining whether instantiation data is stored in the second storage pool of the control inversion container; if no instantiation data is stored, obtaining the constructor from the first storage pool.

[0099] Optionally, in response to the injection service, determining whether instantiated data is stored in the second storage pool of the inversion of control container includes:

[0100] In response to the injection service, obtain the decorator factory function and the key value of the class corresponding to the first decorator;

[0101] Based on the key value and the decorator factory function corresponding to the first decorator, it is determined whether instantiated data is stored in the second storage pool of the inversion of control container.

[0102] In response to the injection service for the class, the electronic device determines the dependent class corresponding to the class based on the class-to-class correspondence table, obtains the decorator factory function of the first decorator, and obtains the key-value pair of the dependent class.

[0103] The electronic device performs parameter decomposition on the key-value pair. The parameter decomposition process is as follows: taking the parameter name of the dependent class as the instance name, and determining the key value of the dependent class based on the key-value pair;

[0104] Subsequently, the electronic device searches the mapping relationship table between key and metadata based on the key value of the dependent class, obtains the target metadata corresponding to the key value of the dependent class, and inputs the target metadata into the decorator factory function of the first decorator, so as to judge whether the instantiation data of the dependent class is stored in the second storage pool based on the metadata through the service instance status judgment function in the decorator factory function of the first decorator.

[0105] If there is instantiation data of the dependent class, the instantiation data of the dependent class is directly obtained from the second storage pool, the instance name obtained by parameter decomposition is used as the name of the instantiation data, and the instantiation data is injected into the class;

[0106] If the instantiation data of the dependent class does not exist, it is necessary to perform the operation of obtaining the data information of the dependent class corresponding to the class from the first storage pool of the control inversion container in step S102, specifically:

[0107] Based on the target metadata, query the first storage pool of the control inversion container to determine whether there is a constructor of the dependent class in the first storage pool. If so, obtain the constructor of the dependent class and generate instantiation data of the dependent class and inject it into the class, and use the instance name obtained by parameter decomposition as the name of the instantiation data; if not, issue an exception prompt.

[0108] Optionally, after obtaining instantiation data of the dependent class based on the data information in step S103, the method may further include:

[0109] The instantiated data is stored in the second storage pool of the Inversion of Control container.

[0110] In this embodiment, the electronic device injects the instantiation data of the dependent class into the class and also stores the instantiation data in the second storage pool of the inversion of control container for subsequent use.

[0111] In summary, refer to Figure 2 , the overall process of this application is as follows:

[0112] S1. Set the service type of the dependent class to a registered service through the first decorator;

[0113] S2, in response to the registration service, obtains the constructor of the dependent class, and stores the constructor in the first storage pool of the IOC container;

[0114] S3. Set the service type of the class to injection service through the first decorator;

[0115] S4, in response to the injection service, determining whether instantiation data of the dependent class is stored in the second storage pool of the inversion of control container;

[0116] S5. If instantiated data is stored, the instantiated data is obtained from the second storage pool and injected into the class;

[0117] S6. If no instantiation data is stored, obtaining a constructor of the dependent class from the first storage pool;

[0118] S7. Generate instantiation data of the dependent class based on the constructor, inject the instantiation data into the class, and store the instantiation data in the second storage pool.

[0119] It should be noted that, when performing dependency injection, if the constructor of the dependency class already exists in the first storage pool, steps S1 and S2 do not need to be executed.

[0120] The solution of this embodiment is different from the above Figure 1 The illustrated embodiments are actually the same solutions. The specific implementation of the solutions in this embodiment can refer to the relevant description of the above-mentioned embodiments, and will not be repeated here.

[0121] Please refer to Figure 3 , a possible scenario of this application is:

[0122] The dependency relationship is: Class B depends on Class A, and C depends on Class B, then:

[0123] Registration service stage: You can first set the service type of class A and class B to registration service through the second decorator, then you can register class A to the IOC container and register class B to the IOC container (that is, store the class constructor in the IOC container);

[0124] Injection service stage: Set the service type of class B and class C to injection service through the first decorator, then determine the dependent class of class B, that is, class A, and the dependent class of class C, that is, class B, respectively, obtain the constructor of class A from the IOC container, obtain the instantiation data of class A based on the constructor of class A and inject it into class B, obtain the constructor of class B from the IOC container, obtain the instantiation data of class B based on the constructor of class B and inject it into class C.

[0125] In summary, this application has the following technical effects:

[0126] 1. Based on the IOC container control and management of the dependencies between classes, it solves the problem of tight code coupling and is a mechanism for loosely coupling codes.

[0127] 2. Compared with the existing solution that requires users to perform a lot of code configuration when performing dependency injection based on IOC container, users of this application only need to set the service type of the class through the decorator to automatically perform dependency injection, which simplifies the difficulty of dependency injection based on IOC container;

[0128] 3. Applicable to front-end pages, simplifies user operations, and has a wider range of applications.

[0129] Please refer to Figure 4 , the present application also provides a dependency injection device, including:

[0130] The first acquisition module 401 is used to set the service type of the class to an injection service through a preset first decorator when acquiring a configuration operation for the class input by the user;

[0131] The second acquisition module 402 is used to obtain data information of a dependent class corresponding to the class from the first storage pool of the inversion of control container in response to the injection service;

[0132] The data information is stored in the first storage pool in response to the registration service when it is detected that the service type of the dependent class is a registration service; the service type of the dependent class is a registration service set by the second decorator;

[0133] The instantiation module 403 is used to obtain the instantiation data of the dependent class based on the data information, and inject the instantiation data into the class indicated by the injection service.

[0134] Optionally, if the data information is a constructor, the instantiation module 403 is specifically used to:

[0135] Generate instantiation data of dependent classes based on constructors.

[0136] Optionally, if the data information is an instance corresponding to a dependent class, the instantiation module 403 is specifically used to:

[0137] Use the data information as instantiation data of the dependent class.

[0138] Optionally, the device further includes a third acquisition module, a judgment module and a first storage module;

[0139] Before the second acquisition module 402 acquires data information of a dependent class corresponding to the class from the first storage pool of the inversion of control container in response to the injection service;

[0140] A third acquisition module is used to detect that the service type of the dependent class is a registration service, and in response to the registration service, obtain the decorator factory function corresponding to the second decorator and the data information of the dependent class;

[0141] A judgment module, used for judging whether data information is stored in the first storage pool based on a decorator factory function corresponding to the second decorator;

[0142] The first storage module is used to store the data information in the first storage pool of the control inversion container if the data information is not stored.

[0143] Optionally, the second acquisition module 402 is specifically configured to:

[0144] In response to the injection service, determining whether instantiated data is stored in the second storage pool of the inversion of control container;

[0145] If no instantiation data is stored, a constructor is obtained from the first storage pool.

[0146] Optionally, when the second acquisition module 402 determines whether instantiated data is stored in the second storage pool of the inversion of control container in response to the injection service, it is specifically configured to:

[0147] In response to the injection service, obtain the decorator factory function and the key value of the class corresponding to the first decorator;

[0148] Based on the key value and the decorator factory function corresponding to the first decorator, it is determined whether instantiated data is stored in the second storage pool of the inversion of control container.

[0149] Optionally, the device further includes a second storage module, after the instantiation module 403 obtains the instantiation data of the dependent class based on the data information, the second storage module is used to:

[0150] The instantiated data is stored in the second storage pool of the Inversion of Control container.

[0151] In an alternative embodiment, an electronic device is provided, such as Figure 5 As shown, Figure 5The electronic device 4000 shown includes: a processor 4001 and a memory 4003. The processor 4001 and the memory 4003 are connected, such as through a bus 4002. Optionally, the electronic device 4000 may also include a transceiver 4004. It should be noted that in actual applications, the transceiver 4004 is not limited to one, and the structure of the electronic device 4000 does not constitute a limitation on the embodiments of the present application.

[0152] Processor 4001 may be a CPU (Central Processing Unit), a general-purpose processor, a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array) or other programmable logic devices, transistor logic devices, hardware components or any combination thereof. It may implement or execute various exemplary logic blocks, modules and circuits described in conjunction with the disclosure of this application. Processor 4001 may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, etc.

[0153] The bus 4002 may include a path to transmit information between the above components. The bus 4002 may be a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus. The bus 4002 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 5 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0154] The memory 4003 can be a ROM (Read Only Memory) or other types of static storage devices that can store static information and instructions, a RAM (Random Access Memory) or other types of dynamic storage devices that can store information and instructions, or an EEPROM (Electrically Erasable Programmable Read Only Memory), a CD-ROM (Compact Disc Read Only Memory) or other optical disk storage, optical disk storage (including compressed optical disk, laser disk, optical disk, digital versatile disk, Blu-ray disk, etc.), a magnetic disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited to this.

[0155] The memory 4003 is used to store application code for executing the solution of the present application, and the execution is controlled by the processor 4001. The processor 4001 is used to execute the application code stored in the memory 4003 to implement the content shown in any of the above method embodiments.

[0156] It should be understood that, although the steps in the flowchart of the accompanying drawings are displayed in sequence as indicated by the arrows, these steps are not necessarily executed in sequence in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least a part of the steps in the flowchart of the accompanying drawings may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be executed in turn or alternately with other steps or at least a part of the sub-steps or stages of other steps.

[0157] The above are only some embodiments of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.

Claims

1. A dependency injection method, characterized in that: include: When obtaining the configuration operation for the class input by the user, the service type of the class is set to the injection service through the preset first decorator; In response to the injection service, obtaining data information of a dependent class corresponding to the class from a first storage pool of the inversion of control container; Wherein, the data information is stored in the first storage pool in response to the registration service when it is detected that the service type of the dependency class is a registration service; the service type of the dependency class is a registration service set by the second decorator; Based on the data information, obtaining instantiation data of the dependent class, and injecting the instantiation data into the class indicated by the injection service; If the data information is a constructor, then obtaining instantiation data of the dependent class based on the data information specifically includes: Based on the constructor, generate instantiation data of the dependent class; The step of obtaining, in response to the injection service, data information of a dependent class corresponding to the class from a first storage pool of the inversion of control container specifically includes: In response to the injection service, determining whether the instantiation data is stored in the second storage pool of the inversion of control container; If the instantiation data is not stored, obtaining the constructor from the first storage pool; The step of determining, in response to the injection service, whether the instantiation data is stored in the second storage pool of the inversion of control container specifically includes: In response to the injection service, obtaining a decorator factory function corresponding to the first decorator and a key value of the class; Based on the key value and the decorator factory function corresponding to the first decorator, it is determined whether the instantiation data is stored in the second storage pool of the inversion of control container.

2. The method according to claim 1, characterized in that If the data information is an instance corresponding to a dependent class, then obtaining instantiation data of the dependent class based on the data information includes: The data information is used as instantiation data of the dependent class.

3. The method according to claim 1, characterized in that: Before obtaining data information of a dependent class corresponding to the class from the first storage pool of the inversion of control container in response to the injection service, the method includes: When it is detected that the service type of the dependent class is a registration service, in response to the registration service, a decorator factory function corresponding to the second decorator and data information of the dependent class are obtained; Based on the decorator factory function corresponding to the second decorator, determining whether the data information is stored in the first storage pool; If the data information is not stored, the data information is stored in the first storage pool of the control inversion container.

4. The method according to claim 1, characterized in that: After obtaining the instantiation data of the dependent class based on the data information, the method further includes: The instantiated data is stored in a second storage pool of the inversion of control container.

5. A dependency injection device, characterized in that: include: A first acquisition module is used to set the service type of the class to an injection service through a preset first decorator when acquiring a configuration operation for the class input by a user; A second acquisition module is used to obtain data information of a dependent class corresponding to the class from a first storage pool of the inversion of control container in response to the injection service; Wherein, the data information is stored in the first storage pool in response to the registration service when it is detected that the service type of the dependency class is a registration service; the service type of the dependency class is a registration service set by the second decorator; An instantiation module, used for obtaining instantiation data of the dependent class based on the data information, and injecting the instantiation data into the class indicated by the injection service; If the data information is a constructor, then the instantiation module is specifically used for: Based on the constructor, generate instantiation data of the dependent class; The second acquisition module is specifically used for: In response to the injection service, determining whether the instantiation data is stored in the second storage pool of the inversion of control container; If the instantiation data is not stored, obtaining the constructor from the first storage pool; The second acquisition module is further used for: In response to the injection service, obtaining a decorator factory function corresponding to the first decorator and a key value of the class; Based on the key value and the decorator factory function corresponding to the first decorator, it is determined whether the instantiation data is stored in the second storage pool of the inversion of control container.

6. An electronic device, characterized in that: The electronic device comprises a memory and a processor; The memory stores a computer program; The processor is configured to execute the method according to any one of claims 1 to 4 when running the computer program.

7. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, and when the program is executed by a processor, the method according to any one of claims 1 to 4 is implemented.