A dynamic plug-in method based on SpringBoot
By using global plugin processor mapping and custom plugin Spring containers, the problems of dependency injection and bean conflicts in Spring Boot dynamic plugins are solved, achieving high scalability and high availability, and improving the maintainability and extensibility of the application.
Patent Information
- Application Number
- CN202411055504.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-02
- Publication Date
- 2024-11-08
- Estimated Expiration
- 2044-08-02
AI Technical Summary
Existing technologies based on Spring Boot's dynamic plugin implementation methods cannot solve dependency injection and dynamic proxy issues, and are prone to bean conflicts and difficulties in modification and deletion, thus failing to achieve high scalability and high availability.
By employing a global plugin processor mapping, a custom class loader, and a custom plugin Spring container, HTTP requests are forwarded to different plugin controller mappings to ensure that Class objects do not conflict and to resolve dependency injection and Bean object conflicts, achieving high scalability and high flexibility.
It achieves high extensibility, high availability, and high flexibility of dynamic plugins based on Spring Boot, solves dependency injection and bean conflict issues, and improves the maintainability and scalability of the application.
Smart Images

Figure CN118567754B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a dynamic plug-in method based on SpringBoot. Background Art
[0002] At present, enterprise-level applications are basically developed based on the SpringBoot framework. SpringBoot is an open source framework based on Spring that can simplify the creation and deployment of Spring applications. Spring is a framework that supports rapid development of Java EE applications. It provides a series of underlying containers and infrastructure.
[0003] Dynamic plug-ins can achieve business decoupling of enterprise-level applications, facilitate maintenance, and improve the scalability of applications. They can dynamically expand functions without restarting the application. Common dynamic plug-in implementation methods include SPI, OSGI, etc. These methods are separated from the management of the Spring container and cannot implement the dynamic plug-in function based on SpringBoot. They destroy the integrity of the Spring container through reflection, and cannot solve the dependency injection and dynamic proxy problems. They are prone to Bean conflicts and difficulty in modification and deletion. Summary of the invention
[0004] The purpose of the present invention is to solve the shortcomings of the prior art and to propose a method of dynamic plug-in based on SpringBoot.
[0005] To achieve the above object, the present invention adopts the following technical solutions:
[0006] A method for dynamic plug-in based on SpringBoot, comprising the following steps:
[0007] S1: Create a global plug-in processor mapping and forward the corresponding HTTP request to the plug-in processor mapping;
[0008] The following sub-steps are included:
[0009] S11: Create a global plug-in processor mapping GlobalHandlerMapping, the processor mapping inherits the RequestMappingHandlerMapping group, and rewrites the getHandlerInternal method;
[0010] The parameter of the getHandlerInternal method is the HttpServletRequest request;
[0011] S12: Obtain the URI path from the HttpServletRequest request, and loop through each key in the global variable pluginContextMap according to the URI path to determine whether the URI path matches the plugin path in the key;
[0012] The data type of the global variable pluginContextMap is a Map key-value pair, where the key is the plugin name and plugin path, and the value is the plugin Spring container and plugin processor mapping;
[0013] S13: If the URI path matches the plugin path in the key, obtain the value corresponding to the key, obtain the plugin handler mapping from the value, execute the getHandlerInternal method of the plugin handler mapping, and forward the URI path that matches the plugin path to the corresponding plugin handler mapping.
[0014] S14: If the URI path does not match the plugin path in the key, execute the getHandlerInternal method of the parent class to release the request;
[0015] S15: Register the global plugin processor mapping to the Spring container of the current environment.
[0016] S2: Upload and modify dynamic plug-in files, create plug-in Spring container and plug-in processor mapping, and register them in the global variable pluginContextMap;
[0017] The following sub-steps are included:
[0018] S21: receiving the uploaded dynamic plug-in file and verifying it;
[0019] Receive a plug-in file uploaded by a user, wherein the plug-in file corresponds to a plug-in file type including a RAR, ZIP compressed file type and a JAR file type;
[0020] Check the format of the plugin file;
[0021] Specifically, the plug-in file type is determined according to the file extension or file header information; if the plug-in file type does not belong to the RAR, ZIP compressed file type or JAR file type, it is considered abnormal; if the plug-in file type belongs to the RAR, ZIP compressed file type or JAR file type, for RAR and ZIP compressed files, verify whether the plug-in file can be decompressed and the content therein can be accessed; if it can, the plug-in file is normal; if not, the plug-in file is abnormal; for JAR files, verify whether the plug-in file is a valid JAR package file, if it is, the plug-in file is normal; if not, the plug-in file is abnormal;
[0022] For an abnormal plug-in file, an exception is thrown and no subsequent processing is performed on the plug-in file; for a normal plug-in file, step S22 is performed;
[0023] S22: read the plugin information of the uploaded dynamic plugin file and load the Class file into the JVM virtual machine; and store the successfully loaded Class object into a List collection whose element is Class, recorded as pluginClassList;
[0024] Create a corresponding plug-in file class loader according to the plug-in file type to read the plug-in file content and load the Class file into the JVM virtual machine;
[0025] Furthermore, a RAR class loader is created for a RAR compressed file, a ZIP class loader is created for a ZIP compressed file, and a JAR class loader is created for a JAR file. These corresponding class loaders load the non-class file resources in the dynamic plug-in file into the application and load the class files in the file into the JVM virtual machine.
[0026] Specifically, first determine whether there is a plugin.json file in the META-INF / plugins directory of the plugin file; if the plugin.json file does not exist, throw an exception and do not execute the next step; if the plugin.json file exists, read the content of the plugin.json file.
[0027] The plugin.json file contains information such as the plugin name, plugin version, plugin path, and developer; obtain the plugin name and plugin path in the plugin.json file;
[0028] Loop through each file in the plugin file. If it is a Class file, load the Class file into the JVM and store the successfully loaded Class object into a List collection whose element is Class, recorded as pluginClassList; if it is not a Class file, load the file directly into the application;
[0029] S23: Create a custom plug-in Spring container, set the parent container to the Spring container in the current environment, loop through each element Class object in pluginClassList, register the Class object to the custom plug-in Spring container, execute the refresh method, and refresh the custom plug-in Spring container;
[0030] The Spring container in the current environment is the Spring container created when the application is started;
[0031] S24: Create a custom plug-in processor mapping PluginHandlerMapping, set the container of the custom plug-in processor mapping to the custom plug-in Spring container, and execute the afterPropertiesSet initialization method of the custom plug-in processor mapping;
[0032] S25: Save the custom plug-in Spring container and the custom plug-in processor mapping to the global variable pluginContextMap;
[0033] According to the plug-in path obtained in step S22, each key in the global variable pluginContextMap is looped through; and it is determined whether there is a matching plug-in path in the key of pluginContextMap.
[0034] If it exists, delete the key and the corresponding value from the global variable pluginContextMap, obtain the plug-in Spring container from the deleted value, call the plug-in Spring container's shutdown method, and close the deleted plug-in Spring container.
[0035] If it does not exist, the plug-in name and plug-in path obtained in step S22 are used as keys, and the custom plug-in Spring container and custom plug-in processor mapping are used as values to insert into the global variable pluginContextMap.
[0036] S3: Delete the dynamic plugin from the global variable pluginContextMap according to the plugin path;
[0037] The following sub-steps are included:
[0038] S31: receiving the plug-in name and plug-in path, looping through each key in the global variable pluginContextMap, and determining whether there is a matching plug-in path and plug-in name in the key of pluginContextMap;
[0039] S32: If it does not exist, an exception is thrown directly and the process ends;
[0040] S33: If it exists, delete the key and the corresponding value from the global variable pluginContextMap, obtain the plug-in Spring container from the deleted value, call the close method of the plug-in Spring container, and close the deleted plug-in Spring container.
[0041] Compared with the prior art, the present invention has the following beneficial effects:
[0042] The present invention proposes a new method to realize flexible dynamic plug-ins based on Spring Boot. By creating a custom class loader, a custom plug-in controller mapping and a custom plug-in Spring container for each dynamic plug-in, a global plug-in controller mapping is created to intercept HTTP requests, and the HTTP requests are forwarded to different plug-in controller mappings. The custom class loader ensures that there will be no Class object conflict. The custom plug-in Spring container perfectly solves a series of problems such as dependency injection, dynamic proxy, container Bean object conflict, modification and deletion difficulties, etc., to achieve high scalability, high availability and high flexibility. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 The present invention is a flow chart of the steps of a method for a dynamic plug-in based on SpringBoot. DETAILED DESCRIPTION
[0044] In order to provide a further understanding of the purpose, structure, features, and functions of the present invention, the following detailed description is given in conjunction with the embodiments.
[0045] like Figure 1 As shown, a method of dynamic plug-in based on SpringBoot includes the following steps:
[0046] S1: Create a global plug-in processor mapping and forward the corresponding HTTP request to the plug-in processor mapping;
[0047] The following sub-steps are included:
[0048] S11: Create a global plug-in processor mapping GlobalHandlerMapping, the processor mapping inherits the component RequestMappingHandlerMapping, and rewrites the getHandlerInternal method;
[0049] The parameter of the getHandlerInternal method is the HttpServletRequest request;
[0050] S12: Obtain the URI path from the HttpServletRequest request, and loop through each key in the global variable pluginContextMap according to the URI path to determine whether the URI path matches the plugin path in the key;
[0051] The data type of the global variable pluginContextMap is a Map key-value pair, where the key is the plugin name and plugin path, and the value is the plugin Spring container and plugin processor mapping;
[0052] S13: If the URI path matches the plugin path in the key, obtain the value corresponding to the key, obtain the plugin handler mapping from the value, execute the getHandlerInternal method of the plugin handler mapping, and forward the URI path that matches the plugin path to the corresponding plugin handler mapping.
[0053] S14: If the URI path does not match the plugin path in the key, execute the getHandlerInternal method of the parent class to release the request;
[0054] S15: Register the global plugin processor mapping to the Spring container of the current environment.
[0055] S2: Upload and modify dynamic plug-in files, create plug-in Spring container and plug-in processor mapping, and register them in the global variable pluginContextMap;
[0056] The following sub-steps are included:
[0057] S21: receiving the uploaded dynamic plug-in file and verifying it;
[0058] Receive a plug-in file uploaded by a user, wherein the plug-in file corresponds to a plug-in file type including a RAR, ZIP compressed file type and a JAR file type;
[0059] Check the format of the plugin file;
[0060] Specifically, the plug-in file type is determined according to the file extension or file header information; if the plug-in file type does not belong to the RAR, ZIP compressed file type or JAR file type, it is considered abnormal; if the plug-in file type belongs to the RAR, ZIP compressed file type or JAR file type, for RAR and ZIP compressed files, verify whether the plug-in file can be decompressed and the content therein can be accessed; if it can, the plug-in file is normal; if not, the plug-in file is abnormal; for JAR files, verify whether the plug-in file is a valid JAR package file, if it is, the plug-in file is normal; if not, the plug-in file is abnormal;
[0061] For an abnormal plug-in file, an exception is thrown and no subsequent processing is performed on the plug-in file; for a normal plug-in file, step S22 is performed;
[0062] S22: read the plugin information of the uploaded dynamic plugin file and load the Class file into the JVM virtual machine; and store the successfully loaded Class object into a List collection whose element is Class, recorded as pluginClassList;
[0063] Create a corresponding plug-in file class loader according to the plug-in file type to read the plug-in file content and load the Class file into the JVM virtual machine;
[0064] Furthermore, a RAR class loader is created for a RAR compressed file, a ZIP class loader is created for a ZIP compressed file, and a JAR class loader is created for a JAR file. These corresponding class loaders load the non-class file resources in the dynamic plug-in file into the application and load the class files in the file into the JVM virtual machine.
[0065] Specifically, first determine whether there is a plugin.json file in the META-INF / plugins directory of the plugin file; if the plugin.json file does not exist, throw an exception and do not execute the next step; if the plugin.json file exists, read the content of the plugin.json file.
[0066] The plugin.json file contains information such as the plugin name, plugin version, plugin path, and developer; obtain the plugin name and plugin path in the plugin.json file;
[0067] Loop through each file in the plugin file. If it is a Class file, load the Class file into the JVM and store the successfully loaded Class object into a List collection whose element is Class, recorded as pluginClassList; if it is not a Class file, load the file directly into the application;
[0068] S23: Create a custom plug-in Spring container, set the parent container to the Spring container in the current environment, loop through each element Class object in pluginClassList, register the Class object to the custom plug-in Spring container, execute the refresh method, and refresh the custom plug-in Spring container;
[0069] The Spring container in the current environment is the Spring container created when the application is started;
[0070] S24: Create a custom plug-in processor mapping PluginHandlerMapping, set the container of the custom plug-in processor mapping to the custom plug-in Spring container, and execute the afterPropertiesSet initialization method of the custom plug-in processor mapping;
[0071] S25: Save the custom plug-in Spring container and the custom plug-in processor mapping to the global variable pluginContextMap;
[0072] According to the plug-in path obtained in step S22, each key in the global variable pluginContextMap is looped through; and it is determined whether there is a matching plug-in path in the key of pluginContextMap.
[0073] If it exists, delete the key and the corresponding value from the global variable pluginContextMap, obtain the plug-in Spring container from the deleted value, call the plug-in Spring container's shutdown method, and close the deleted plug-in Spring container.
[0074] If it does not exist, the plug-in name and plug-in path obtained in step S22 are used as keys, and the custom plug-in Spring container and custom plug-in processor mapping are used as values to insert into the global variable pluginContextMap.
[0075] S3: Delete the dynamic plugin from the global variable pluginContextMap according to the plugin path;
[0076] The following sub-steps are included:
[0077] S31: receiving the plug-in name and plug-in path, looping through each key in the global variable pluginContextMap, and determining whether there is a matching plug-in path and plug-in name in the key of pluginContextMap;
[0078] S32: If it does not exist, an exception is thrown directly and the process ends;
[0079] S33: If it exists, delete the key and the corresponding value from the global variable pluginContextMap, obtain the plug-in Spring container from the deleted value, call the close method of the plug-in Spring container, and close the deleted plug-in Spring container.
[0080] The present invention has been described by the above-mentioned relevant embodiments, however, the above-mentioned embodiments are only examples for implementing the present invention. It must be pointed out that the disclosed embodiments do not limit the scope of the present invention. On the contrary, changes and modifications made without departing from the spirit and scope of the present invention are all within the scope of patent protection of the present invention.
Claims
1. A method of dynamic plug-in based on SpringBoot, characterized in that: The following steps are involved: S1: Create a global plug-in processor mapping and forward the corresponding HTTP request to the plug-in processor mapping; The following sub-steps are included: S11: Create a global plug-in processor mapping GlobalHandlerMapping, the processor mapping inherits the component RequestMappingHandlerMapping, and rewrites the getHandlerInternal method; The parameter of the getHandlerInternal method is the HttpServletRequest request; S12: Obtain the URI path from the HttpServletRequest request, and loop through each key in the global variable pluginContextMap according to the URI path to determine whether the URI path matches the plugin path in the key; The data type of the global variable pluginContextMap is a Map key-value pair, where the key is the plugin name and plugin path, and the value is the plugin Spring container and plugin processor mapping; S13: If the URI path matches the plug-in path in the key, obtain the value corresponding to the key, obtain the plug-in processor mapping from the value, execute the getHandlerInternal method of the plug-in processor mapping, and forward the URI path that matches the plug-in path to the corresponding plug-in processor mapping; S14: If the URI path does not match the plugin path in the key, execute the getHandlerInternal method of the parent class to release the request; S15: Register the global plug-in processor mapping to the Spring container of the current environment; S2: Upload and modify dynamic plug-in files, create plug-in Spring container and plug-in processor mapping, and register them in the global variable pluginContextMap; The following sub-steps are included: S21: receiving the uploaded dynamic plug-in file and verifying it; Receive a plug-in file uploaded by a user, wherein the plug-in file corresponds to a plug-in file type including a RAR, ZIP compressed file type and a JAR file type; S22: Read the plugin information of the uploaded dynamic plugin file and load the Class file into the JVM virtual machine; Create a corresponding plug-in file class loader according to the plug-in file type to read the plug-in file content and load the Class file into the JVM virtual machine; and store the successfully loaded Class object into a List collection whose element is Class, recorded as pluginClassList; S23: Create a custom plug-in Spring container, set the parent container to the Spring container in the current environment, loop through each element Class object in the plug-in pluginClassList, register the Class object to the custom plug-in Spring container, execute the refresh method, and refresh the custom plug-in Spring container; The Spring container in the current environment is the Spring container created when the application is started; S24: Create a custom plug-in processor mapping PluginHandlerMapping, set the container of the custom plug-in processor mapping to the custom plug-in Spring container, and execute the afterPropertiesSet initialization method of the custom plug-in processor mapping; S25: Save the custom plug-in Spring container and the custom plug-in processor mapping to the global variable pluginContextMap; S3: Delete the dynamic plugin from the global variable pluginContextMap according to the plugin path.
2. The method of dynamic plug-in based on SpringBoot as claimed in claim 1, characterized in that: In step S21, the plug-in file is format checked, and the specific contents are as follows: Determine the plug-in file type based on the file extension or file header information; if the plug-in file type does not belong to the RAR, ZIP compressed file type or JAR file type, it is considered abnormal; if the plug-in file type belongs to the RAR, ZIP compressed file type or JAR file type, for RAR and ZIP compressed files, verify whether the plug-in file can be decompressed and the content can be accessed; if so, the plug-in file is normal; If not, the plug-in file is abnormal. For JAR files, verify whether the plug-in file is a valid JAR package file. If it is, the plug-in file is normal. If not, the plug-in file is abnormal. For an abnormal plug-in file, an exception is thrown and no subsequent processing is performed on the plug-in file; for a normal plug-in file, step S22 is performed.
3. The method of dynamic plug-in based on SpringBoot as claimed in claim 1, characterized in that: The specific contents of step S22 are as follows: For RAR compressed files, a RAR class loader is created; for ZIP compressed files, a ZIP class loader is created; for JAR files, a JAR class loader is created. These corresponding class loaders load the non-class file resources in the dynamic plug-in file into the application and load the class files in the file into the JVM virtual machine; Specifically, first determine whether there is a plugin.json file in the META-INF / plugins directory of the plugin file; If the plugin.json file does not exist, an exception is thrown and the next step is not executed; if the plugin.json file exists, the plugin.json file content is read; The plugin.json file contains the plugin name, plugin version, plugin path, and developer information; Get the plugin name and plugin path in the plugin.json file; Loop through each file in the plugin file. If it is a Class file, load the Class file into the JVM and store the successfully loaded Class object into a List collection whose element is Class, recorded as pluginClassList; if it is not a Class file, load the file directly into the application.
4. The method of dynamic plug-in based on SpringBoot as claimed in claim 1, characterized in that: In step S25, according to the plug-in path obtained in step S22, each key in the global variable pluginContextMap is looped through; and it is determined whether there is a matching plug-in path in the key of pluginContextMap; If it exists, delete the key and the corresponding value from the global variable pluginContextMap, obtain the plug-in Spring container from the deleted value, call the plug-in Spring container's close method, and close the deleted plug-in Spring container; If it does not exist, the plug-in name and plug-in path obtained in step S22 are used as keys, and the custom plug-in Spring container and custom plug-in processor mapping are used as values to insert into the global variable pluginContextMap.
5. The method of dynamic plug-in based on SpringBoot as claimed in claim 1, characterized in that: Step S3 includes the following sub-steps: S31: receiving the plug-in name and plug-in path, looping through each key in the global variable pluginContextMap, and determining whether there is a matching plug-in path and plug-in name in the key of pluginContextMap; S32: If it does not exist, an exception is thrown directly and the process ends; S33: If it exists, delete the key and the corresponding value from the global variable pluginContextMap, obtain the plug-in Spring container from the deleted value, call the closing method of the plug-in Spring container, and close the deleted plug-in Spring container.
