Script dependency injection method and device
By automatically assembling the script engine to parse and inject the target service object, the problem of incomplete Groovy script dependency injection is solved, simplifying the development process and improving the service online speed.
Patent Information
- Application Number
- CN202410038098.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-01-10
- Publication Date
- 2025-07-11
AI Technical Summary
The support of the Groovy script dependency injection framework during execution is incomplete, resulting in cumbersome development process, consuming a lot of manpower and time, and affecting the speed of service online.
The initial script source code is parsed through the automatic assembly script engine, obtain the target service object name, and obtain the target service object from the service container, and directly inject it into the script to implement dependency injection.
简化了脚本开发过程,使脚本代码变得简洁,提高了服务上线速度。
Smart Images

Figure CN120295628A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technologies, and particularly to a method and apparatus for script dependency injection. Background Art
[0002] In Internet services in all walks of life, due to the rapid change of user requirements, the service launch speed is crucial. Groovy is an agile development language based on the JVM (Java Virtual Machine), and is an agile dynamic language for the Java virtual machine. For business logics that need to be frequently adjusted, Groovy development can be used to implement the real-time compilation of Groovy to ensure meeting the requirements of the launch speed.
[0003] Currently, when executing a Groovy script, the main method for obtaining a Java service object on which the business logic depends is as follows: the Groovy script engine (GroovyScriptEngine) sends the service container (ApplicationContext) to the Groovy script. After the Groovy script obtains the ApplicationContext, it then obtains the service object parameters from the ApplicationContext to execute the script. Therefore, as a script-type language, Groovy does not have perfect support for dependency injection frameworks.
[0004] For each Groovy script to obtain a service object on which the business execution depends, business personnel need to actively write long codes for the obtaining process in the Groovy script. The development process is cumbersome, the code is not concise, which consumes a large amount of manpower and time, and also makes the service launch speed become long. Therefore, there is an urgent need for a method that can realize the automatic injection of dependent objects in Groovy scripts. Summary of the Invention
[0005] In view of this, embodiments of the present invention provide a method and apparatus for script dependency injection, which can realize the dependency injection of scripts, simplify the script code, and simplify the script development process to meet the requirements of the service launch speed.
[0006] To achieve the above object, according to one aspect of embodiments of the present invention, a method for script dependency injection is provided.
[0007] A method for script dependency injection according to embodiments of the present invention includes:
[0008] Parsing an initial script source code by using an automatic assembly script engine to obtain a name of a target service object on which the script depends when executed;
[0009] Based on the name of the target service object, obtaining the target service object by using the automatic assembly script engine;
[0010] Inject the above target service object to obtain a script;
[0011] In response to the completion of the injection, execute the above script.
[0012] Optionally, the method for constructing the above automatic assembly script engine includes:
[0013] Write a script engine with an automatic assembly command based on the script engine call interface standard to obtain an automatic assembly script engine.
[0014] Optionally, the above method of using the automatic assembly script engine to parse the initial script source code to obtain the names of the target service objects on which the script depends includes:
[0015] Use the automatic assembly script engine to parse the initial script source code;
[0016] Compile the above initial script source code into an initial script;
[0017] Scan the above initial script based on the automatic assembly command to obtain the names of the target service objects on which the script depends.
[0018] Optionally, the above method of using the automatic assembly script engine to obtain the target service object based on the above target service object name includes:
[0019] Based on the above target service object name, use the automatic assembly script engine to obtain the target service object from the service container; wherein, there is a set composed of all service objects in the above service container.
[0020] Optionally, the above injection of the above target service object to obtain a script includes:
[0021] Assign the above target service object to the above initial script property to obtain a script.
[0022] To achieve the above object, according to another aspect of the embodiments of the present invention, there is provided a script dependency injection device.
[0023] A script dependency injection device according to an embodiment of the present invention includes:
[0024] A parsing module, configured to use an automatic assembly script engine to parse the initial script source code to obtain the names of the target service objects on which the script depends;
[0025] An obtaining module, configured to obtain the target service object based on the above target service object name by using the automatic assembly script engine;
[0026] An injection module, configured to inject the above target service object to obtain a script;
[0027] An execution module, configured to execute the above script in response to the completion of injection.
[0028] Optionally, the method for constructing the above-mentioned automatic assembly script engine includes:
[0029] Writing a script engine with an automatic assembly command based on the script engine call interface standard to obtain an automatic assembly script engine.
[0030] Optionally, the above parsing module is further configured to: parse the initial script source code by using the automatic assembly script engine; compile the above initial script source code into an initial script; and scan the above initial script based on the automatic assembly command to obtain the name of the target service object upon which the script depends during execution.
[0031] To achieve the above object, according to another aspect of the embodiments of the present invention, there is provided an electronic device for script dependency injection.
[0032] An electronic device for script dependency injection according to an embodiment of the present invention includes: one or more processors; a storage device configured to store one or more programs, and when the above one or more programs are executed by the above one or more processors, the above one or more processors implement a method for script dependency injection according to an embodiment of the present invention.
[0033] To achieve the above object, according to still another aspect of the embodiments of the present invention, there is provided a computer-readable storage medium.
[0034] A computer-readable storage medium according to an embodiment of the present invention, on which a computer program is stored, and when the above program is executed by a processor, a method for script dependency injection according to an embodiment of the present invention is implemented.
[0035] One of the above embodiments of the invention has the following advantages or beneficial effects: Before executing the script, the target service object name can be obtained by directly parsing the initial script source code through the automatic assembly script engine, and the target service object can be directly obtained from the service container through the automatic assembly script engine and then injected into the script to achieve script dependency injection, making the script code concise and simplifying the script development process to meet the requirements of service online speed.
[0036] The further effects of the above non-conventional optional methods will be described in combination with specific embodiments hereinafter. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] The drawings are used to better understand the present invention and do not unduly limit the present invention. Among them:
[0038] Figure 1It is a schematic flowchart of a script dependency injection method according to an embodiment of the present invention;
[0039] Figure 2 It is a schematic flowchart of obtaining the name of a target service object according to an embodiment of the present invention;
[0040] Figure 3 It is a schematic diagram of the main modules of a script dependency injection device according to an embodiment of the present invention;
[0041] Figure 4 It is an exemplary system architecture diagram to which the embodiments of the present invention can be applied;
[0042] Figure 5 It is a schematic structural diagram of a computer system of a terminal device or a server suitable for implementing the embodiments of the present invention. Detailed implementation manners
[0043] The following describes exemplary embodiments of the present invention with reference to the accompanying drawings. Various details of the embodiments of the present invention are included to facilitate understanding, and they should be considered merely exemplary. Therefore, those of ordinary skill in the art should recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, descriptions of well-known functions and structures are omitted below.
[0044] It should be noted that, without conflict, the embodiments of the present invention and the technical features in the embodiments can be combined with each other.
[0045] It should be noted that in the technical solutions of the present disclosure, in terms of the collection, gathering, updating, analysis, processing, use, transmission, storage, etc. of user personal information, they all comply with the provisions of relevant laws and regulations, are used for legal purposes, and do not violate public order and good customs. Necessary measures are taken for user personal information to prevent illegal access to user personal information data, and to safeguard user personal information security, network security, and national security.
[0046] Figure 1 It is a schematic diagram of the main steps of a script dependency injection method according to an embodiment of the present invention.
[0047] As Figure 1 shown, the script dependency injection method of the embodiments of the present invention mainly includes the following steps:
[0048] Step S101, using an automatic assembly script engine to parse the initial script source code to obtain the name of the target service object on which the script depends when executed;
[0049] Step S102, based on the above-mentioned name of the target service object, using an automatic assembly script engine to obtain the target service object;
[0050] Step S103: Inject the above target service object to obtain a script;
[0051] Step S104: In response to the completion of the injection, execute the above script.
[0052] When the automatic assembly script engine is built, the developer writes an automatic assembly (@Autowired) command into the script engine to form an automatic assembly script engine, enabling it to perform automatic assembly on the script and achieve dependency injection of the script.
[0053] In an optional embodiment, the method for building the above automatic assembly script engine includes: writing a script engine with an automatic assembly command based on the script engine call interface standard to obtain an automatic assembly script engine.
[0054] Furthermore, the function of @Autowired includes injecting attributes. After obtaining the target service object name, @Autowired can assign the target service object to the initial script attributes by name, achieving automatic injection of the script-dependent object.
[0055] The automatic assembly script engine written with @Autowired can scan the initial script to determine whether a service object is declared therein. When a service object is declared in the initial script, obtain the above service object name and use it as the target service object name, so that the automatic assembly script engine can automatically obtain the target service object from the ApplicationContext and assign it to the initial script attributes.
[0056] In an optional embodiment, as Figure 2 shown, the above method for parsing the initial script source code using the automatic assembly script engine to obtain the target service object name upon script execution includes:
[0057] Step S201: Parse the initial script source code using the automatic assembly script engine;
[0058] Step S202: Compile the above initial script source code into an initial script;
[0059] Step S203: Scan the above initial script based on the automatic assembly command to obtain the target service object name upon script execution.
[0060] After calling the automatic assembly script engine to parse the initial script source code and compile it into an executable program (initial script), automatically scan the initial script to obtain the target service object name upon initial script execution, and identify the target service object name based on the automatic assembly command.
[0061] In an alternative embodiment, obtaining the target service object by using the automatic assembly script engine based on the above-mentioned target service object name includes: obtaining the target service object from the service container by using the automatic assembly script engine based on the above-mentioned target service object name; wherein, there is a set composed of all service objects in the above-mentioned service container.
[0062] Use the automatic assembly script engine to obtain the target service object corresponding to the target service object name of the automatic assembly command identifier from the ApplicationContext, so as to inject the target service object into the initial script.
[0063] In an alternative embodiment, injecting the above-mentioned target service object to obtain a script includes: assigning the above-mentioned target service object to the above-mentioned initial script property to obtain a script.
[0064] When the script is executed, the service object properties in the script have been assigned values, so the script can be executed normally.
[0065] The following uses a specific embodiment to detail the script dependency injection process:
[0066] For a script compiled using the Groovy language, Java code can execute the script through the Groovy script execution engine (GroovyScriptEngine). The Java code uses the Spring framework, and the business logic execution of the Groovy script depends on the existing Java service objects in Spring. In the Spring framework, all Java service objects are maintained in the ApplicationContext.
[0067] When constructing the Groovy script engine, write the @Autowired command to obtain the Groovy automatic assembly script engine (AWGroovyScriptEngine).
[0068] The business system calls the AWGroovyScriptEngine to parse the Groovy initial script source code, compiles the Groovy initial script source code into a Groovy initial script, and scans the Groovy initial script to obtain the target service object name on which the Groovy initial script depends when executed. At the same time, the target service object name is identified based on @Autowired.
[0069] The business system calls the AWGroovyScriptEngine to obtain the target service object corresponding to the target service object name marked with @Autowired from the ApplicationContext and assigns it to the Groovy initial script property, resulting in a Groovy script that can be executed normally.
[0070] According to the technical solution of the embodiment of the present invention, before executing the script, the initial script source code can be directly parsed through the autowired script engine to obtain the target service object name, and the target service object can be directly obtained from the service container through the autowired script engine and then injected into the script to achieve dependency injection of the script, making the script code concise, simplifying the script development process, and meeting the requirements of service online speed. Through the extension of the script engine, the constructed autowired script engine supports annotation parsing, making it more convenient when the initial script calls the service formation, and avoiding the way of only obtaining the service object through parameter passing.
[0071] Figure 3 It is a schematic diagram of the main modules of the script dependency injection device according to the embodiment of the present invention.
[0072] As Figure 3 shown, the script dependency injection device 300 according to the embodiment of the present invention includes:
[0073] A parsing module 301, configured to parse the initial script source code by using the autowired script engine to obtain the target service object name on which the script depends during execution;
[0074] An obtaining module 302, configured to obtain the target service object by using the autowired script engine based on the above target service object name;
[0075] An injection module 303, configured to inject the above target service object to obtain a script;
[0076] An execution module 304, configured to execute the above script in response to the completion of injection.
[0077] In an optional embodiment of the present invention, the construction method of the above autowired script engine includes: writing a script engine with an autowired command based on the script engine call interface standard to obtain an autowired script engine.
[0078] In an optional embodiment of the present invention, the above parsing module 301 is further configured to: parse the initial script source code by using the autowired script engine; compile the above initial script source code into an initial script; and scan the above initial script based on the autowired command to obtain the target service object name on which the script depends during execution.
[0079] In an alternative embodiment of the present invention, the obtaining module 302 is further configured to: based on the target service object name, obtain the target service object from the service container by using an automatic assembly script engine; wherein, a set composed of all service objects exists in the service container.
[0080] In an alternative embodiment of the present invention, the injection module 303 is further configured to: assign the target service object to the initial script property to obtain a script.
[0081] According to the technical solution of the embodiment of the present invention, before executing the script, the initial script source code can be directly parsed by the automatic assembly script engine to obtain the target service object name, and the target service object can be directly obtained from the service container by the automatic assembly script engine, and then injected into the script to implement dependency injection of the script, making the script code concise, simplifying the script development process, and meeting the requirements of service online speed.
[0082] Figure 4 An exemplary system architecture 400 to which the script dependency injection method or the script dependency injection device of the embodiment of the present invention can be applied is shown.
[0083] As Figure 4 shown, the system architecture 400 may include terminal devices 401, 402, 403, a network 404, and a server 405. The network 404 is used to provide a medium for a communication link between the terminal devices 401, 402, 403 and the server 405. The network 404 may include various connection types, such as wired, wireless communication links, or fiber optic cables, etc.
[0084] Users can use the terminal devices 401, 402, 403 to interact with the server 405 through the network 404 to receive or send data, etc. Various communication client applications may be installed on the terminal devices 401, 402, 403, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc.
[0085] The terminal devices 401, 402, 403 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop portable computers, and desktop computers, etc.
[0086] The server 405 may be a server providing various services, such as a background management server that provides support for the initial script source code sent by the user using the terminal devices 401, 402, 403. The background management server may perform processing such as parsing on the initial script source code and feedback the processing result (such as the target service object name) to the terminal device.
[0087] It should be noted that the script dependency injection method provided by the embodiments of the present invention is generally executed by the server 405. Correspondingly, the script dependency injection device is generally set in the server 405.
[0088] It should be understood that Figure 4 the number of terminal devices, networks, and servers in
[0089] is merely illustrative. According to actual needs, there can be any number of terminal devices, networks, and servers. Figure 5 is merely illustrative. According to actual needs, there can be any number of terminal devices, networks, and servers. Figure 5 The terminal device shown is merely an example and should not impose any limitations on the functions and usage scope of the embodiments of the present invention.
[0090] As Figure 5 shown, the computer system 500 includes a central processing unit (CPU) 501, which can execute various appropriate actions and processes according to the program stored in the read-only memory (ROM) 502 or the program loaded from the storage section 508 into the random access memory (RAM) 503. In the RAM 503, various programs and data required for the operation of the system 500 are also stored. The CPU 501, ROM 502, and RAM 503 are connected to each other via a bus 504. The input / output (I / O) first interface 505 is also connected to the bus 504.
[0091] The following components are connected to the I / O first interface 505: an input section 506 including a keyboard, a mouse, etc.; an output section 507 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc. and a speaker; a storage section 508 including a hard disk, etc.; and a communication section 509 including a network first interface card such as a LAN card, a modem, etc. The communication section 509 performs communication processing via a network such as the Internet. A drive 510 is also connected to the I / O first interface 505 as needed. A removable medium 511, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 510 as needed so that the computer program read from it can be installed into the storage section 508 as needed.
[0092] In particular, according to the embodiments disclosed in the present invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, the embodiments disclosed in the present invention include a computer program product that includes a computer program carried on a computer-readable medium, and the computer program contains program codes for executing the methods shown in the flowcharts. In such an embodiment, the computer program can be downloaded and installed from a network through the communication section 509, and / or installed from the removable medium 511. When the computer program is executed by the central processing unit (CPU) 501, the above-described functions defined in the system of the present invention are executed.
[0093] It should be noted that the computer-readable medium shown in the present invention can be a computer-readable signal medium or a computer-readable storage medium or any combination of the two. The computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the above. More specific examples of the computer-readable storage medium can include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, the computer-readable storage medium can be any tangible medium that contains or stores a program, and the program can be used by or in combination with an instruction execution system, apparatus, or device. In the present invention, the computer-readable signal medium can include a data signal propagated in a baseband or as part of a carrier wave, in which the computer-readable program code is carried. Such a propagated data signal can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. The computer-readable signal medium can also be any computer-readable medium other than the computer-readable storage medium, and the computer-readable medium can send, propagate, or transmit a program for use by or in combination with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted by any suitable medium, including but not limited to: wireless, wire, optical fiber, RF, etc., or any suitable combination of the above.
[0094] The flowcharts and block diagrams in the accompanying drawings illustrate the possible architectures, functions, and operations of systems, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagram may represent a module, a segment of a program, or a portion of code that contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions noted in the blocks may occur in a different order than that noted in the accompanying drawings. For example, two consecutive blocks shown may actually be executed substantially in parallel, or they may sometimes be executed in the reverse order, depending on the functions involved. It should also be noted that each block in the block diagram or flowchart, as well as combinations of blocks in the block diagram or flowchart, can be implemented by a dedicated hardware-based system that performs the specified functions or operations, or can be implemented by a combination of dedicated hardware and computer instructions.
[0095] The modules described in the embodiments of the present invention can be implemented in software or in hardware. The described modules can also be provided in a processor. For example, it can be described as: a processor includes a parsing module, an acquisition module, an injection module, and an execution module. Among them, the names of these modules do not constitute a limitation on the module itself in some cases. For example, the parsing module can also be described as "a module that parses the initial script source code using an automatic assembly script engine to obtain the names of the target service objects on which the script depends when executed".
[0096] As another aspect, the present invention also provides a computer-readable medium, which can be included in the device described in the above embodiments; or it can exist separately and not be assembled into the device. The above computer-readable medium carries one or more programs. When the one or more programs are executed by a device, the device includes: parsing the initial script source code using an automatic assembly script engine to obtain the names of the target service objects on which the script depends when executed; obtaining the target service objects using the automatic assembly script engine based on the names of the target service objects; injecting the target service objects to obtain a script; and executing the script in response to the completion of the injection.
[0097] According to the technical solution of the embodiments of the present invention, before executing a script, the initial script source code can be directly parsed by an automatic assembly script engine to obtain the names of the target service objects, and the target service objects can be directly obtained from the service container by the automatic assembly script engine, and then injected into the script to achieve dependency injection of the script, making the script code concise and simplifying the script development process to meet the requirements for the service go-live speed.
[0098] The above specific embodiments do not constitute a limitation to the protection scope of the present invention. Those skilled in the art should understand that various modifications, combinations, sub - combinations and substitutions can occur depending on design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principle of the present invention shall be included within the protection scope of the present invention.
Claims
1. A script dependency injection method, characterized in that, including: Parsing the initial script source code by using an automatic assembly script engine to obtain the names of target service objects upon which the script depends when executed; Obtaining the target service objects by using the automatic assembly script engine based on the names of the target service objects; Injecting the target service objects to obtain a script; Executing the script in response to the completion of injection.
2. The script dependency injection method according to claim 1, wherein The method for constructing the automatic assembly script engine includes: Writing a script engine with automatic assembly commands based on the script engine call interface standard to obtain the automatic assembly script engine.
3. The script dependency injection method according to claim 2, characterized in that The parsing of the initial script source code by using the automatic assembly script engine to obtain the names of target service objects upon which the script depends when executed includes: Parsing the initial script source code by using the automatic assembly script engine; Compiling the initial script source code into an initial script; Scanning the initial script based on the automatic assembly commands to obtain the names of target service objects upon which the script depends when executed.
4. The script dependency injection method according to claim 1, characterized in that, The obtaining of the target service objects by using the automatic assembly script engine based on the names of the target service objects includes: Obtaining the target service objects from a service container by using the automatic assembly script engine based on the names of the target service objects; wherein, there is a set composed of all service objects in the service container.
5. The script dependency injection method according to claim 3, wherein The injecting of the target service objects to obtain a script includes: Assigning the target service objects to the properties of the initial script to obtain a script.
6. A script dependency injection device, characterized in that, including: A parsing module for parsing the initial script source code by using the automatic assembly script engine to obtain the names of target service objects upon which the script depends when executed; An obtaining module for obtaining the target service objects by using the automatic assembly script engine based on the names of the target service objects; An injecting module for injecting the target service objects to obtain a script; An executing module for executing the script in response to the completion of injection.
7. The script dependency injection device according to claim 6, wherein The method for constructing the automatic assembly script engine includes: Writing a script engine with automatic assembly commands based on the script engine call interface standard to obtain the automatic assembly script engine.
8. The script dependency injection device according to claim 7, wherein The parsing module is further configured to: parse the initial script source code by using the automatic assembly script engine; compile the initial script source code into an initial script; Scan the initial script based on the automatic assembly commands to obtain the names of target service objects upon which the script depends when executed.
9. An electronic device for script dependency injection, characterized in that, including: One or more processors; A storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, enabling the one or more processors to implement the method according to any one of claims 1-5.
10. A computer-readable medium having a computer program stored thereon, characterized in that, The program, when executed by the processor, implements the method according to any one of claims 1-5.