Spring-based specified class file loading reassembly method and device and medium
By using class loading mechanism and dependency injection technology in Spring system, the updated class files are automatically assembled and reinjected, which solves the problem that the system cannot take effect immediately after the system is updated, and the function can be updated without restarting the service, which improves the stability and flexibility of the system.
Patent Information
- Application Number
- CN202510200280.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-24
- Publication Date
- 2025-06-13
AI Technical Summary
Existing systems cannot take effect immediately after updating files without restarting the service, resulting in the inability to implement update-ready functions, especially in the case of independent function points and emergency update requirements.
Through Spring-based class loading mechanism and dependency injection technology, an uploaded Controller is developed, the importClassAndLoad method is called, the automatic assembly component is combined with the class loader to parse the class file, and the original instance is destroyed using Spring's BeanFactory, reinjected into the Spring container, traversing all Spring instances under the component-scan package, and reloading the class annotated with @Autowired@Resource.
It realizes the rapid launch of new functions without restarting the system, achieving the purpose of updating and using it, improving the stability and flexibility of the system, and reducing the troubleshooting time between implementing personnel and R&D personnel.
Smart Images

Figure CN120144202A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of Web applications, in particular to a method, device and medium for reassembling a specified class file based on Spring loading. Background Art
[0002] The current system puts the generated war package into tomcat for startup and operation. Every time there is a new independent demand and after the R&D completes the function development, the project implementation personnel update the files to the server. However, the new files cannot take effect immediately without restarting the service. It is necessary to wait until after get off work in the evening to update the files and restart the service for functional testing. For independent functional points and urgent update needs, it is impossible to achieve update and application.
[0003] Therefore, how to replace files and quickly bring them online without restarting the system and affecting the normal use of business personnel, so as to achieve the goal of updating and using them immediately, is a technical problem that needs to be solved urgently. Summary of the invention
[0004] The technical task of the present invention is to provide a method, device and medium for reassembling specified class files based on Spring loading, so as to solve the problem of how to replace files and quickly put them online without restarting the system to affect the normal use of business personnel, so as to achieve the purpose of updating and using them.
[0005] The technical task of the present invention is achieved in the following way: a method for reassembling a specified class file based on Spring loading. The method is based on Java's class loading mechanism and Spring dependency injection technology. An uploaded Controller is developed, and the importClassAndLoad method is called together with the class file stream and update path that need to be updated. The automatic assembly component is combined with the class loader to automatically parse the class file. At the same time, the original instance is destroyed by using Spring's BeanFactory, and the new instance is re-injected into the Spring container. All Spring instances under the corresponding package of component-scan are traversed, and all classes annotated with the corresponding class @Autowired@Resource will be reloaded. The entire automatic assembly process is completed, and there is no need to restart the service to test the functions provided by the new file.
[0006] Preferably, the method is as follows:
[0007] In the DynamicLoadClassServiceImpl class, receive the file stream to be updated, convert it into an instance, and reinject it into the Spring container and reload it into all objects that depend on the DynamicLoadClassServiceImpl class through the @Autowired and @Resource annotations;
[0008] Load the uploaded file as a class file stream through the ClassLoadUtil utility class;
[0009] Implement the Spring BeanFactoryAware interface through the SpringContextUtil utility class. After Spring is initialized, perform dependency injection and destruction operations on the instance through the BeanFactory object provided by Spring;
[0010] Load the instantiated object into the class with the @Autowired annotation in the BeanDefinitionRegistry.
[0011] Preferably, the Java class loading mechanism refers to the process of reading the bytecode file of a class into memory and creating a java.lang.Class object for the bytecode file of the class; the class loader in Java is used to load the bytecode of the class into the Java virtual machine;
[0012] Among them, the class loaders in Java include the Bootstrap Class Loader, Extension Class Loader, System Class Loader or Application Class Loader, and user-defined class loaders;
[0013] The Bootstrap Class Loader is used to load the core class libraries of Java;
[0014] The Extension Class Loader is used to load the extension class libraries of Java;
[0015] The System Class Loader or Application Class Loader is used to load the class libraries on the user classpath;
[0016] The user-defined class loader is used for the user to create a custom class loader by inheriting the java.lang.ClassLoader class.
[0017] Preferably, Spring's Dependency Injection (DI) is a technique that dynamically injects dependency relationships into objects during program execution, which helps reduce the coupling between programs and improve the testability and maintainability of programs;
[0018] The ways of Spring dependency injection include Constructor Injection, Setter Injection, Interface Injection, and Field Injection.
[0019] Preferably, this method injects the name of the Bean object through the BeanNameAware#setBeanName method and accurately obtains the Bean by the name of the Bean object;
[0020] This method injects the class loader of the Bean through the BeanClassLoaderAware#setBeanClassLoader method and performs related operations through the class loader of the Bean;
[0021] This method obtains the BeanFactory object through the BeanFactoryAware#setBeanFactory method and obtains the Bean instance in the Spring container through the BeanFactory object;
[0022] This method injects the Bean to obtain the Environment object through the EnvironmentAware#setEnvironment method, configures properties and environment variables through the Environment object, and then obtains the current environment configuration. Specifically: obtain the property values in the configuration file for the development environment, test environment, or production environment.
[0023] Preferably, this method obtains the resource loader through the ResourceLoaderAware#setResourceLoader method and loads the configuration file and static resources through the resource loader; among them, the static resources include pictures;
[0024] This method obtains the event publisher through the ApplicationEventPublisherAware#setApplicationEventPublisher method and implements custom events and listens for Spring container events through the event publisher.
[0025] Preferably, this method obtains the message internationalization object through the MessageSourceAware#setMessageSource method, and obtains the internationalization information through the message internationalization object.
[0026] More preferably, this method injects the Spring context object through the ApplicationContextAware#setApplicationContext method, and obtains the ServletContext, internationalization information, and Scheduler timed task in the Web project through the Spring context object;
[0027] This method obtains the ServletConfig object through the ServletContextAware#setServletContext method, and obtains the name of the Servlet and the initialization parameters of the Servlet through the ServletConfig object.
[0028] An electronic device includes: a memory and at least one processor;
[0029] Wherein, a computer program is stored on the memory;
[0030] The at least one processor executes the computer program stored in the memory, so that the at least one processor executes the method for reassembling a specified class file based on Spring as described above.
[0031] A computer-readable storage medium stores a computer program, and the computer program can be executed by a processor to implement the method for reassembling a specified class file based on Spring as described above.
[0032] The method, device, and medium for reassembling a specified class file based on Spring of the present invention have the following advantages:
[0033] (1) The present invention uses the automatic assembly function to upload the class file, which will automatically reload the file and inject it into the Spring container for service calls. Without restarting the system and affecting the normal use of business personnel, the new function can be quickly launched by replacing the file, achieving the purpose of being available immediately after the update;
[0034] (2) During the system usage, the project implementers do not need to consider the situation that restarting the service after updating the files will affect the stable operation of the system. For files that need to be urgently updated temporarily, the files are uploaded to the server through the automatic assembly technology, the files are automatically updated and automatically injected into the Spring container by dependency injection, realizing that the partial update of the functional files does not affect the normal use of other functions and increasing the stability of the system;
[0035] (3) While the present invention performs automatic assembly, it also checks whether other class files are missing, reducing the time for implementers and R & D personnel to troubleshoot errors;
[0036] (4) The present invention introduces an automatic assembly component. By uploading the class file through the front end and specifying the relative path of the file, the automatic dependency injection of the instance can be completed after the file stream is passed into the automatic assembly component, and the operation is simple and easy to implement;
[0037] (5) The automatic update class assembly of the present invention based on the class loader and Spring dependency injection reads the class file to recreate the class instance without restarting the service, and updates and injects the instance object into the Spring container, having good promotion and application value. Description of the Drawings
[0038] The present invention will be further described below with reference to the accompanying drawings.
[0039] Appendix Figure 1 It is a schematic flow chart of the method for reassembling the specified class file based on Spring loading. Detailed Embodiments
[0040] The method, device and medium for reassembling the specified class file based on Spring loading of the present invention will be described in detail below with reference to the accompanying drawings of the specification and specific embodiments.
[0041] Embodiment 1:
[0042] As shown in the appendix Figure 1As shown in the figure, this embodiment provides a method for reassembling by loading a specified class file based on Spring. This method is based on the Java class loading mechanism and Spring dependency injection technology. A Controller for uploading is developed, and the class file stream to be updated and the update path are called together in the importClassAndLoad method. The automatic assembly component combines with the class loader to automatically parse the class file. At the same time, the original instance is destroyed using Spring's BeanFactory, and the new instance is re-injected into the Spring container. All Spring instances under the corresponding package of component-scan are traversed, and all classes using the @Autowired and @Resource annotations will be reloaded. The entire automatic assembly process is completed, and the function provided by the new file can be tested without restarting the service.
[0043] The code writing and encapsulation part of the backend in this method is as follows:
[0044]
[0045]
[0046]
[0047] The above code is a code snippet of the DynamicLoadClassServiceImpl.java dynamic loading implementation class.
[0048] In the DynamicLoadClassServiceImpl class, the file stream to be updated is received and converted into an example and re-injected into the Spring container and reloaded into all objects that depend on the DynamicLoadClassServiceImpl class through the @Autowired and @Resource annotations;
[0049]
[0050]
[0051] The above code is a code snippet of the ClassLoadUtil.java dynamic class loader
[0052] As can be seen from the above code snippets, the uploaded file is loaded as a class file stream through the ClassLoadUtil tool class;
[0053]
[0054]
[0055] The above code is a utility class for re-injecting the SpringContextUtil.java class into the Spring container.
[0056] By implementing the BeanFactoryAware interface of Spring through the SpringContextUtil utility class, after Spring is initialized, operations such as dependency injection and destruction of instances are performed on the instances through the BeanFactory object provided by Spring;
[0057]
[0058]
[0059] The above code is a code snippet of the BeanDefinitionRegistry.java Bean definition registry post-processor.
[0060] In the BeanDefinitionRegistry, the instantiated objects are loaded into the classes annotated with @Autowired.
[0061] The Java class loading mechanism in this embodiment refers to the process of reading the bytecode file of a class into memory and creating a java.lang.Class object for the bytecode file of the class; the class loader in Java is used to load the bytecode of a class into the Java virtual machine;
[0062] Among them, the class loaders in Java include the Bootstrap Class Loader, Extension Class Loader, System Class Loader or Application Class Loader, and user-defined class loaders;
[0063] The Bootstrap Class Loader is used to load the core class libraries of Java;
[0064] The Extension Class Loader is used to load the extension class libraries of Java;
[0065] The System Class Loader or Application Class Loader is used to load the class libraries on the user classpath;
[0066] The user-defined class loader is used for users to create custom class loaders by inheriting the java.lang.ClassLoader class.
[0067] In this embodiment, the Dependency Injection (DI) of Spring is a technology that dynamically injects dependency relationships into objects during the program running process, which helps to reduce the coupling degree between programs and improve the testability and maintainability of programs.
[0068] The ways of Spring dependency injection include Constructor Injection, Setter Injection, Interface Injection, and Field Injection.
[0069] The main classes and methods used in this embodiment are shown in the following table:
[0070]
[0071] Embodiment 2:
[0072] This embodiment also provides an electronic device, including: a memory and at least one processor;
[0073] Wherein, the memory stores computer execution instructions;
[0074] The at least one processor executes the computer execution instructions stored in the memory, so that the at least one processor executes the method of reassembling by loading a specified class file based on Spring in any embodiment of the present invention.
[0075] The processor can be a Central Processing Unit (CPU), or can also be other general-purpose processors, Digital Signal Processors (DSPs), Application Specific Integrated Circuits (ASICs), Field Programmable Gate Arrays (FPGAs) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The processor can be a microprocessor or the processor can also be any conventional processor, etc.
[0076] The memory can be used to store computer programs and / or modules. The processor realizes various functions of the electronic device by running or executing the computer programs and / or modules stored in the memory and calling the data stored in the memory. The memory mainly includes a program storage area and a data storage area. Among them, the program storage area can store an operating system, application programs required for at least one function, etc.; the data storage area can store data created according to the use of the terminal, etc. In addition, the memory can also include high-speed random access memory, and can also include non-volatile memory, such as a hard disk, memory, plug-in hard disk, Smart Media Card (SMC), Secure Digital (SD) card, flash card, at least one magnetic disk storage period, flash device, or other volatile solid-state storage devices.
[0077] Example 3:
[0078] This embodiment also provides a computer-readable storage medium, in which multiple instructions are stored. The instructions are loaded by a processor to cause the processor to execute the method of reassembling a specified class file based on Spring loading in any embodiment of the present invention. Specifically, a system or device equipped with a storage medium can be provided. On this storage medium, software program codes for implementing the functions of any one of the above embodiments are stored, and the computer (or CPU or MPU) of the system or device reads and executes the program codes stored in the storage medium.
[0079] In this case, the program code read from the storage medium itself can implement the functions of any one of the above embodiments. Therefore, the program code and the storage medium storing the program code constitute a part of the present invention.
[0080] Embodiments of the storage medium for providing program codes include floppy disks, hard disks, magneto-optical disks, optical disks (such as CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RYM, DVD-RW, DVD+RW), magnetic tapes, non-volatile memory cards, and ROMs. Optionally, the program code can be downloaded from a server computer via a communication network.
[0081] In addition, it should be clear that not only can the functions of any one of the above embodiments be realized by executing the program code read by the computer, but also by causing an operating system or the like operating on the computer based on the instructions of the program code to complete part or all of the actual operations.
[0082] In addition, it can be understood that the program code read from the storage medium is written into the memory provided in the expansion board inserted into the computer or into the memory provided in the expansion unit connected to the computer. Subsequently, based on the instructions of the program code, the CPU or the like installed on the expansion board or the expansion unit executes part and all of the actual operations, thereby realizing the functions of any one of the above embodiments.
[0083] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit them. Although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions described in the foregoing embodiments, or perform equivalent replacements on some or all of the technical features. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A reassembly method based on Spring loading of specified class files, characterized in that: This method is based on Java's class loading mechanism and Spring dependency injection technology. An uploaded Controller is developed, and the importClassAndLoad method is called together with the class file stream and update path that need to be updated. The automatic assembly component is combined with the class loader to automatically parse the class file. At the same time, Spring's BeanFactory is used to destroy the original instance, and the new instance is re-injected into the Spring container. All Spring instances under the corresponding package of component-scan are traversed, and all classes annotated with @Autowired@Resource will be reloaded. The entire automatic assembly process is completed, and there is no need to restart the service to test the functions provided by the new file.
2. The reassembly method based on Spring loading of a specified class file according to claim 1, characterized in that: The method is as follows: In the DynamicLoadClassServiceImpl class, receive the file stream to be updated, convert it into an example, re-inject it into the Spring container, and reload it into all objects that depend on the DynamicLoadClassServiceImpl class through the @Autowired and @Resource annotations; Use the ClassLoadUtil tool class to load the uploaded file into a class file stream; Implement Spring's BeanFactoryAware interface through the SpringContextUtil tool class. After Spring is initialized, perform dependency injection and destruction operations on instances through the BeanFactory object provided by Spring. Load the instantiated object into the Autowired annotated class in the BeanDefinitionRegistry.
3. The reassembly method based on Spring loading of a specified class file according to claim 1, characterized in that: The Java class loading mechanism refers to the process of reading the bytecode file of a class into memory and creating a java.lang.Class object for the bytecode file of the class; the class loader in Java is used to load the bytecode of the class into the Java virtual machine; Among them, the class loaders in Java include the startup class loader, the extension class loader, the system class loader or application class loader, and the user-defined class loader; The startup class loader is used to load the Java core class library; The extension class loader is used to load Java extension class libraries; The system class loader or application class loader is used to load class libraries on the user class path; User-defined class loaders are used to create custom class loaders for users by inheriting the java.lang.ClassLoader class.
4. The reassembly method based on Spring loading of a specified class file according to claim 1, characterized in that: Spring's dependency injection is a technique for dynamically injecting dependencies into objects while the program is running; Spring dependency injection methods include constructor injection, setter injection, interface injection, and field injection.
5. The reassembly method based on Spring loading of specified class files according to claim 1 is characterized in that: This method injects the name of the Bean object through the BeanNameAware#setBeanName method, and accurately obtains the Bean through the name of the Bean object; This method injects the Bean's class loader through the BeanClassLoaderAware#setBeanClassLoader method and performs related operations through the Bean's class loader; This method obtains the BeanFactory object through the BeanFactoryAware#setBeanFactory method, and obtains the Bean instance in the Spring container through the BeanFactory object; This method injects the Bean through the EnvironmentAware#setEnvironment method to obtain the Environment object, configures the properties and environment variables through the Environment object, and then obtains the current environment configuration, specifically: the development environment, test environment or production environment obtains the property values in the configuration file.
6. The reassembly method based on Spring loading of a specified class file according to claim 1, characterized in that: This method obtains the resource loader through the ResourceLoaderAware#setResourceLoader method, and loads configuration files and static resources through the resource loader; wherein the static resources include images; This method obtains the event publisher through the ApplicationEventPublisherAware#setApplicationEventPublisher method, implements custom events through the event publisher, and listens to Spring container events.
7. The reassembly method based on Spring loading of a specified class file according to claim 1, characterized in that: This method obtains the message internationalization object through the MessageSourceAware#setMessageSource method, and obtains the internationalization information through the message internationalization object.
8. The reassembly method based on Spring loading of a specified class file according to any one of claims 1 to 7, characterized in that: This method injects the Spring context object through the ApplicationContextAware#setApplicationContext method, and obtains the ServletContext, internationalization information, and Scheduler timed tasks in the Web project through the Spring context object; This method obtains the ServletConfig object through the ServletContextAware#setServletContext method, and obtains the Servlet name and Servlet initialization parameters through the ServletConfig object.
9. An electronic device, characterized in that: include: memory and at least one processor; Wherein, the memory stores a computer program; The at least one processor executes the computer program stored in the memory, so that the at least one processor performs the Spring-based loading of a specified class file reassembly method as described in any one of claims 1 to 8.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, which can be executed by a processor to implement the Spring-based loading of a specified class file reassembly method according to any one of claims 1 to 8.