Non-invasive interface expansion method and device, electronic equipment and storage medium
By obtaining and parsing timing functions, determining context objects, and defining target interfaces, we realize the non-invasive expansion of functions in Java virtual machines, solving the security risks caused by the expansion of functions in the existing technology that require modifying code, and meeting higher business needs.
Patent Information
- Application Number
- CN202411363512.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-27
- Publication Date
- 2024-12-13
Smart Images

Figure CN119127352A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software development, and in particular to a non-intrusive interface expansion method and device, electronic equipment and storage medium. Background Art
[0002] As a programming language, Java is widely used due to its multiple advantages such as cross-platform, object-oriented, indirect and multi-threaded. In the Java programming process, the Java virtual machine is the basis for the operation of Java programs. It provides a stable, secure and efficient operating environment for Java programs, enabling the Java language to achieve the feature of "write once, run repeatedly" on different platforms.
[0003] Currently, in order to better manage Java programs, the Spring framework has been introduced, which can manage Java applications throughout their life cycle. Spring applications are Java applications written based on the Spring framework. Functions in the completed Spring applications can often only be called within the Java virtual machine.
[0004] Based on the growth of business needs, it is necessary to expand the functions in the Spring application. In the related technology, it is necessary to expand the functions in the Spring application in an intrusive way by modifying the code. This method may cause security risks to the virtual machine.
[0005] To address the above-mentioned problems, no effective solution has been proposed yet. Summary of the invention
[0006] The embodiments of the present invention provide a non-intrusive interface expansion method and device, an electronic device and a storage medium, so as to at least solve the technical problem in the related art that when the internal functions of a virtual machine are expanded outward, it is necessary to modify the code, which may cause security risks to the virtual machine.
[0007] According to one aspect of an embodiment of the present invention, a non-intrusive interface expansion method is provided, which is applied to a non-intrusive interface expansion system, and the non-intrusive interface expansion method includes: obtaining a timing function from a Spring application corresponding to a target virtual machine, wherein the timing function includes at least one of the following: a function object whose calling frequency in a target period is higher than a preset calling frequency threshold, and a function object for which a calling interface has been created; obtaining a callback function when the timing function is called, and parsing the callback function to obtain parameter information of the callback function; determining a context object of the Spring application based on the parameter information of the callback function, and obtaining a Bean object of the Spring application based on the context object, wherein the Bean object is an entry for accessing a container object in a Spring framework; obtaining a target function through the Bean object, and defining a target interface for the target function, wherein after defining the target interface, an external application accesses the target function through the target interface, and the external application represents a software program or service running outside the target virtual machine.
[0008] Optionally, the step of obtaining the timing function from the Spring application includes: monitoring the Spring application in the target virtual machine, obtaining the function object called in the Spring application and the calling frequency of the function object in the target cycle; when the calling frequency of the function object in the target cycle is greater than the preset calling frequency threshold, selecting the function object as the timing function.
[0009] Optionally, the step of obtaining the timing function from the Spring application includes: obtaining the created calling interface in the Spring application; calling the function object in the Spring program through the created calling interface, and using the successfully called function object as the timing function.
[0010] Optionally, the step of determining the context object of the Spring application based on the parameter information of the callback function includes: obtaining the class loader of the Spring application through the parameter information; using the class loader to obtain the class object that implements the context interface through reflection; and determining the context object of the Spring application from the class object through reflection.
[0011] Optionally, after defining a target interface for the target function, the method further includes: testing the target interface to obtain a test result; when the test result indicates that the target interface passes the test, storing the target function and the target interface corresponding to the target function into a target function storage container.
[0012] Optionally, the step of testing the target interface includes: in a test environment, calling the target function based on the target interface, and obtaining data transmitted by the target function during the calling process; obtaining transmission parameters pre-defined by the target function, and comparing the data transmitted by the target function during the calling process with the pre-defined transmission parameters to obtain a comparison result; when the comparison result indicates that the data transmitted by the target function during the calling process is consistent with the pre-defined transmission parameters, determining that the target interface has passed the test.
[0013] Optionally, the type of parameter information of the callback function includes at least one of the following: event parameter information of a processing event involved when the callback function is executed, result parameter information of a processing event involved when the callback function is executed, and state parameter information of a processing event involved when the callback function is executed.
[0014] According to another aspect of an embodiment of the present invention, a non-intrusive interface expansion device is also provided, which is applied to a non-intrusive interface expansion system, and the non-intrusive interface expansion device includes: an acquisition unit, which is used to acquire a timing function from a Spring application corresponding to a target virtual machine, wherein the timing function includes at least one of the following: a function object whose calling frequency in a target period is higher than a preset calling frequency threshold, and a function object for which a calling interface has been created; a parsing unit, which is used to acquire a callback function when the timing function is called, and parse the callback function to obtain parameter information of the callback function; a determination unit, which is used to determine a context object of the Spring application based on the parameter information of the callback function, and acquire a Bean object of the Spring application based on the context object, wherein the Bean object is an entry for accessing a container object in a Spring framework; a definition unit, which is used to acquire a target function through the Bean object, and define a target interface for the target function, wherein after defining the target interface, an external application accesses the target function through the target interface, and the external application represents a software program or service running outside the target virtual machine.
[0015] Optionally, the acquisition unit includes: a first monitoring module, used to monitor the Spring application in the target virtual machine, obtain the function objects called in the Spring application and the calling frequency of the function objects within the target period; a first selection module, used to select the function object as the timing function when the calling frequency of the function object within the target period is greater than the preset calling frequency threshold.
[0016] Optionally, the acquisition unit includes: a first acquisition module, used to acquire the created calling interface in the Spring application; a first acting module, used to call the function object in the Spring program through the created calling interface, and use the successfully called function object as the timing function.
[0017] Optionally, the determination unit includes: a second acquisition module, used to obtain the class loader of the Spring application through the parameter information; a third acquisition module, used to use the class loader to obtain the class object that implements the context interface through reflection; and a first determination module, used to determine the context object of the Spring application from the class object through reflection.
[0018] Optionally, the non-invasive interface expansion device also includes: a first testing module, used to test the target interface and obtain a test result; a first storage module, used to store the target function and the target interface corresponding to the target function in a target function storage container when the test result indicates that the target interface passes the test.
[0019] Optionally, the first test module includes: a first calling sub-module, used to call the target function based on the target interface in a test environment, and obtain the data transmitted by the target function during the calling process; a first comparison sub-module, used to obtain the transmission parameters pre-defined by the target function, and compare the data transmitted by the target function during the calling process with the pre-defined transmission parameters to obtain a comparison result; and a first determination sub-module, used to determine that the target interface has passed the test when the comparison result indicates that the data transmitted by the target function during the calling process is consistent with the pre-defined transmission parameters.
[0020] Optionally, the type of parameter information of the callback function includes at least one of the following: event parameter information of a processing event involved when the callback function is executed, result parameter information of a processing event involved when the callback function is executed, and state parameter information of a processing event involved when the callback function is executed.
[0021] According to another aspect of an embodiment of the present invention, a computer-readable storage medium is further provided, wherein the computer-readable storage medium includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute any one of the above-mentioned non-intrusive interface expansion methods.
[0022] According to another aspect of an embodiment of the present invention, there is also provided an electronic device, comprising one or more processors and a memory, wherein the memory is used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement any one of the above-mentioned non-intrusive interface expansion methods.
[0023] In the present application, the following steps are performed: first, a timing function is obtained from the Spring application corresponding to the target virtual machine, wherein the timing function includes at least one of the following: a function object whose calling frequency within the target period is higher than a preset calling frequency threshold, and a function object whose calling interface has been created; then, a callback function is obtained when the timing function is called, and the callback function is parsed to obtain parameter information of the callback function; and the context object of the Spring application is determined based on the parameter information of the callback function, and the Bean object of the Spring application is obtained based on the context object, wherein the Bean object is an entry for accessing a container object in a Spring framework; finally, the target function is obtained through the Bean object, and a target interface is defined for the target function, wherein after the target interface is defined, an external application accesses the target function through the target interface, and the external application represents a software program or service running outside the target virtual machine.
[0024] In this application, an appropriate timing function is selected, and the context object of the Spring application is obtained through the callback function when the timing function is called, and the entrance to the container object in the Spring framework is obtained according to the context object, thereby entering the Java virtual machine, selecting the target function from the Java virtual machine and defining the target interface, so that applications outside the Java virtual machine can call the target function through the target interface, thereby realizing the expansion of function objects in the Java virtual machine in a non-invasive manner without modifying the code, meeting more business needs, and thus solving the technical problem in the related technology that when expanding the internal functions of the virtual machine outward, it is necessary to modify the code to achieve it, which will cause security risks to the virtual machine. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0026] Figure 1 is a flow chart of an optional non-intrusive interface expansion method according to an embodiment of the present invention;
[0027] Figure 2 is an optional non-intrusive interface expansion system architecture diagram according to an embodiment of the present invention;
[0028] Figure 3 is a schematic diagram of an optional non-intrusive interface expansion process according to an embodiment of the present invention;
[0029] Figure 4 is an optional objective function expansion principle diagram according to an embodiment of the present invention;
[0030] Figure 5 is a schematic diagram of an optional non-intrusive interface expansion device according to an embodiment of the present invention;
[0031] Figure 6 The present invention is a hardware structure block diagram of an electronic device (or mobile device) according to a non-intrusive interface expansion method of an embodiment of the present invention. DETAILED DESCRIPTION
[0032] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0033] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0034] To facilitate those skilled in the art to understand the present invention, some terms or nouns involved in the embodiments of the present invention are explained below:
[0035] Java Virtual Machine, Java Virtual Machine, abbreviated as JVM, is a virtual computer that can execute Java bytecode.
[0036] JVMTI, the full name of JVM Tool I nterface, Java virtual machine tool interface.
[0037] Bytecode enhancement technology is a technology based on JVMTI that obtains information and status of Java programs and changes the running process of Java programs. It is usually used for log tracking, dynamic proxy, etc.
[0038] Spr ing framework, an enterprise-level service framework for life cycle management of Java objects.
[0039] Spring application, a Java application written based on the Spring framework.
[0040] It should be noted that the non-intrusive interface expansion method and device in the present application can be used in the field of software development technology when the target function in the virtual machine is expanded, and can also be used in any field outside the field of software development technology when the target function in the virtual machine is expanded. The application of the non-intrusive interface expansion method and device in the present application does not limit the application field.
[0041] It should be noted that the relevant information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are information and data authorized by the user or fully authorized by all parties, and the collection, storage, use, processing, transmission, provision, disclosure and application of the relevant data comply with the relevant laws, regulations and standards of the relevant regions, take necessary confidentiality measures, do not violate public order and good customs, and provide corresponding operation entrances for users to choose to authorize or refuse. For example, an interface is set up between this system and relevant users or organizations. Before obtaining relevant information, it is necessary to send an acquisition request to the aforementioned user or organization through the interface, and obtain relevant information after receiving the consent information fed back by the aforementioned user or organization.
[0042] It should be noted that in this application, when collecting and analyzing customer information, corresponding operation entrances are provided for users to choose to agree or reject the automated decision-making results; if the user chooses to reject, the expert decision-making process will be entered.
[0043] The following embodiments of the present invention can be applied to various non-intrusive interface expansion systems / applications / devices. The present invention utilizes the event-driven characteristics of bytecode enhancement technology, determines the appropriate timing function, and cuts into the Java virtual machine through the timing function, obtains the target function object and defines the target interface for it, and expands the target function in the Java virtual machine non-intrusively without modifying the code to meet higher business needs.
[0044] The present invention is described in detail below in conjunction with various embodiments.
[0045] Embodiment 1
[0046] According to an embodiment of the present invention, an embodiment of a non-intrusive interface expansion method is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0047] Figure 1 is a flowchart of an optional non-intrusive interface expansion method according to an embodiment of the present invention, which is applied to a non-intrusive interface expansion system, such as Figure 1 As shown, the non-intrusive interface expansion method includes the following steps:
[0048] Step S101, obtaining a timing function from a Spring application corresponding to a target virtual machine, wherein the timing function includes at least one of the following: a function object whose calling frequency in a target period is higher than a preset calling frequency threshold, and a function object whose calling interface has been created;
[0049] Step S102, obtaining a callback function when the timing function is called, and parsing the callback function to obtain parameter information of the callback function;
[0050] Step S103, determining the context object of the Spring application based on the parameter information of the callback function, and acquiring the Bean object of the Spring application based on the context object, wherein the Bean object is an entry for accessing the container object in the Spring framework;
[0051] Step S104, obtaining the target function through the Bean object, and defining a target interface for the target function, wherein after the target interface is defined, an external application accesses the target function through the target interface, and the external application represents a software program or service running outside the target virtual machine.
[0052] Through the above steps, firstly, a timing function is obtained from the Spring application corresponding to the target virtual machine, wherein the timing function includes at least one of the following: a function object whose calling frequency in the target period is higher than a preset calling frequency threshold, and a function object whose calling interface has been created; then, a callback function when the timing function is called is obtained, and the callback function is parsed to obtain parameter information of the callback function; and based on the parameter information of the callback function, a context object of the Spring application is determined, and based on the context object, a Bean object of the Spring application is obtained, wherein the Bean object is an entry for accessing a container object in a Spring framework; finally, a target function is obtained through the Bean object, and a target interface is defined for the target function; wherein, after defining the target interface, an external application accesses the target function through the target interface, and the external application represents a software program or service running outside the target virtual machine.
[0053] In this embodiment, an appropriate timing function is selected, and the context object of the Spring application is obtained through the callback function when the timing function is called, and the entrance to the container object in the Spring framework is obtained according to the context object, thereby entering the Java virtual machine, selecting the target function from the Java virtual machine and defining the target interface, so that the application outside the Java virtual machine can call the target function through the target interface, thereby realizing the expansion of function objects in the Java virtual machine in a non-invasive manner without modifying the code, meeting more business needs, and thus solving the technical problem in the related technology that when the internal functions of the virtual machine are expanded outward, it is necessary to modify the code to achieve it, which will cause security risks to the virtual machine.
[0054] The embodiment of the present invention is described in detail below in combination with the above steps.
[0055] The interface expansion method provided by the embodiment of the present invention is non-intrusive. Based on bytecode enhancement technology, the internal functions of the Java virtual machine are expanded into external interfaces, so that it can be applied to more business scenarios and meet higher business needs. While expanding the Spring application interface, there is no need to modify the code of the original Spring application. It can avoid restrictions in many usage scenarios, such as: interface expansion of applications that cannot be stopped. At the same time, it can avoid risks in code management, for example: while avoiding the risks brought by the modification of the application code in the test phase, the application interface is expanded to improve the test efficiency or realize some test scenarios that are not easy to realize.
[0056] It should be noted that bytecode enhancement technology can only achieve functional enhancement of the original program by passively receiving information from the Java application program in an event-driven manner and changing the program's running process, but cannot provide active triggering functional expansion. The embodiment of the present invention utilizes the event-driven characteristics of bytecode enhancement technology, cuts into the JVM through an appropriate timing function, obtains the target function object from the Java virtual machine and stores it; when an application outside the Java virtual machine actively triggers, the target function object obtained in advance is used to call the target interface corresponding to the target function through reflection and other methods, thereby achieving the expansion of the target function and changing the control of the target function from active to passive.
[0057] Step S101, obtaining a timing function from a Spring application corresponding to a target virtual machine.
[0058] It should be noted that when expanding the interface, first find a timing function that can be determined to run in the Spring application. Obtaining the appropriate timing function will help to enter the Java virtual machine faster and more accurately, thereby facilitating the non-intrusive interface expansion system to access the function objects in the Java virtual machine and expand them.
[0059] It should be noted that the function objects of the Spring application are uniformly managed by the Spring framework in the Spring context object. Therefore, to obtain the target function object, the Spring context must be obtained. To obtain the Spring context, the ClassLoader (class loader) in which it is located must be obtained. Therefore, selecting the appropriate timing function becomes the key to obtaining the target function object. In the embodiment of the present invention, a function that is automatically triggered in a certain period in the Spring application, or a function that can be called and triggered by an external interface is used as the timing function, that is, the timing function can be a function object whose calling frequency in the target period is higher than a preset calling frequency threshold and a function object that has created a calling interface.
[0060] Optionally, the step of obtaining the timing function from the Spring application includes: monitoring the Spring application in the target virtual machine, obtaining the function object called in the Spring application and the calling frequency of the function object in the target cycle; when the calling frequency of the function object in the target cycle is greater than a preset calling frequency threshold, selecting the function object as the timing function.
[0061] It should be noted that when selecting the timing function, the Spring application in the target Java virtual machine can be monitored to determine the function objects called in the Spring application and the calling frequency of the function objects within a preset target period, and the function objects that are cyclically called at a certain period are selected as the timing function based on the calling frequency.
[0062] Optionally, the step of obtaining the timing function from the Spring application includes: obtaining a created calling interface in the Spring application; calling a function object in the Spring program through the created calling interface, and using the successfully called function object as the timing function.
[0063] It should be noted that when selecting an appropriate timing function, a function object whose interface has been created and can be successfully called in the Spring program can also be obtained as the timing function.
[0064] Step S102, obtaining a callback function when the opportunity function is called, and parsing the callback function to obtain parameter information of the callback function.
[0065] It should be noted that the timing function usually refers to a function that is automatically triggered at a specific time point or when a specific event occurs. The callback function refers to a function that is passed as a parameter to another function and is called when a specific event occurs. When the timing function is triggered, it is often accompanied by the execution of the callback function. The callback function can be pre-defined and directly obtained when the timing function is triggered, or it can be registered when the timing function is triggered. The timing function will traverse the registered callback list and call the callback function in sequence. This is usually completed at a certain stage of the timing function. The timing function will pass parameters to the callback function so that the callback function can perform specific operations based on these parameters. By parsing the callback function, you can obtain the parameters required for the events involved in the execution of the timing function and the return value after the event is executed.
[0066] Optionally, the type of parameter information of the callback function includes at least one of the following: event parameter information of a processing event involved in executing the callback function, result parameter information of a processing event involved in executing the callback function, and state parameter information of a processing event involved in executing the callback function.
[0067] It should be noted that the embodiment of the present invention parses the callback function, and the parameter information obtained may be: event parameter information of the processing event involved when the callback function is executed, result parameter information of the processing event involved when the callback function is executed, and state parameter information of the processing event involved when the callback function is executed.
[0068] Step S103: determining the context object of the Spring application based on the parameter information of the callback function, and acquiring the Bean object of the Spring application based on the context object.
[0069] It should be noted that the embodiment of the present invention uses the agent or attach mode of bytecode enhancement technology to mount on the running Spring application. After intercepting the call event of the opportunity function, the class loader of the Spring application is obtained by reflection, and then the context object is obtained. The Bean object of the Spring application is obtained through the context object, and then all function objects on the Spring application are accessed through the Bean object. The Bean object is the entry for accessing the container object in the Spring framework.
[0070] Optionally, the step of determining the context object of the Spring application based on parameter information of the callback function includes: obtaining the class loader of the Spring application through the parameter information; using the class loader to obtain the class object that implements the context interface through reflection; and determining the context object of the Spring application from the class object through reflection.
[0071] It should be noted that the ClassLoader, i.e., the class loader, of the Spring application is obtained through the parameters of the callback function. ClassLoader is a component of the Java virtual machine and is responsible for dynamically loading Java classes and resources. When a Java program is running, when the Java program needs to use a class (for example, instantiating an object through the new keyword or referencing a class through reflection), ClassLoader is responsible for loading the .class file of the class into memory and creating a corresponding Class object. After determining the class loader of the Spring application, the class object that implements the context interface can be obtained through reflection based on the class loader, and then the context object of the Spring application can be obtained from the class object through reflection.
[0072] Step S104, obtaining the target function through the Bean object, and defining a target interface for the target function.
[0073] It should be noted that the Bean object is the entry point to access the container object in the Spring framework. Multiple function objects are stored in the Bean object. Through the Bean object, the target function can be obtained from the Spring program, and the interface of the target function can be defined to achieve the expansion of the target function.
[0074] It should be noted that after defining the target interface, the external application accesses the target function through the target interface. The external application represents a software program or service running outside the target virtual machine, and then calls the target interface corresponding to the target function to execute related business.
[0075] Optionally, after defining a target interface for the target function, the method further includes: testing the target interface to obtain a test result; and when the test result indicates that the target interface passes the test, storing the target function and the target interface corresponding to the target function into a target function storage container.
[0076] It should be noted that after defining the target interface for the target function, the target interface needs to be tested. When the test result indicates that the target interface passes the test, the target function and its corresponding target interface are stored in the target function storage container for external applications to call.
[0077] Optionally, the step of testing the target interface includes: in a test environment, calling a target function based on the target interface, and obtaining data transmitted by the target function during the calling process; obtaining transmission parameters pre-defined for the target function, and comparing the data transmitted by the target function during the calling process with the pre-defined transmission parameters to obtain a comparison result; when the comparison result indicates that the data transmitted by the target function during the calling process is consistent with the pre-defined transmission parameters, determining that the target interface has passed the test.
[0078] It should be noted that when conducting a test, necessary parameters and return values are adapted and transmitted according to the external interface definition and the target function definition to ensure that necessary information is passed and fed back when the function is called. Specifically, in a test environment, the target function is called based on the target interface, and the comparison result between the data transmitted by the target function during the call and the pre-defined transmission parameters of the target function is used to determine whether the target interface passes the test. The target interface that passes the test can be expanded for external program calls.
[0079] Another optional specific implementation is described in detail below.
[0080] Figure 2 is an optional non-intrusive interface expansion system architecture diagram according to an embodiment of the present invention, such as Figure 2 As shown, the non-intrusive interface expansion system interacts with the Spring application running architecture. The non-intrusive interface expansion system includes: a bytecode enhancement module, a target function object acquisition module and an external interface definition module. Specifically,
[0081] The bytecode enhancement module is a code and interface that uses JVMTI (Java Virtual Machine Tool Interface) to obtain various events, information and status in the Java virtual machine and can intervene in the JVM execution process. It highly abstracts the events, information and status that can be obtained in the JVM. Other modules can be programmed based on the interface provided by the bytecode enhancement module to define the events, information and status that need to be obtained and the subsequent execution process. The bytecode enhancement module can be developed based on JVMTI or directly use existing bytecode enhancement tools, such as ASM, Javassist, Byte Buddy, etc.
[0082] The target function object acquisition module is used to obtain the target function object, store it and provide it to the external interface definition module for calling. To obtain the target function object, the bytecode enhancement module must first enter the JVM, grab the context object of the Spring application, and use reflection to obtain the target function object from the context object.
[0083] The external interface definition module is used to define interfaces for external users to remotely call through network protocols such as HTTP. When the external interface is called, the target function is called. If necessary, the parameters and return values of the external interface and the target function are converted and adapted to ensure that the parameters and return values are correctly passed between the external interface and the target function.
[0084] The embodiment of the present invention cuts into the Java virtual machine by selecting an appropriate timing function to obtain the target function. The Java virtual machine is managed by the Spring framework. The objects of the Spring application are uniformly managed by the Spring framework in the Spring context. Therefore, to obtain the target function object, the Spring context must be obtained. To obtain the Spring context, the ClassLoader where it is located must be obtained. Therefore, selecting an appropriate timing function becomes the key to obtaining the target function object. In the embodiment of the present invention, a function that is automatically triggered in a certain period in the Spring application, or a function that can be called and triggered by an external interface is used as the timing function. The agent or attach mode of the bytecode enhancement technology is used to mount it on the running Spring application. After intercepting the call event of the timing function, the ClassLoader is obtained by the reflection method, and then the context object and the Bean object of the Spring application are obtained. Finally, the target function object is obtained and stored, waiting for active triggering.
[0085] The embodiment of the present invention uses the above-mentioned non-intrusive interface expansion system to perform non-intrusive interface expansion, expands the functions in the Java virtual machine, and provides for external application programs to call. Figure 3is a schematic diagram of an optional non-intrusive interface expansion process according to an embodiment of the present invention, such as Figure 3 As shown in the figure, the non-intrusive interface expansion process includes:
[0086] Step 1, determine the timing function;
[0087] Find a timing function that can be determined to run in the Spring application, such as a function that is called cyclically at a certain period, or a function that can be triggered by an external interface, etc. Record the full name of the class where the timing function calling code is located (including package name and class name), as well as the function name of the called function.
[0088] Step 2, intercept the timing function to obtain ClassLoader (corresponding to the above class loader);
[0089] In the bytecode enhancement module, define and register a callback function when the timing function call occurs. In the callback function, obtain the ClassLoader of the Spring application through the parameters of the callback function. Find the full name of the class that implements the ApplicationContextAware interface (corresponding to the above context interface) in the application, and use ClassLoader to obtain this class object through reflection.
[0090] Step 3: Get the context object of the Spring application;
[0091] There is a static variable applicationContext (corresponding to the above context object) in the implementation class of the ApplicationContextAware interface, which stores the Spring application context. Use the class object to access this static variable through reflection and obtain the Spring application context object.
[0092] Step 4, obtaining and storing the target function object;
[0093] Use the application context of the Spring framework to obtain the application Bean object, and then use reflection to obtain and store the target function object from the Bean object.
[0094] Step 5: Define the interface and call the target function.
[0095] Define an external interface for the obtained target function, such as HTTP interface. Get the target function object stored in step 4 in this interface, and call the target function through reflection or other means. When calling, pay attention to adapting and transmitting necessary parameters and return values according to the external interface definition and target function definition to ensure that necessary information is transmitted and fed back when the function is called.
[0096] Figure 4 This is an optional target function expansion principle diagram according to an embodiment of the present invention. Based on the bytecode enhancement technology, it is possible to access the Java virtual machine non-invasively, but it can only passively receive information from the Java application program in an event-driven manner and then change the program's running process, thereby enhancing the original program's functions, and cannot provide active triggering function expansion. Figure 4 As shown, the embodiment of the present invention utilizes the event-driven characteristics of bytecode enhancement technology, determines the appropriate timing function, cuts into the JVM (Java virtual machine), obtains the target function object and stores it in the target function storage container; when actively triggered, the external user initiates a call request, uses the previously obtained target function object, and calls the target function through reflection and other methods, thereby achieving a reversal of the control of the target function from passive to active.
[0097] The embodiment of the present invention utilizes the event-driven characteristics of bytecode enhancement technology to determine the appropriate timing function, and cuts into the Java virtual machine through the timing function, obtains the target function object and defines the target interface for it, and non-invasively expands the target function in the Java virtual machine without modifying the code to meet higher business needs.
[0098] The non-intrusive interface expansion method based on the embodiment of the present invention expands the interface without modifying the Spring application code, which not only makes it possible to meet the needs of scenarios where the code cannot be modified but the interface needs to be expanded, but also provides a new technical route for scenarios where the interface is expanded.
[0099] The embodiment of the present invention does not modify the Spring application code while expanding the interface, which can greatly reduce the risk of code modification; the interface expansion method in the embodiment of the present invention only provides a channel for external active triggering of the internal function of the Spring application, and will not interfere with or affect the original program and operation. In addition, since the bytecode enhancement technology is based on the Java mounting mechanism, when necessary, the module of the interface expansion part can be installed or uninstalled at any time while keeping the Spring application running without stopping, which greatly reduces the risk brought by the expansion interface.
[0100] In some scenarios, using the interface expansion method of the embodiment of the present invention can greatly improve work efficiency. For example, when testing a software product with a timed processing mechanism, an immediate processing interface can be expanded to save waiting time and improve testing efficiency.
[0101] The following is a detailed description in conjunction with another embodiment.
[0102] Embodiment 2
[0103] A non-invasive interface expansion device provided in this embodiment includes multiple implementation units, each implementation unit corresponds to each implementation step in the above-mentioned embodiment 1. Its specific implementation methods and beneficial effects can refer to the above-mentioned method embodiments and will not be repeated here.
[0104] Figure 5 is a schematic diagram of an optional non-intrusive interface expansion device according to an embodiment of the present invention, such as Figure 5 As shown, the non-intrusive interface expansion device may include: an acquisition unit 51, a parsing unit 52, a determination unit 53, and a definition unit 54, wherein:
[0105] The acquisition unit 51 is used to acquire a timing function from a Spring application corresponding to the target virtual machine, wherein the timing function includes at least one of the following: a function object whose calling frequency in the target period is higher than a preset calling frequency threshold, and a function object whose calling interface has been created;
[0106] The parsing unit 52 is used to obtain the callback function when the timing function is called, and parse the callback function to obtain parameter information of the callback function;
[0107] A determination unit 53 is used to determine the context object of the Spring application based on the parameter information of the callback function, and obtain the Bean object of the Spring application based on the context object, wherein the Bean object is an entry for accessing the container object in the Spring framework;
[0108] The definition unit 54 is used to obtain the target function through the Bean object and define a target interface for the target function, wherein after the target interface is defined, the external application accesses the target function through the target interface, and the external application represents a software program or service running outside the target virtual machine.
[0109] The above-mentioned non-intrusive interface expansion device obtains the timing function from the Spring application corresponding to the target virtual machine through the acquisition unit 41, wherein the timing function includes at least one of the following: a function object whose calling frequency within the target period is higher than a preset calling frequency threshold, and a function object for which a calling interface has been created; obtains the callback function when the timing function is called through the parsing unit 42, and parses the callback function to obtain parameter information of the callback function; determines the context object of the Spring application based on the parameter information of the callback function through the determination unit 43, and obtains the Bean object of the Spring application based on the context object, wherein the Bean object is an entry for accessing the container object in the Spring framework; obtains the target function through the Bean object through the definition unit 44, and defines the target interface for the target function, wherein after the target interface is defined, the external application accesses the target function through the target interface, and the external application represents a software program or service running outside the target virtual machine.
[0110] In this embodiment, an appropriate timing function is selected, and the context object of the Spring application is obtained through the callback function when the timing function is called. The entrance to the container object in the Spring framework is obtained according to the context object, thereby entering the Java virtual machine, selecting the target function from the Java virtual machine and defining the target interface, so that the application program outside the Java virtual machine can call the target function through the target interface, thereby realizing the expansion of the function objects in the Java virtual machine in a non-invasive manner without modifying the code, meeting more business needs, and thus solving the technical problem in the related technology that when the internal functions of the virtual machine are expanded outward, it is necessary to modify the code to achieve it, which will cause the virtual machine to have security risks.
[0111] Optionally, the acquisition unit 51 includes: a first monitoring module, used to monitor the Spring application in the target virtual machine, obtain the function objects called in the Spring application and the calling frequency of the function objects within the target period; a first selection module, used to select the function object as the timing function when the calling frequency of the function object within the target period is greater than a preset calling frequency threshold.
[0112] Optionally, the acquisition unit 51 includes: a first acquisition module, used to acquire the created call interface in the Spring application; a first acting module, used to call the function object in the Spring program through the created call interface, and use the successfully called function object as the timing function.
[0113] Optionally, the determination unit 53 includes: a second acquisition module, used to obtain the class loader of the Spring application through parameter information; a third acquisition module, used to use the class loader to obtain the class object that implements the context interface through reflection; and a first determination module, used to determine the context object of the Spring application from the class object through reflection.
[0114] Optionally, the non-invasive interface expansion device also includes: a first testing module, used to test the target interface and obtain a test result; a first storage module, used to store the target function and the target interface corresponding to the target function into a target function storage container when the test result indicates that the target interface passes the test.
[0115] Optionally, the first test module includes: a first calling sub-module, used to call the target function based on the target interface in the test environment, and obtain the data transmitted by the target function during the calling process; a first comparison sub-module, used to obtain the transmission parameters pre-defined by the target function, and compare the data transmitted by the target function during the calling process with the pre-defined transmission parameters to obtain a comparison result; a first determination sub-module, used to determine that the target interface has passed the test when the comparison result indicates that the data transmitted by the target function during the calling process is consistent with the pre-defined transmission parameters.
[0116] Optionally, the type of parameter information of the callback function includes at least one of the following: event parameter information of a processing event involved in executing the callback function, result parameter information of a processing event involved in executing the callback function, and state parameter information of a processing event involved in executing the callback function.
[0117] The above-mentioned non-intrusive interface expansion device may also include a processor and a memory. The above-mentioned acquisition unit 51, analysis unit 52, determination unit 53, definition unit 54, etc. are all stored in the memory as program units, and the processor executes the above-mentioned program units stored in the memory to realize corresponding functions.
[0118] The processor includes a kernel, which retrieves the corresponding program unit from the memory. One or more kernels can be set, and the target function corresponding to the virtual machine can be expanded by adjusting the kernel parameters.
[0119] The above-mentioned memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash memory (fl ash RAM), and the memory includes at least one storage chip.
[0120] According to another aspect of an embodiment of the present invention, a computer-readable storage medium is provided, the computer-readable storage medium including a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute any one of the above-mentioned non-intrusive interface expansion methods.
[0121] According to another aspect of an embodiment of the present invention, there is also provided an electronic device, comprising one or more processors and a memory, wherein the memory is used to store one or more programs, wherein when the one or more programs are executed by one or more processors, the one or more processors implement any one of the above-mentioned non-intrusive interface expansion methods.
[0122] According to another aspect of an embodiment of the present invention, a computer program product is further provided. The computer program product includes a computer program, wherein when the computer program is executed by a processor, any one of the above-mentioned non-intrusive interface expansion methods is implemented.
[0123] The present application also provides a computer program product, which, when executed on a data processing device, is suitable for executing a program that is initialized with the following method steps: obtaining a timing function from a Spring application corresponding to a target virtual machine, wherein the timing function includes at least one of the following: a function object whose calling frequency within a target period is higher than a preset calling frequency threshold, and a function object whose calling interface has been created; obtaining a callback function when the timing function is called, and parsing the callback function to obtain parameter information of the callback function; determining a context object of the Spring application based on the parameter information of the callback function, and obtaining a Bean object of the Spring application based on the context object, wherein the Bean object is an entry for accessing a container object in a Spring framework; obtaining a target function through the Bean object, and defining a target interface for the target function, wherein after defining the target interface, an external application accesses the target function through the target interface, and the external application represents a software program or service running outside the target virtual machine.
[0124] The present application also provides a computer program product, which, when executed on a data processing device, is also suitable for executing a program that is initialized with the following method steps: the step of obtaining a timing function from a Spring application includes: monitoring the Spring application in a target virtual machine, obtaining function objects called in the Spring application and the calling frequency of the function objects within a target period; when the calling frequency of the function object within the target period is greater than a preset calling frequency threshold, selecting the function object as the timing function.
[0125] The present application also provides a computer program product, which, when executed on a data processing device, is also suitable for executing a program that initializes the following method steps: the step of obtaining a timing function from a Spring application includes: obtaining a created calling interface in the Spring application; calling a function object in the Spring program through the created calling interface, and using the successfully called function object as the timing function.
[0126] The present application also provides a computer program product, which, when executed on a data processing device, is also suitable for executing a program that is initialized with the following method steps: the step of determining the context object of the Spring application based on the parameter information of the callback function includes: obtaining the class loader of the Spring application through the parameter information; using the class loader to obtain the class object that implements the context interface through reflection; and determining the context object of the Spring application from the class object through reflection.
[0127] The present application also provides a computer program product, which, when executed on a data processing device, is also suitable for executing a program that is initialized with the following method steps: after defining a target interface for a target function, it also includes: testing the target interface to obtain a test result; when the test result indicates that the target interface passes the test, storing the target function and the target interface corresponding to the target function in a target function storage container.
[0128] The present application also provides a computer program product, which, when executed on a data processing device, is also suitable for executing a program that is initialized with the following method steps: the step of testing the target interface includes: in a test environment, calling a target function based on the target interface, and obtaining data transmitted by the target function during the calling process; obtaining transmission parameters pre-defined by the target function, and comparing the data transmitted by the target function during the calling process with the pre-defined transmission parameters to obtain a comparison result; when the comparison result indicates that the data transmitted by the target function during the calling process is consistent with the pre-defined transmission parameters, determining that the target interface has passed the test.
[0129] The present application also provides a computer program product, which, when executed on a data processing device, is also suitable for executing a program that is initialized with the following method steps: the type of parameter information of the callback function includes at least one of the following: event parameter information of the processing event involved when the callback function is executed, result parameter information of the processing event involved when the callback function is executed, and state parameter information of the processing event involved when the callback function is executed.
[0130] Figure 6 1 is a hardware structure block diagram of an electronic device (or mobile device) according to a non-intrusive interface expansion method according to an embodiment of the present invention. Figure 6As shown, the electronic device may include one or more ( Figure 6 602a, 602b, ..., 602n are used to illustrate) processor 602 (processor 602 may include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA), and memory 604 for storing data. In addition, it may also include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which may be included as one of the ports of the I / O interface), a network interface, a keyboard, a power supply and / or a camera. It can be understood by those skilled in the art that Figure 6 The structure shown is only for illustration and does not limit the structure of the above electronic device. Figure 6 More or fewer components as shown, or with Figure 6 Different configurations are shown.
[0131] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.
[0132] In the above embodiments of the present invention, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0133] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of the units can be a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0134] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0135] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0136] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, magnetic disk or optical disk and other media that can store program codes.
[0137] The above is only a preferred embodiment 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 principle of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A non-intrusive interface expansion method, It is characterized in that Applied to a non-intrusive interface expansion system, the non-intrusive interface expansion method includes: Obtain a timing function from a Spring application corresponding to the target virtual machine, wherein the timing function includes at least one of the following: a function object whose calling frequency in the target period is higher than a preset calling frequency threshold, and a function object whose calling interface has been created; Obtaining a callback function when the timing function is called, and parsing the callback function to obtain parameter information of the callback function; Determine the context object of the Spring application based on the parameter information of the callback function, and obtain the Bean object of the Spring application based on the context object, wherein the Bean object is an entry for accessing a container object in the Spring framework; The target function is obtained through the Bean object, and a target interface is defined for the target function, wherein after the target interface is defined, an external application accesses the target function through the target interface, and the external application represents a software program or service running outside the target virtual machine.
2. The method according to claim 1, It is characterized in that The steps to get timing functions from a Spring application include: Monitor the Spring application in the target virtual machine to obtain the function objects called in the Spring application and the calling frequency of the function objects in the target cycle; When the calling frequency of the function object in the target period is greater than the preset calling frequency threshold, the function object is selected as the timing function.
3. The method according to claim 1, It is characterized in that The steps to get timing functions from a Spring application include: Obtain the created calling interface in the Spring application; The function object in the Spring program is called through the created calling interface, and the successfully called function object is used as the timing function.
4. The method according to claim 1, It is characterized in that The step of determining the context object of the Spring application based on the parameter information of the callback function includes: Obtain the class loader of the Spring application through the parameter information; Using the class loader to obtain the class object that implements the context interface by reflection; The context object of the Spring application is determined from the class object by reflection.
5. The method according to claim 1, It is characterized in that After defining the target interface for the target function, it also includes: Testing the target interface to obtain a test result; When the test result indicates that the target interface passes the test, the target function and the target interface corresponding to the target function are stored in a target function storage container.
6. The method according to claim 5, It is characterized in that The step of testing the target interface comprises: In a test environment, calling the target function based on the target interface, and obtaining data transmitted by the target function during the calling process; Acquire transmission parameters predefined by the target function, compare data transmitted by the target function during the calling process with the predefined transmission parameters, and obtain a comparison result; When the comparison result indicates that the data transmitted by the target function during the calling process is consistent with the predefined transmission parameters, it is determined that the target interface passes the test.
7. The method according to claim 1, It is characterized in that The type of parameter information of the callback function includes at least one of the following: event parameter information of a processing event involved in the execution of the callback function, result parameter information of a processing event involved in the execution of the callback function, and state parameter information of a processing event involved in the execution of the callback function.
8. A non-intrusive interface expansion device, It is characterized in that Applied to a non-intrusive interface expansion system, the non-intrusive interface expansion device comprises: An acquisition unit is used to acquire a timing function from a Spring application corresponding to a target virtual machine, wherein the timing function includes at least one of the following: a function object whose calling frequency in a target period is higher than a preset calling frequency threshold, and a function object whose calling interface has been created; A parsing unit, used for obtaining a callback function when the timing function is called, and parsing the callback function to obtain parameter information of the callback function; A determination unit, configured to determine a context object of the Spring application based on parameter information of the callback function, and obtain a Bean object of the Spring application based on the context object, wherein the Bean object is an entry for accessing a container object in a Spring framework; A definition unit is used to obtain a target function through the Bean object and define a target interface for the target function, wherein after the target interface is defined, an external application accesses the target function through the target interface, and the external application represents a software program or service running outside the target virtual machine.
9. A computer-readable storage medium, It is characterized in that The computer-readable storage medium includes a stored computer program, wherein when the computer program is executed, the device where the computer-readable storage medium is located is controlled to execute the non-intrusive interface expansion method according to any one of claims 1 to 7.
10. An electronic device, It is characterized in that It includes one or more processors and a memory, wherein the memory is used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the non-intrusive interface expansion method described in any one of claims 1 to 7.
Citation Information
Cited By
Non-intrusive interface expansion method and apparatus, electronic device, and storage medium
WO2026067559A1
