Method for dynamic generation of controllers based on custom interfaces and annotations
By dynamically generating controller methods through custom interfaces and annotations, the cumbersome controller generation problem in traditional Spring MVC development is solved, achieving concise and efficient automated controller generation and maintenance, which is suitable for rapid development of microservice architectures.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- JIANGSU FINSTONE TECH CO LTD
- Filing Date
- 2022-08-02
- Publication Date
- 2026-04-21
AI Technical Summary
In traditional Spring MVC development, the controller generation method is cumbersome and complex, the code is inconvenient to maintain, and the controller layer has a heavy workload and is difficult to troubleshoot in the case of front-end and back-end separation.
We adopt a dynamic controller generation method based on custom interfaces and annotations. By using custom interface annotations and Byte Buddy dynamic class generation technology, we can automatically generate controller classes, reduce manual code writing, and implement controller functionality using custom annotations and proxy classes.
It simplifies the controller generation process, reduces the amount of code, lowers the coding error rate, improves development efficiency and ease of later maintenance, and is suitable for rapid and efficient development of microservice architecture.
Smart Images

Figure CN117539470B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of controller communication technology, specifically a method for dynamically generating controllers based on custom interfaces and annotations. Background Technology
[0002] In traditional Spring MVC development, developers must create controllers, with the development steps generally as follows: first, configure the mapping path; then, match the path to the method in the controller; next, call the Service layer within the method; and finally, redirect to the page (View) or output the return value. This process is cumbersome and troubleshooting is difficult. For larger projects, maintaining the controller code is also inconvenient. In a front-end / back-end separation architecture, the controller layer no longer needs to handle page redirection, making the traditional approach overly cumbersome. Based on these issues, we consider dynamically generating automatic code for the controller.
[0003] The existing controller generation method has a relatively cumbersome and complex workflow, making troubleshooting difficult and hindering later maintenance. Summary of the Invention
[0004] The purpose of this invention is to provide a method for dynamically generating controllers based on custom interfaces and annotations, in order to solve the problems mentioned in the background art, such as the relatively cumbersome and complex workflow of existing controller generation methods, the difficulty in troubleshooting, and the inconvenience of later maintenance.
[0005] To this end, the present invention provides a method for dynamically generating controllers based on custom interfaces and annotations, comprising the following steps:
[0006] S1. Equipment Operation and Inspection: Check whether the dynamic identification equipment, communication equipment, interface modules, controllers and input devices in the system setup have established reliable connections;
[0007] S2. Interface Definition: Define the service interface, add the FusionService annotation to the defined service interface, and set the access path;
[0008] S3. Interface method annotations: Add the Fusion Operation annotation to the traditional interface method and set the access path, method description and data request method;
[0009] S4. Spring Startup: Create a new class to implement the service interface, start Spring, and find the Fusion Rest Bean PostProcessor in the Spring startup extension class that implements the Bean definition registry post processor;
[0010] S5. Search: Scan the Classpath, scan all type interface definitions under the Fusion Rest Bean Post Processor path, and search for interface definitions with the Fusion Service annotation in the interface type.
[0011] S6. Invocation: Generate a dynamic class name by concatenating the Controller with the interface name, generate the dynamic class using the name method of Byte Buddy, specify its parent class using the subclass method, create a dynamic implementation class interface based on the matched interface, and add the Spring MVC Request Mapping class annotation to the dynamic class.
[0012] S7. Inject Controller: Add a method call Proxy Handler, use the make method to generate bytecode, use the load method to load the generated bytecode into the JVM, and use the register Singleton method to inject it into the Spring container.
[0013] Preferably, in step S2, the interface corresponding to the mapped path is matched according to the URL request, and the corresponding Service method is automatically executed. The generated controller class is simply a class with Controller and Request Mapping annotations, the mapped method is annotated with Request Mapping and Response Body, the parameters are annotated with RequestBody, and the parameters are annotated with Content-Type=application / json as needed.
[0014] Preferably, in step S1, the dynamic identification device is used to scan and record the access frequency of the access devices in the interface module and identify the types of access frequencies, wherein the types of access frequencies include high-frequency access signals, medium-frequency access signals and low-frequency access signals.
[0015] Preferably, in step S6, a method proxy Handler is added to the dynamic class.
[0016] Preferably, the annotation definition is a two-dimensional array, where the first dimension represents the nth parameter and the second dimension represents the nth annotation of that parameter.
[0017] Preferably, the method is applied to a microservice architecture that includes multiple service units, where the server belongs to the microservice architecture.
[0018] Preferably, in step S7, the controller injection method can also be as follows: inject the controller using XML custom tags. In the qualified file of springmvc.xsd, the Context Namespace Handler inherits the Namespace Handler Support class to parse custom tags, and the Controller Bean Definition Parser implements the Bean Definition Parser interface to inject Beans. The dynamically generated Controller class is set as a Bean by root.set Bean Class(Controller Register Factory.regist(c)) and then injected into the Spring container.
[0019] Preferably, in step S7, the controller injection method can also be: obtaining a reference to the Application Context based on Application ContextAware, then dynamically obtaining the object based on the Application Context, and then directly dynamically obtaining the required Bean in the code, and then injecting the Bean into the Spring container.
[0020] The method for dynamically generating controllers based on custom interfaces and annotations proposed in this invention has the following advantages:
[0021] 1. This controller generation method directly adds custom annotations to the service interface class and its methods. The annotations generate proxy classes to realize the role of the controller. It is automatically generated in a dynamic way, so the controller does not need to be written manually. This can greatly reduce the amount of code to be written and reduce the chance of coding errors.
[0022] 2. Integrate the interface definition with the controller configuration to reduce the number of controller class definitions, reduce development configuration steps, improve development efficiency, simplify the development configuration process, make it more concise, and facilitate later troubleshooting and maintenance;
[0023] 3. Based on Byte Man dynamic class generation technology, it is more flexible in use and has certain performance advantages compared to JDK dynamic proxies and cglib. With framework-level processing, development follows the specifications and requires no additional processing, enabling fast and efficient server development. Attached Figure Description
[0024] Figure 1 This is a system flowchart of the present invention. Detailed Implementation
[0025] The technical solution of the present invention will now be described in detail through specific embodiments.
[0026] Example 1:
[0027] Please see Figure 1 This invention provides a method for dynamically generating controllers based on custom interfaces and annotations, comprising the following steps:
[0028] S1. Equipment Operation and Inspection: Check whether the dynamic identification device, communication device, interface module, controller and input device in the system construction equipment have established a reliable connection. The dynamic identification device is used to scan and record the access frequency of the access device in the interface module and identify the type of access frequency. The access frequency types include high frequency access signal, medium frequency access signal and low frequency access signal.
[0029] S2. Interface Definition: Define the service interface, add the FusionService annotation to the defined service interface, and set the access path. Based on the URL request, match the interface corresponding to the mapping path, automatically execute the corresponding Service method, and generate the controller class. Simply put, the generated class has the Controller and Request Mapping annotations, the mapped methods have the Request Mapping and Response Body annotations, the parameters use the RequestBody annotation method, and the parameters are annotated with Content-Type=application / json as needed.
[0030] S3. Interface method annotations: Add the Fusion Operation annotation to the traditional interface method and set the access path, method description and data request method;
[0031] S4. Spring Startup: Create a new class to implement the service interface, start Spring, find the Fusion Rest Bean PostProcessor in the Spring startup extension class that implements the Bean definition registry post processor, and add a method proxy handler to the dynamic class;
[0032] S5. Search: Scan the Classpath, scan all type interface definitions under the Fusion Rest Bean Post Processor path, and search for interface definitions with the Fusion Service annotation in the interface type.
[0033] S6. Invocation: Generate a dynamic class name by concatenating the Controller with the interface name, generate the dynamic class using the name method of Byte Buddy, specify its parent class using the subclass method, create a dynamic implementation class interface based on the matched interface, and add the Spring MVC Request Mapping class annotation to the dynamic class.
[0034] S7. Inject Controller: Add a method call Proxy Handler, use the make method to generate bytecode, use the load method to load the generated bytecode into the JVM, and use the register Singleton method to inject it into the Spring container.
[0035] An annotation definition is a two-dimensional array. The first dimension indicates which parameter it is, and the second dimension indicates which annotation it is. The method is applied in a microservice architecture containing multiple service units, where the server belongs to a microservice architecture.
[0036] In this embodiment: custom annotations are added directly to the service interface class and its methods. The annotations generate proxy classes to implement the controller function. The controller is automatically generated dynamically, so the controller does not need to be written manually. This can greatly reduce the amount of code to write, reduce the probability of coding errors, integrate the interface definition and controller configuration, reduce the number of controller class definitions, reduce development configuration steps, improve development efficiency, simplify the development configuration process, and make it more concise.
[0037] Example 2:
[0038] Please see Figure 1 This invention provides a method for dynamically generating controllers based on custom interfaces and annotations, comprising the following steps:
[0039] S1. Equipment Operation and Inspection: Check whether the dynamic identification device, communication device, interface module, controller and input device in the system construction equipment have established a reliable connection. The dynamic identification device is used to scan and record the access frequency of the access device in the interface module and identify the type of access frequency. The access frequency types include high frequency access signal, medium frequency access signal and low frequency access signal.
[0040] S2. Interface Definition: Define the service interface, add the FusionService annotation to the defined service interface, and set the access path. Based on the URL request, match the interface corresponding to the mapping path, automatically execute the corresponding Service method, and generate the controller class. Simply put, the generated class has the Controller and Request Mapping annotations, the mapped methods have the Request Mapping and Response Body annotations, the parameters use the RequestBody annotation method, and the parameters are annotated with Content-Type=application / json as needed.
[0041] S3. Interface method annotations: Add the Fusion Operation annotation to the traditional interface method and set the access path, method description and data request method;
[0042] S4. Spring Startup: Create a new class to implement the service interface, start Spring, find the Fusion Rest Bean PostProcessor in the Spring startup extension class that implements the Bean definition registry post processor, and add a method proxy handler to the dynamic class;
[0043] S5. Search: Scan the Classpath, scan all type interface definitions under the Fusion Rest Bean Post Processor path, and search for interface definitions with the Fusion Service annotation in the interface type.
[0044] S6. Invocation: Generate a dynamic class name by concatenating the Controller with the interface name, generate the dynamic class using the name method of Byte Buddy, specify its parent class using the subclass method, create a dynamic implementation class interface based on the matched interface, and add the Spring MVC Request Mapping class annotation to the dynamic class.
[0045] Another way to inject a controller is to use a custom XML tag. In the springmvc.xsd configuration file, the Context Namespace Handler inherits the Namespace HandlerSupport class to parse custom tags, and the Controller Bean Definition Parser implements the BeanDefinition Parser interface to inject beans. The dynamically generated controller class is set as a bean by root.setBeanClass(ControllerRegisterFactory.regist(c)) and then injected into the Spring container.
[0046] An annotation definition is a two-dimensional array. The first dimension indicates which parameter it is, and the second dimension indicates which annotation it is. The method is applied in a microservice architecture containing multiple service units, where the server belongs to a microservice architecture.
[0047] In this embodiment: Compared with Embodiment 1, this embodiment adopts the same dynamic production method, only replacing the Bean injection method. Based on Byte Man dynamic class generation technology, it is more flexible in use and has certain performance advantages compared with JDK dynamic proxy and cglib. It is a framework-level processing method. Development follows the specifications and requires no additional processing, enabling fast and efficient server development.
[0048] Example 3:
[0049] Please see Figure 1 This invention provides a method for dynamically generating controllers based on custom interfaces and annotations, comprising the following steps:
[0050] S1. Equipment Operation and Inspection: Check whether the dynamic identification device, communication device, interface module, controller and input device in the system construction equipment have established a reliable connection. The dynamic identification device is used to scan and record the access frequency of the access device in the interface module and identify the type of access frequency. The access frequency types include high frequency access signal, medium frequency access signal and low frequency access signal.
[0051] S2. Interface Definition: Define the service interface, add the FusionService annotation to the defined service interface, and set the access path. Based on the URL request, match the interface corresponding to the mapping path, automatically execute the corresponding Service method, and generate the controller class. Simply put, the generated class has the Controller and Request Mapping annotations, the mapped methods have the Request Mapping and Response Body annotations, the parameters use the RequestBody annotation method, and the parameters are annotated with Content-Type=application / json as needed.
[0052] S3. Interface method annotations: Add the Fusion Operation annotation to the traditional interface method and set the access path, method description and data request method;
[0053] S4. Spring Startup: Create a new class to implement the service interface, start Spring, find the Fusion Rest Bean PostProcessor in the Spring startup extension class that implements the Bean definition registry post processor, and add a method proxy handler to the dynamic class;
[0054] S5. Search: Scan the Classpath, scan all type interface definitions under the Fusion Rest Bean Post Processor path, and search for interface definitions with the Fusion Service annotation in the interface type.
[0055] S6. Invocation: Generate a dynamic class name by concatenating the Controller with the interface name, generate the dynamic class using the name method of Byte Buddy, specify its parent class using the subclass method, create a dynamic implementation class interface based on the matched interface, and add the Spring MVC Request Mapping class annotation to the dynamic class.
[0056] In step S7, the controller injection method can also be: obtain a reference to the Application Context based on Application Context Aware, then dynamically obtain the object based on the Application Context, and then directly obtain the required Bean in the code, and then inject the Bean into the Spring container. The annotation definition is a two-dimensional array, the first dimension represents which parameter, and the second dimension represents which annotation of the parameter. The method is applied in a microservice architecture containing multiple service units, and the server belongs to the microservice architecture.
[0057] In this embodiment, compared with Embodiment 1 and Embodiment 2, a method is provided to obtain a reference to the Application Context based on Application ContextAware, then dynamically obtain objects based on the Application Context, and then directly obtain the required Beans in the code and inject them into the Spring container to produce the controller. This method has the advantages of not requiring manual writing, greatly reducing the amount of code to be written, reducing the probability of coding errors, integrating interface definition with controller configuration, reducing one controller class definition, reducing development configuration steps, improving development efficiency, and simplifying the development configuration process.
[0058] The above description is only a preferred embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any equivalent substitutions or modifications made by those skilled in the art within the scope of the technology disclosed in the present invention, based on the technical solution and inventive concept of the present invention, should be covered within the scope of protection of the present invention.
Claims
1. A method for dynamically generating controllers based on custom interfaces and annotations, characterized in that: Includes the following steps: S1. Equipment Operation and Inspection: Check whether the dynamic identification equipment, communication equipment, interface modules, controllers and input devices in the system setup have established reliable connections; S2. Interface Definition: Define the service interface, add the FusionService annotation to the defined service interface, and set the access path; S3. Interface method annotations: Add the Fusion Operation annotation to the traditional interface method and set the access path, method description and data request method; S4. Spring Startup: Create a new class to implement the service interface, start Spring, and find the Fusion Rest Bean PostProcessor in the Spring startup extension class that implements the Bean definition registry post processor; S5. Search: Scan the Classpath, scan all type interface definitions under the Fusion Rest Bean Post Processor path, and search for interface definitions with the Fusion Service annotation in the interface type. S6. Invocation: Generate a dynamic class name by concatenating the Controller with the interface name, generate the dynamic class using the name method of Byte Buddy, specify its parent class using the subclass method, create a dynamic implementation class interface based on the matched interface, and add the Spring MVC Request Mapping class annotation to the dynamic class. S7. Inject Controller: Add a method call Proxy Handler, use the make method to generate bytecode, use the load method to load the generated bytecode into the JVM, and use the register Singleton method to inject it into the Spring container.
2. The method for dynamically generating controllers based on custom interfaces and annotations according to claim 1, characterized in that: In step S2, the interface corresponding to the mapped path is matched according to the URL request, and the corresponding Service method is automatically executed. The generated controller class is simply a class with Controller and Request Mapping annotations, the mapped method is annotated with Request Mapping and Response Body annotations, the parameters use the Request Body annotation method, and the parameters are annotated with Content-Type=application / json as needed.
3. The method for dynamically generating controllers based on custom interfaces and annotations according to claim 1, characterized in that: In step S1, the dynamic identification device is used to scan and record the access frequency of the access devices in the interface module and identify the types of access frequencies, including high-frequency access signals, medium-frequency access signals and low-frequency access signals.
4. The method for dynamically generating controllers based on custom interfaces and annotations according to claim 1, characterized in that: In step S6, a method proxy Handler is added to the dynamic class.
5. The method for dynamically generating controllers based on custom interfaces and annotations according to claim 1, characterized in that: The annotation Fusion Service definition is a two-dimensional array, where the first dimension indicates which parameter is being called and the second dimension indicates which annotation is being called for that parameter.
6. The method for dynamically generating controllers based on custom interfaces and annotations according to claim 1, characterized in that: The method is applied to a microservice architecture that includes multiple service units, where the server is part of the microservice architecture.
7. The method for dynamically generating controllers based on custom interfaces and annotations according to claim 1, characterized in that: In step S7, the controller can also be injected in the following way: the controller is injected using a custom XML tag. In the qualified file of springmvc.xsd, the Context Namespace Handler inherits the Namespace HandlerSupport class to parse the custom tag, and the Controller Bean Definition Parser implements the BeanDefinition Parser interface to inject the Bean. The dynamically generated Controller class is set as a Bean by root.set Bean Class and then injected into the Spring container.
8. The method for dynamically generating controllers based on custom interfaces and annotations according to claim 1, characterized in that: In step S7, the controller can also be injected by obtaining a reference to the Application Context based on Application Context Aware, then dynamically obtaining the object based on the Application Context, and then dynamically obtaining the required Bean in the code and injecting the Bean into the Spring container.
Citation Information
Patent Citations
Data dictionary dynamic escape method for realizing code-to-Chinese conversion
CN117540705A