Method, device, electronic device and medium for dynamically modifying annotation information
The method allows for dynamic annotation modification in SpringBoot projects by utilizing annotation modification starters and configuration classes to replace annotation information, addressing the lack of dynamic modification support in Spring frameworks and reducing code redundancy.
Patent Information
- Application Number
- CN202011584169.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-28
- Publication Date
- 2025-03-25
- Estimated Expiration
- 2040-12-28
AI Technical Summary
Spring frameworks do not support dynamic modification of annotation values within projects, leading to inefficiencies and code redundancy in enterprise application development.
A method and apparatus for dynamically modifying annotation information in SpringBoot projects by detecting annotation modification markers, loading corresponding annotation modification starters, and using configuration classes to replace target annotation information through bytecode manipulation during the initialization phase.
Enables dynamic annotation modification without writing non-business-related code, reducing project code redundancy and providing a one-stop solution for annotation updates across various Spring starters.
Smart Images

Figure CN112698867B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence technology, and in particular to a method, device, electronic device and storage medium for dynamically modifying annotation information. Background Art
[0002] Spring is an open source framework created to address the complexity of enterprise application development. It is a lightweight Inversion of Control (IOC) and Aspect Oriented Programming (AOP) container framework. It is also the most popular open source framework in the Java development field, such as developing Java web projects.
[0003] When the project starts, the configuration file is loaded and the information in the configuration file is read, such as database information and Redis information. This information can be obtained dynamically. Spring has fully realized this project. Whether it is @Value or EL expression language, it provides convenience for Spring-based project development. In Spring project development, it often involves calling various resources, including ordinary files, URLs, configuration files, system environment variables, etc. Spring's EL can be used to inject resources. Spring mainly uses expression language in the parameters of the @value annotation.
[0004] However, during the project development process, you may encounter the problem of "how to dynamically modify the value in the annotation". Obviously, Spring does not support dynamic modification of the value in the annotation. For example, an annotation @Region(value="CHINA"), Spring does not support dynamic modification of the "CHINA" value. Therefore, how to modify the annotation information is a technical problem that needs to be solved in this field. Summary of the invention
[0005] The purpose of this application is to provide a method, device, electronic device and medium for dynamically modifying annotation information, so as to realize dynamic modification of annotation information, while avoiding writing a large amount of business-irrelevant code to realize this function, which can effectively reduce the redundancy of project code.
[0006] The first aspect of the present application provides a method for dynamically modifying annotation information, comprising:
[0007] When a project file based on SpringBoot is started, it is detected whether there is an annotation modification identifier in the project file; the annotation modification identifier is used to identify the annotation modification starter that needs to be loaded;
[0008] When the annotation modification identifier is detected, when the Spring starter is loaded, the annotation modification starter corresponding to the annotation modification identifier is preferentially loaded from the warehouse to modify the target annotation information based on the annotation modification starter, and the target annotation information is the annotation information in the configuration file loaded when the project is started.
[0009] In some implementations of the present application, the process of modifying target annotation information based on the annotation modification initiator is as follows:
[0010] Extract target annotation information from a configuration file of the project;
[0011] Inject a configuration class into the annotation modification enabler;
[0012] The target annotation information is modified using the configuration class.
[0013] In some implementations of the present application, using the configuration class to modify the target annotation information includes:
[0014] Call back the afterProperiesSet function during the configuration class initialization phase;
[0015] The afterProperiesSet function is used to complete the bytecode replacement of the target annotation information.
[0016] In some implementations of the present application, before the SpringBoot-based project file is started, the method further includes:
[0017] Predefine a Spring starter according to the annotation information to be modified to generate the annotation modification starter;
[0018] Publish the annotation modification enabler to a repository.
[0019] A second aspect of the present application provides a device for dynamically modifying annotation information, comprising:
[0020] A detection module, used to detect whether there is an annotation modification identifier in a project file based on SpringBoot when the project file is started; the annotation modification identifier is used to identify the annotation modification starter that needs to be loaded;
[0021] The modification module is used to, when the annotation modification identifier is detected, preferentially load the annotation modification launcher corresponding to the annotation modification identifier from the warehouse when loading the Spring launcher, so as to modify the target annotation information based on the annotation modification launcher, wherein the target annotation information is the annotation information in the configuration file loaded when the project is started.
[0022] In some implementation modes of the present application, the modification module is specifically used to:
[0023] Extract target annotation information from a configuration file of the project;
[0024] Inject a configuration class into the annotation modification enabler;
[0025] The target annotation information is modified using the configuration class.
[0026] In some implementation modes of the present application, the modification module is specifically used to:
[0027] Call back the afterProperiesSet function during the configuration class initialization phase;
[0028] The afterProperiesSet function is used to complete the bytecode replacement of the target annotation information.
[0029] In some implementations of the present application, the detection module is also used to predefine a Spring starter according to the annotation information to be modified before the SpringBoot-based project file is started to generate the annotation modification starter; and publish the annotation modification starter to a warehouse.
[0030] The third aspect of the present application provides an electronic device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the method described in the first aspect of the present application.
[0031] A fourth aspect of the present application provides a computer-readable storage medium having computer-readable instructions stored thereon, wherein the computer-readable instructions can be executed by a processor to implement the method described in the first aspect of the present application.
[0032] Compared with the prior art, the method, device, electronic device and storage medium for dynamically modifying annotation information provided by the present application detect whether there is an annotation modification identifier in the project file when a project file based on SpringBoot is started; the annotation modification identifier is used to identify the annotation modification launcher that needs to be loaded; when the annotation modification identifier is detected, when the Spring launcher is loaded, the annotation modification launcher corresponding to the annotation modification identifier is preferentially loaded from the warehouse to modify the target annotation information based on the annotation modification launcher, and the target annotation information is the annotation information in the configuration file loaded when the project is started. This solution realizes the dynamic modification of annotation information and avoids writing a large amount of code that is not related to the business in order to realize this function, which can effectively reduce the redundancy of project code. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present application. Also, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:
[0034] Figure 1 The Springboot project startup flow chart is shown;
[0035] Figure 2 A flowchart of a method for dynamically modifying annotation information provided by some embodiments of the present application is shown;
[0036] Figure 3 A flowchart of modifying target annotation information based on an annotation modification initiator provided by some embodiments of the present application is shown;
[0037] Figure 4 A schematic diagram showing a device for dynamically modifying annotation information provided by some embodiments of the present application is shown;
[0038] Figure 5 A schematic diagram of an electronic device provided by some embodiments of the present application is shown;
[0039] Figure 6 A schematic diagram of a computer-readable storage medium provided in some embodiments of the present application is shown. DETAILED DESCRIPTION
[0040] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments described herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.
[0041] It should be noted that, unless otherwise specified, the technical terms or scientific terms used in this application should have the common meanings understood by technicians in the field to which this application belongs.
[0042] In addition, the terms "first" and "second" etc. are used to distinguish different objects, rather than to describe a specific order. 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 limited to the listed steps or units, but may optionally include steps or units that are not listed, or may optionally include other steps or units that are inherent to these processes, methods, products or devices.
[0043] For the modification of annotation information, most of the current solutions are to use reflection to modify the bytecode information. However, there is a problem: there is no definite solution as to which link to modify.
[0044] But it is certain that the modification must be completed before the spring starter (springstarter) is started, because some starters (such as: @RedisHash of redis-data) will extract the annotation information and load it into memory for subsequent use. It is meaningless to modify the bytecode after Redisstarter is started. There are two ways to modify it. See Figure 1 The Springboot project startup flowchart shown.
[0045] Solution 1:
[0046] Each SpringBoot program has a main entry, that is, the main method, which calls SpringApplication.run() to start the entire SpringBoot program.
[0047] After the project source code is compiled and before starting Springboot, you can dynamically modify the bytecode file of the annotation information in the first line of code of the Main method. After the modification is completed, read the spring configuration file and call the spring tool class to load and parse.
[0048] The disadvantages of Solution 1 are: it cannot be completed in one stop, no solution is formed, and when using springtest, the first line of code cannot be run, and the testing process needs to be adapted.
[0049] Option 2:
[0050] Before starting springstarter, in order to successfully solve the springtest problem, you can customize the Processor and set a higher priority before ConfigurationClassPostProcessor and before the automatically configured beans are registered with the beanFactory. So far, the problem has been solved, but one-stop completion is still a problem, and doing so makes the project code bloated.
[0051] In view of this, the embodiments of the present application will provide a method and device for dynamically modifying annotation information, and an electronic device and storage medium based on the method. In the scheme of the embodiments of the present application, a Spring starter is predefined according to the annotation information to be modified to generate an annotation modification starter; the annotation modification starter is published to the warehouse. An annotation modification identifier corresponding to the annotation modification starter is set in the project file. When the project file based on SpringBoot is started, the annotation modification identifier is detected. When loading multiple Spring starters, the annotation modification starter corresponding to the annotation modification identifier is loaded first to modify the target annotation information based on the annotation modification starter. This scheme realizes the dynamic modification of annotation information, while avoiding writing a large amount of business-irrelevant code to realize this function, which can effectively reduce the redundancy of project code.
[0052] To further illustrate the solution of the embodiment of the present application, the following description will be made in conjunction with the accompanying drawings. It is understandable that in the following embodiments, the same or corresponding contents can be referenced to each other, and for the sake of simplicity of description, no further description will be given later.
[0053] Please refer to Figure 2 , which shows a flowchart of a method for dynamically modifying annotation information provided by some embodiments of the present application, and the implementation subject of the method can be a dynamic modification device for annotation information. As shown in the figure, the method for dynamically modifying annotation information can include the following steps:
[0054] Step S101: when a project file based on SpringBoot is started, it is detected whether there is an annotation modification identifier in the project file; the annotation modification identifier is used to identify the annotation modification starter that needs to be loaded.
[0055] Specifically, before starting the project file based on SpringBoot in step S101, the method further includes the following steps: predefine the Spring starter according to the annotation information to be modified to generate the annotation modified starter; publish the annotation modified starter to the warehouse. In other words, you can customize a starter, publish it to a warehouse (such as a Maven warehouse), and then customize an annotation @Enable* to enable this function. In this way, all functions are encapsulated in the POM file and annotations, which simplifies the difficulty of use and forms a one-stop function. POM (Project Object Model): Maven project object model.
[0056] Step S102: When the annotation modification identifier is detected, when loading multiple Spring starters, the annotation modification starter corresponding to the annotation modification identifier is loaded first to modify the target annotation information based on the annotation modification starter, and the target annotation information is the annotation information in the configuration file loaded when the project is started.
[0057] The annotation modification flag corresponds to the above-mentioned custom annotation @Enable*. When the project is started, if the annotation modification flag is detected, it means that the user needs to modify the annotation information before SpringBoot starts various built-in Springstarters.
[0058] At this time, because it has the highest priority, when loading the Spring starter, the annotation modification starter corresponding to the annotation modification identifier is loaded first to modify the target annotation information based on the annotation modification starter.
[0059] Specifically, Figure 3 As shown, the process of modifying the target annotation information based on the annotation modification initiator is as follows:
[0060] Step S201: extracting target annotation information in the configuration file of the project;
[0061] Step S202: injecting a configuration class into the annotation modification initiator;
[0062] Step S203: Use the configuration class to modify the target annotation information.
[0063] Specifically, in step S203, modifying the target annotation information using the configuration class specifically includes:
[0064] Call back the afterProperiesSet function during the configuration class initialization phase; use the afterProperiesSet function to complete the bytecode replacement of the target annotation information.
[0065] Compared with the prior art, the method for dynamically modifying annotation information provided in this embodiment detects an annotation modification identifier when a SpringBoot-based project is started; when loading multiple Spring starters, the annotation modification starter corresponding to the annotation modification identifier is loaded first, so as to modify the target annotation information based on the annotation modification starter, and the target annotation information is the annotation information in the configuration file loaded when the project is started. This solution realizes the dynamic modification of annotation information and avoids writing a large amount of code unrelated to the business to realize this function, which can effectively reduce the redundancy of project code.
[0066] In the above-mentioned embodiment, a method for dynamically modifying annotation information is provided. Correspondingly, the present application also provides a device for dynamically modifying annotation information. Figure 4 , which shows a schematic diagram of a dynamic modification device for annotation information provided by some embodiments of the present application. Since the device embodiment is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment. The device embodiment described below is only illustrative.
[0067] like Figure 4 As shown, the dynamic modification device 10 of annotation information may include:
[0068] The detection module 101 is used to detect whether there is an annotation modification identifier in the project file when the project file based on SpringBoot is started; the annotation modification identifier is used to identify the annotation modification starter that needs to be loaded;
[0069] The modification module 102 is used to, when the annotation modification identifier is detected, preferentially load the annotation modification launcher corresponding to the annotation modification identifier when loading the Spring launcher, so as to modify the target annotation information based on the annotation modification launcher, where the target annotation information is the annotation information in the configuration file loaded when the project is started.
[0070] According to some implementations of the present application, the modification module 102 is specifically used to:
[0071] Extract target annotation information from the project's configuration file;
[0072] Inject a configuration class into the annotation modification enabler;
[0073] Use the configuration class to modify the target annotation information.
[0074] According to some implementations of the present application, the modification module 102 is specifically used to:
[0075] Call back the afterProperiesSet function during the configuration class initialization phase;
[0076] Use the afterProperiesSet function to complete the bytecode replacement of the target annotation information.
[0077] According to some implementations of the present application, the detection module 101 is also used to predefine a Spring starter according to the annotation information to be modified before starting a SpringBoot-based project file to generate an annotation modification starter; and publish the annotation modification starter to a warehouse.
[0078] The dynamic modification device 10 of annotation information provided in the embodiment of the present application is based on the same inventive concept as the dynamic modification method of annotation information provided in the aforementioned embodiment of the present application and has the same beneficial effects.
[0079] The embodiments of the present application also provide an electronic device corresponding to the dynamic modification method of annotation information provided in the aforementioned embodiments, such as a mobile phone, a laptop computer, a tablet computer, a desktop computer, etc., to execute the aforementioned dynamic modification method of annotation information.
[0080] Please refer to Figure 5 , which shows a schematic diagram of an electronic device provided by some embodiments of the present application. Figure 5 As shown, the electronic device 20 includes: a processor 200, a memory 201, a bus 202 and a communication interface 203, and the processor 200, the communication interface 203 and the memory 201 are connected via the bus 202; the memory 201 stores a computer program that can be run on the processor 200, and when the processor 200 runs the computer program, it executes the dynamic modification method of annotation information provided in any of the aforementioned embodiments of the present application.
[0081] The memory 201 may include a high-speed random access memory (RAM), and may also include a non-volatile memory, such as at least one disk memory. The communication connection between the system network element and at least one other network element is realized through at least one communication interface 203 (which may be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. may be used.
[0082] The bus 202 may be an ISA bus, a PCI bus, or an EISA bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. The memory 201 is used to store programs, and the processor 200 executes the programs after receiving the execution instruction. The method for dynamically modifying the annotation information disclosed in any implementation of the embodiment of the present application may be applied to the processor 200, or implemented by the processor 200.
[0083] The processor 200 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit or software instructions in the processor 200. The above processor 200 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a readily available programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present application can be implemented or executed. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc. The steps of the method disclosed in the embodiments of the present application can be directly embodied as a hardware decoding processor to be executed, or the hardware and software modules in the decoding processor can be executed. The software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 201, and the processor 200 reads the information in the memory 201 and completes the steps of the above method in combination with its hardware.
[0084] The electronic device provided in the embodiment of the present application and the method for dynamically modifying annotation information provided in the embodiment of the present application are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented therein.
[0085] The present application also provides a computer-readable storage medium corresponding to the method for dynamically modifying annotation information provided in the above embodiment. Figure 6 The computer-readable storage medium shown is a CD 30 on which a computer program (ie, a program product) is stored. When the computer program is executed by a processor, the method for dynamically modifying annotation information provided by any of the aforementioned embodiments will be executed.
[0086] It should be noted that examples of the computer-readable storage medium may also include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other optical or magnetic storage media, which are not listed here one by one.
[0087] The computer-readable storage medium provided by the above-mentioned embodiment of the present application and the method for dynamically modifying annotation information provided by the embodiment of the present application are based on the same inventive concept and have the same beneficial effects as the method adopted, run or implemented by the application program stored therein.
[0088] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein by equivalents. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application, and they should all be included in the scope of the claims and specification of the present application.
Claims
1. A method for dynamically modifying annotation information, characterized in that: include: When a SpringBoot-based project file is started, detect whether there is an annotation modification identifier in the project file; The annotation modification identifier is used to identify the annotation modification initiator that needs to be loaded; When the annotation modification identifier is detected, when the Spring starter is loaded, the annotation modification starter corresponding to the annotation modification identifier is preferentially loaded from the warehouse to modify the target annotation information based on the annotation modification starter, and the target annotation information is the annotation information in the configuration file loaded when the project is started; The process of modifying the target annotation information based on the annotation modification initiator is as follows: Extract target annotation information from the configuration file of the project; inject a configuration class into the annotation modification initiator; call back the afterProperiesSet function during the initialization phase of the configuration class; and use the afterProperiesSet function to complete the bytecode replacement of the target annotation information.
2. The method according to claim 1, characterized in that Before the SpringBoot-based project file is started, the method further includes: Predefine a Spring starter according to the annotation information to be modified to generate the annotation modification starter; Publish the annotation modification enabler to a repository.
3. A device for dynamically modifying annotation information, characterized in that: include: A detection module is used to detect whether there is an annotation modification identifier in a project file when the project file based on SpringBoot is started; The annotation modification identifier is used to identify the annotation modification initiator that needs to be loaded; A modification module, which is used for, when the annotation modification identifier is detected, preferentially loading the annotation modification starter corresponding to the annotation modification identifier from the warehouse when loading the Spring starter, so as to modify the target annotation information based on the annotation modification starter, wherein the target annotation information is the annotation information in the configuration file loaded when the project is started; The modification module is specifically used for: Extract target annotation information from the configuration file of the project; inject a configuration class into the annotation modification initiator; call back the afterProperiesSet function during the initialization phase of the configuration class; and use the afterProperiesSet function to complete the bytecode replacement of the target annotation information.
4. The device according to claim 3, characterized in that The detection module is also used to predefine the Spring starter according to the annotation information to be modified before the SpringBoot-based project file is started to generate the annotation modification starter; and publish the annotation modification starter to the warehouse.
5. An electronic device comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the method according to any one of claims 1 to 2.
6. A computer-readable storage medium, characterized in that: Computer-readable instructions are stored thereon, and the computer-readable instructions can be executed by a processor to implement the method according to any one of claims 1 to 2.