Method for hot updating Bean in Spring
The method addresses the cumbersome nature of Bean updates in Spring by using a custom hot swap post-processor and dynamic proxies to perform non-invasive hot updates, solving AOP and IoC issues and enabling dynamic code updates without application downtime.
Patent Information
- Application Number
- CN202411236380.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-04
- Publication Date
- 2024-12-24
AI Technical Summary
Existing methods for updating Beans in the Spring framework are cumbersome and require downtime, failing to address Aspect Oriented Programming (AOP) and Inversion of Control (IoC) issues, and cannot perform hot updates without complex processes.
A method involving a custom hot swap bean post-processor and dynamic proxy beans to intercept and update Beans within the Spring container, allowing non-invasive hot updates without application restarts, using a custom Spring container to manage Bean creation and updates.
Enables non-invasive hot updates of Beans, solving AOP and IoC problems, allowing developers to update code dynamically without affecting users, and facilitating seamless testing between development and production environments.
Smart Images

Figure CN119179510A_ABST
Abstract
Description
Technical Field
[0001] The patent of this invention relates to the technical field of hot update of Bean in Spring, specifically a method for hot update of Bean in Spring. Background Art
[0002] At present, enterprise-level applications are basically developed based on the Spring framework of Java language. Spring is a framework that supports rapid development of Java EE applications. Its core is Inversion of Control (IoC) and Aspect Oriented Programming (AOP). All created Bean objects are managed by Spring's container, so replacing Beans can update functions. However, to release Java code to the production environment, developers need to submit the code to git or other code management platforms. Operation and maintenance personnel use operation and maintenance tools to package the Java project code on git into a jar package, send it to the application container and start the application.
[0003] When bugs or other problems occur in online software, the developers have to submit the code and the operation and maintenance personnel have to publish it. This is a cumbersome process and hot updates cannot be performed without downtime. In addition, the Bean creation process is a very complicated process. The traditional way of creating Beans cannot solve AOP and IoC problems and cannot achieve true replacement of Beans.
[0004] Invention patent content
[0005] The purpose of this patent is to provide a method for hot updating Bean in Spring to solve the problems raised in the above background technology.
[0006] In order to solve the above technical problems, the present invention provides the following technical solutions: a method for hot updating Bean in Spring, comprising the following steps:
[0007] S1: Create a custom hot update Bean post-processor and inject it into the Spring container;
[0008] The step S1 includes the following sub-steps:
[0009] S11: Create a custom hot swap bean postprocessor HotSwapBeanPostProcessor to implement the Spring extension point BeanPostProcessor interface and override the postProcessBeforeInitialization method. The execution process of this method is as follows:
[0010] M11: Read configuration information and obtain the scan path;
[0011] M12: Determine whether the original bean belongs to the scan path;
[0012] If it does not belong to the scan path, the original bean is returned;
[0013] If it belongs to the scan path, create a dynamic proxy bean of the original bean;
[0014] The dynamic proxy Bean is used to intercept each method of the original Bean;
[0015] The process of the dynamic proxy bean intercepting each method of the original bean includes the following steps:
[0016] P1: According to the Class name of the original Bean object, query and update the Bean from the global variable proxy Bean mapping;
[0017] P2: If the update bean is not empty, execute the update bean method;
[0018] P3: If the updated bean is empty, the method of the original bean is executed;
[0019] M13: Store the key as the class name of the original bean, the value as the original bean object and the null value in the global variable proxy bean mapping of the data type Map;
[0020] The null value is a placeholder for updating the Bean;
[0021] M14: Returns the dynamic proxy bean;
[0022] S12: Use the @Compoment annotation in the configuration class to inject the HotSwapBeanPostProcessor into the Spring container.
[0023] S2: Create or update the update bean in the proxy bean mapping;
[0024] The step S2 includes the following sub-steps:
[0025] S21: receiving source code;
[0026] The source of the source code includes text code / code file uploaded by the user, text code stored in the database, and text code / code file stored in other storage systems. When receiving the source code, any one of the text code / code file uploaded by the user, the text code / code file stored in the database, and the text code / code file stored in other storage systems is received;
[0027] S22: Create a custom Java compiler, load the source code, and compile the source code into bytecode;
[0028] S23: Create a custom class loader, load the bytecode, load the bytecode into the JVM virtual machine, and generate a Class object.
[0029] S24: Create a custom Spring container;
[0030] S25: Set the parent container of the custom Spring container to the container in the current environment.
[0031] The container in the current environment is the container created when the application is started;
[0032] S26: Get the Bean factory of the custom container;
[0033] S27: Pass in the Class object, execute the create Bean method of the Bean factory, and get the updated Bean;
[0034] S28: Close the custom Spring container.
[0035] S29: Get the Class name according to the Class object;
[0036] S210: Get the value of the name of the Class key from the proxy Bean mapping;
[0037] S211: Replace the update bean in the value with the created update bean.
[0038] S3: Delete the update bean of the specified key in the proxy bean mapping;
[0039] The step S3 includes the following sub-steps:
[0040] S31: Receive Class name;
[0041] S32: According to the Class name, determine whether there is a value with the key of the Class name in the proxy Bean mapping;
[0042] If it does not exist, an error message indicating that the Class name does not exist is returned;
[0043] If it exists, execute S33;
[0044] S33: Replace the update bean in value with null value.
[0045] The beneficial effects achieved by the patent of the present invention are: based on the custom Spring container, the creation of updated beans is realized, the AOP and IoC problems that cannot be handled by the traditional way of creating beans are solved, and non-invasive code hot updates are realized based on dynamic proxies, thereby solving the problem of hot updates of beans in Spring. Developers can update the code without restarting the application, and the dynamic proxy method is non-invasive to the code, saving a lot of time, and developers can use hot updates to solve the problem that the development environment cannot simulate the production environment test. Developers can update at any time as needed, roll back at any time, switch code versions at any time, and delete updates. Users are not aware of it. The software enhances the application in the form of dynamic proxy without any impact, and the software can be removed at any time without causing any impact on the application. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] Figure 1 The figure is a flow chart of a method for hot updating Bean in Spring in an embodiment. DETAILED DESCRIPTION
[0047] The present invention is further described below in conjunction with the accompanying drawings.
[0048] like Figure 1 As shown, the present invention discloses a method for hot updating Bean in Spring, including the following steps:
[0049] S1: Create a custom hot update Bean post-processor and inject it into the Spring container;
[0050] The step S1 includes the following sub-steps:
[0051] S11: Create a custom hot swap bean postprocessor HotSwapBeanPostProcessor to implement the Spring extension point BeanPostProcessor interface and override the postProcessBeforeInitialization method. The execution process of this method is as follows:
[0052] M11: Read configuration information and obtain the scan path;
[0053] M12: Determine whether the original bean belongs to the scan path;
[0054] If it does not belong to the scan path, the original bean is returned;
[0055] If it belongs to the scan path, create a dynamic proxy bean of the original bean;
[0056] The dynamic proxy Bean is used to intercept each method of the original Bean;
[0057] The process of the dynamic proxy bean intercepting each method of the original bean includes the following steps:
[0058] P1: According to the Class name of the original Bean object, query and update the Bean from the global variable proxy Bean mapping;
[0059] P2: If the update bean is not empty, execute the update bean method;
[0060] P3: If the updated bean is empty, the method of the original bean is executed;
[0061] M13: Store the key as the class name of the original bean, the value as the original bean object and the null value in the global variable proxy bean mapping of the data type Map;
[0062] The null value is a placeholder for updating the Bean;
[0063] M14: Returns the dynamic proxy bean;
[0064] S12: Use the @Compoment annotation in the configuration class to inject the HotSwapBeanPostProcessor into the Spring container.
[0065] S2: Create or update the update bean in the proxy bean mapping;
[0066] The step S2 includes the following sub-steps:
[0067] S21: receiving source code;
[0068] The source of the source code includes text code / code file uploaded by the user, text code stored in the database, and text code / code file stored in other storage systems. When receiving the source code, any one of the text code / code file uploaded by the user, the text code / code file stored in the database, and the text code / code file stored in other storage systems is received;
[0069] S22: Create a custom Java compiler, load the source code, and compile the source code into bytecode;
[0070] S23: Create a custom class loader, load the bytecode, load the bytecode into the JVM virtual machine, and generate a Class object.
[0071] S24: Create a custom Spring container;
[0072] S25: Set the parent container of the custom Spring container to the container in the current environment.
[0073] The container in the current environment is the container created when the application is started;
[0074] S26: Get the Bean factory of the custom container;
[0075] S27: Pass in the Class object, execute the create Bean method of the Bean factory, and get the updated Bean;
[0076] S28: Close the custom Spring container.
[0077] S29: Get the Class name according to the Class object;
[0078] S210: Get the value of the name of the Class key from the proxy Bean mapping;
[0079] S211: Replace the update bean in the value with the created update bean.
[0080] S3: Delete the update bean of the specified key in the proxy bean mapping;
[0081] The step S3 includes the following sub-steps:
[0082] S31: Receive Class name;
[0083] S32: According to the Class name, determine whether there is a value with the key of the Class name in the proxy Bean mapping;
[0084] If it does not exist, an error message indicating that the Class name does not exist is returned;
[0085] If it exists, execute S33;
[0086] S33: Replace the update bean in value with null value.
[0087] The patent of this invention is based on a custom Spring container to achieve the creation of updated beans, solve the AOP and IoC problems that cannot be handled by traditional methods of creating beans, and achieve non-invasive hot code updates based on dynamic proxies, thereby solving the problem of hot updates of beans in Spring. Developers can update the code without restarting the application, and the dynamic proxy method is non-invasive to the code, saving a lot of time, and developers can use hot updates to solve the problem that the development environment cannot simulate the production environment test. Developers can update at any time as needed, roll back at any time, switch code versions at any time, and delete updates. Users are unaware. The software enhances the application in the form of dynamic proxy without any impact, and the software can be removed at any time without causing any impact on the application.
[0088] The above are only preferred specific implementation methods of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can make equivalent replacements or changes according to the technical solution and invention concept of the present invention within the technical scope disclosed by the present invention, which should be covered by the protection scope of the present invention.
Claims
1. A method for hot updating Bean in Spring, characterized in that: The steps include: S1: Create a custom hot update Bean post-processor and inject it into the Spring container; S2: Create or update the update bean in the proxy bean mapping; S3: Deletes the update bean for the specified key in the proxy bean map.
2. A method for hot updating Bean in Spring according to claim 1, characterized in that: The step S1 includes the following sub-steps: S11: Create a custom hot swap bean postprocessor HotSwapBeanPostProcessor to implement the Spring extension point BeanPostProcessor interface and override the postProcessBeforeInitialization method. The execution process of this method is as follows: M11: Read configuration information and obtain the scan path; M12: Determine whether the original bean belongs to the scan path; If it does not belong to the scan path, the original bean is returned; If it belongs to the scan path, create a dynamic proxy bean of the original bean; The dynamic proxy Bean is used to intercept each method of the original Bean; The process of the dynamic proxy bean intercepting each method of the original bean includes the following steps: P1: According to the Class name of the original Bean object, query and update the Bean from the global variable proxy Bean mapping; P2: If the update bean is not empty, execute the update bean method; P3: If the updated bean is empty, the method of the original bean is executed; M13: Store the key as the class name of the original bean, the value as the original bean object and the null value in the global variable proxy bean mapping of the data type Map; The null value is a placeholder for updating the Bean; M14: Returns the dynamic proxy bean; S12: Use the @Compoment annotation in the configuration class to inject the HotSwapBeanPostProcessor into the Spring container.
3. A method for hot updating Bean in Spring according to claim 2, characterized in that: The process of the dynamic proxy bean in step M12 intercepting each method of the original bean includes the following steps: P1: According to the Class name of the original Bean object, query and update the Bean from the global variable proxy Bean mapping; P2: If the update bean is not empty, execute the update bean method; P3: If the updated bean is null, the method of the original bean is executed.
4. A method for hot updating Bean in Spring according to claim 1, characterized in that: The step S2 includes the following sub-steps: S21: receiving source code; The source of the source code includes text code / code file uploaded by the user, text code stored in the database, and text code / code file stored in other storage systems. When receiving the source code, any one of the text code / code file uploaded by the user, the text code / code file stored in the database, and the text code / code file stored in other storage systems is received; S22: Create a custom Java compiler, load the source code, and compile the source code into bytecode; S23: Create a custom class loader, load the bytecode, load the bytecode into the JVM virtual machine, and generate a Class object. S24: Create a custom Spring container; S25: Set the parent container of the custom Spring container to the container in the current environment. The container in the current environment is the container created when the application is started; S26: Get the Bean factory of the custom container; S27: Pass in the Class object, execute the create Bean method of the Bean factory, and get the updated Bean; S28: Close the custom Spring container. S29: Get the Class name according to the Class object; S210: Get the value of the name of the Class key from the proxy Bean mapping; S211: Replace the update bean in the value with the created update bean.
5. A method for hot updating Bean in Spring according to claim 1, characterized in that: The step S3 includes the following sub-steps: S31: Receive Class name; S32: According to the Class name, determine whether there is a value with the key of the Class name in the proxy Bean mapping; If it does not exist, an error message indicating that the Class name does not exist is returned; If it exists, execute S33; S33: Replace the update bean in value with null value.
