A method, device, equipment and medium for implementing multi-service instance configuration

By introducing multi-instance container annotations and basic annotations into the Spring Boot framework, multiple service instances are dynamically generated and configured, which solves the problem of insufficient configuration capabilities of Spring Boot in the same service multi-instance scenario, and realizes flexible multi-instance management and configuration, improving the flexibility and stability of the system.

CN119322644BActive Publication Date: 2025-05-23湖南长银五八消费金融股份有限公司
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411444990.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-10-16
Publication Date
2025-05-23
Estimated Expiration
2044-10-16

AI Technical Summary

Technical Problem

The Spring Boot framework lacks configuration capabilities in multiple instance scenarios for the same service, and cannot effectively manage multiple client instances connected to different Redis servers.

Method used

By introducing multi-instance container annotations and multi-instance basic annotations, using the Java compiler and ASM bytecode operation library, multiple service instances are dynamically generated and configured, and official automatic assembly classes are extended and customized to realize the configuration and management of multi-service instances.

Benefits of technology

It realizes the flexibility to create and manage multiple instance objects in Spring Boot applications, makes up for the shortcomings of Spring framework in multi-instance configuration, improves configuration flexibility and consistency, and ensures the correctness and stability of automatic assembly classes in various environments.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119322644B_ABST
    Figure CN119322644B_ABST
Patent Text Reader

Abstract

The present application discloses a method, device, equipment and medium for realizing multi-service instance configuration. In the method, a Java compiler is called to scan source code to obtain a class annotated with a multi-instance container annotation, the class defines multiple service instances, and each service instance is annotated with a corresponding multi-instance basic annotation; based on a preset annotation processor, the multi-instance basic annotation corresponding to each service instance is traversed to obtain the classes attribute value and the prefix attribute value, and the first configuration class corresponding to the first automatic assembly class is obtained; according to the prefix attribute value, the bytecode of the first configuration class and the bytecode of the first automatic assembly class are processed to obtain the second configuration class and the second automatic assembly class; the second configuration class file and the second automatic assembly class file are appended to the resource directory to realize the configuration of the service instance. In this way, by introducing special multi-instance configuration annotations, namely multi-instance container annotations and multi-instance basic annotations, the official automatic assembly class is extended and customized to realize multi-service instance configuration.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a method, apparatus, device and medium for implementing multi-service instance configuration. Background Art

[0002] Spring Boot is a framework designed to simplify the development and deployment of Java applications based on the Spring Framework architecture. One of its core highlights is its excellent configuration simplification capability. Adhering to the concept of "convention is better than configuration", it significantly reduces the burden of manual configuration for developers by presetting a large number of default configurations. For example, it provides built-in automatic integration and assembly client functions for external services such as Database, Redis, MongoDB, Kafka, RabbitMQ, etc. You only need to introduce relevant dependencies and configure relevant parameters in the configuration file. The IoC container will have relevant beans available for dependency injection. For ease of understanding, taking the integration of Redis client as an example, developers only need to simply introduce the "org.springframework.boot:spring-boot-starter-data-redis" dependency into the project, and then make necessary configurations for Redis in the configuration file. Bean instances such as RedisConnectionFactory and RedisTemplate will appear in the IoC container.

[0003] However, the design of Spring Boot itself does not take into account the needs of multiple instances of the same service. For example, when you need to create multiple client instances connected to different Redis servers in the same application, the default configuration of Spring Boot is inadequate. Therefore, there is an urgent need for a multi-service instance configuration method that can integrate multiple instances in the Spring Boot framework. Summary of the invention

[0004] The present application provides a method, device, electronic device and storage medium for implementing multi-service instance configuration, which can implement multi-service instance configuration to integrate multiple instances in a Spring Boot framework.

[0005] In a first aspect, the present application provides a method for implementing multi-service instance configuration, which is applied to Spring Boot, and the method includes:

[0006] Calling a Java compiler to scan source code to obtain a class annotated with a multi-instance container annotation, wherein the class defines multiple service instances, and each of the service instances is annotated with a corresponding multi-instance basic annotation; the multi-instance basic annotation includes a classes attribute and a prefix attribute;

[0007] Based on the preset annotation processor, traverse the classes attribute and prefix attribute in the multi-instance basic annotation corresponding to each of the service instances to obtain the classes attribute value and the prefix attribute value; the classes attribute value is used to specify the first automatic assembly class, which is the official automatic assembly class; the prefix attribute value configures a prefix for each of the service instances;

[0008] Obtain a first configuration class corresponding to the first automatic assembly class; the first configuration class is annotated in the @EnableConfigurationProperties annotation of the first automatic assembly class;

[0009] Parsing the bytecode of the first automatic assembly class through the ASM bytecode operation library, and parsing the bytecode of the first configuration class through the ASM bytecode operation library;

[0010] According to the prefix attribute value, the bytecode of the first configuration class is processed to obtain the bytecode of the second configuration class; and according to the prefix attribute value, the bytecode of the first automatic assembly class is processed to obtain the bytecode of the second automatic assembly class;

[0011] Dynamically generate a second automatic assembly class file of the second automatic assembly class according to the bytecode of the second automatic assembly class; and dynamically generate a second configuration class file of the second configuration class according to the bytecode of the second configuration class;

[0012] The second configuration class file and the second auto-assembly class file are appended to the resource directory so that the second auto-assembly class is loaded when the Spring Boot is started to configure the service instance.

[0013] Optionally, before appending the second configuration class file and the second automatic assembly class file to the resource directory, the method further includes:

[0014] If the first auto-assembly class is scanned and annotated with @Import, the @Import annotation value is read, and the @Import annotation should indicate the auto-assembly class to be imported;

[0015] Obtain the @Import annotation value as the auto-assembly class to be imported, use the auto-assembly class to be imported as the first auto-assembly class, execute the step of obtaining the first configuration class corresponding to the first auto-assembly class, parse and obtain the bytecode of the first auto-assembly class through the ASM bytecode operation library, process the bytecode of the first auto-assembly class according to the prefix attribute value, obtain the bytecode of the second auto-assembly class, and dynamically generate the second auto-assembly class file of the second auto-assembly class according to the bytecode of the second auto-assembly class;

[0016] The @Import annotation value is replaced with the second automatic assembly class, where the second automatic assembly class is the automatic assembly class that imports the automatic assembly class to be imported.

[0017] Optionally, the processing the bytecode of the first configuration class according to the prefix attribute value to obtain the bytecode of the second configuration class includes:

[0018] According to the prefix attribute value, modify the first configuration class name of the first configuration class to obtain the second configuration class name of the second configuration class;

[0019] Modify the @AdditionalAutoConfiguration annotation in the first configuration class according to the prefix attribute value;

[0020] Set the second configuration class name in the @EnableConfigurationProperties annotation;

[0021] The bytecode processing of the first configuration class is completed to obtain the bytecode of the second configuration class.

[0022] Optionally, the processing of the bytecode of the first automatic assembly class according to the prefix attribute value to obtain the bytecode of the second automatic assembly class includes:

[0023] Scan the methods annotated with @Bean in the first auto-assembly class;

[0024] If the method contains the @ConditionalOnMissingBean annotation, remove the @ConditionalOnMissingBean annotation.

[0025] According to the prefix attribute value and the return type of the method, the name attribute and the autowireCandidate attribute in the @Bean annotation of the method are modified to obtain the second autowire class; the name attribute is used to specify the name of the Bean; the autowireCandidate attribute is used to indicate whether the Bean is used for autowire;

[0026] If the method has input parameters, the @Qualifier annotation is configured for the input parameters;

[0027] Generate the @Qualifier annotation value based on the parameter type and the prefix attribute value;

[0028] The bytecode processing of the first automatic assembly class is completed to obtain the bytecode of the second automatic assembly class.

[0029] Optionally, obtaining a first configuration class corresponding to the first automatic assembly class includes:

[0030] If the first automatic assembly class is scanned and annotated with the @EnableConfigurationProperties annotation, the first configuration class in the @EnableConfigurationProperties annotation is obtained.

[0031] Optionally, based on the preset annotation processor, traversing the classes attribute and the prefix attribute in the multi-instance basic annotation corresponding to each of the service instances to obtain the classes attribute value and the prefix attribute value includes:

[0032] Based on the process method in the preset annotation processor, the classes attribute and the prefix attribute in the multi-instance basic annotation corresponding to each of the service instances are traversed to obtain the classes attribute value and the prefix attribute value; the process method includes processing logic for implementing automatic configuration.

[0033] Optionally, the annotation processor class of the preset annotation processor inherits an abstract base class for processing annotations; the first parameter of the process method is a collection of multiple multi-instance basic annotations, so that the preset annotation processor processes the automatically configured processing logic related to the multi-instance basic annotations.

[0034] In a second aspect, the present application also provides a device for implementing multi-service instance configuration, which is applied to SpringBoot, and the device includes:

[0035] A compilation unit, used for calling a Java compiler to scan source code, and obtaining a class annotated with a multi-instance container annotation, wherein the class defines multiple service instances, and each of the service instances is annotated with a corresponding multi-instance basic annotation; the multi-instance basic annotation includes a classes attribute and a prefix attribute;

[0036] An annotation processing unit is used to traverse the classes attribute and the prefix attribute in the multi-instance basic annotation corresponding to each of the service instances based on a preset annotation processor to obtain the classes attribute value and the prefix attribute value; the classes attribute value is used to specify a first automatic assembly class, which is an official automatic assembly class; the prefix attribute value configures a prefix for each of the service instances;

[0037] An acquisition unit, used for acquiring a first configuration class corresponding to the first automatic assembly class; the first configuration class is annotated in the @EnableConfigurationProperties annotation of the first automatic assembly class;

[0038] A parsing unit, configured to parse the bytecode of the first automatic assembly class through an ASM bytecode operation library, and parse the bytecode of the first configuration class through the ASM bytecode operation library;

[0039] a processing unit, configured to process the bytecode of the first configuration class according to the prefix attribute value to obtain the bytecode of the second configuration class; and to process the bytecode of the first automatic assembly class according to the prefix attribute value to obtain the bytecode of the second automatic assembly class;

[0040] A generating unit, configured to dynamically generate a second automatic assembly class file of the second automatic assembly class according to the bytecode of the second automatic assembly class; and dynamically generate a second configuration class file of the second configuration class according to the bytecode of the second configuration class;

[0041] An appending unit is used to append the second configuration class file and the second automatic assembly class file to a resource directory, so as to load the second automatic assembly class when the Spring Boot is started to configure the service instance.

[0042] Optionally, the device further comprises an introduction unit;

[0043] The introduction unit is used for:

[0044] If the first auto-assembly class is scanned and annotated with @Import, the @Import annotation value is read, and the @Import annotation should indicate the auto-assembly class to be imported;

[0045] Obtain the @Import annotation value as the auto-assembly class to be imported, use the auto-assembly class to be imported as the first auto-assembly class, execute the step of obtaining the first configuration class corresponding to the first auto-assembly class, parse and obtain the bytecode of the first auto-assembly class through the ASM bytecode operation library, process the bytecode of the first auto-assembly class according to the prefix attribute value, obtain the bytecode of the second auto-assembly class, and dynamically generate the second auto-assembly class file of the second auto-assembly class according to the bytecode of the second auto-assembly class;

[0046] The @Import annotation value is replaced with the second automatic assembly class, where the second automatic assembly class is the automatic assembly class that imports the automatic assembly class to be imported.

[0047] Optionally, the processing unit is specifically used for:

[0048] According to the prefix attribute value, modify the first configuration class name of the first configuration class to obtain the second configuration class name of the second configuration class;

[0049] Modify the @AdditionalAutoConfiguration annotation in the first configuration class according to the prefix attribute value;

[0050] Set the second configuration class name in the @EnableConfigurationProperties annotation;

[0051] The bytecode processing of the first configuration class is completed to obtain the bytecode of the second configuration class.

[0052] Optionally, the processing unit is specifically used for:

[0053] Scan the methods annotated with @Bean in the first auto-assembly class;

[0054] If the method contains the @ConditionalOnMissingBean annotation, remove the @ConditionalOnMissingBean annotation.

[0055] According to the prefix attribute value and the return type of the method, the name attribute and the autowireCandidate attribute in the @Bean annotation of the method are modified to obtain the second autowire class; the name attribute is used to specify the name of the Bean; the autowireCandidate attribute is used to indicate whether the Bean is used for autowire;

[0056] If the method has input parameters, the @Qualifier annotation is configured for the input parameters;

[0057] Generate the @Qualifier annotation value based on the parameter type and the prefix attribute value;

[0058] The bytecode processing of the first automatic assembly class is completed to obtain the bytecode of the second automatic assembly class.

[0059] Optionally, the acquisition unit is specifically used to:

[0060] If the first automatic assembly class is scanned and annotated with the @EnableConfigurationProperties annotation, the first configuration class in the @EnableConfigurationProperties annotation is obtained.

[0061] Optionally, the annotation processing unit is specifically used to:

[0062] Based on the process method in the preset annotation processor, the classes attribute and the prefix attribute in the multi-instance basic annotation corresponding to each of the service instances are traversed to obtain the classes attribute value and the prefix attribute value; the process method includes processing logic for implementing automatic configuration.

[0063] Optionally, the annotation processor class of the preset annotation processor inherits an abstract base class for processing annotations; the first parameter of the process method is a collection of multiple multi-instance basic annotations, so that the preset annotation processor processes the automatically configured processing logic related to the multi-instance basic annotations.

[0064] In a third aspect, the present application further provides an electronic device, the electronic device comprising a processor and a memory:

[0065] The memory is used to store computer programs;

[0066] The processor is used to execute the method provided in the first aspect according to the computer program.

[0067] In a fourth aspect, the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium is used to store a computer program, and the computer program is used to execute the method provided in the first aspect above.

[0068] It can be seen that this application has the following beneficial effects:

[0069] The present application provides a method, apparatus, device and medium for implementing multi-service instance configuration. In the method, a Java compiler is called to scan source code to obtain a class annotated with a multi-instance container annotation, wherein the class defines multiple service instances, and each service instance is annotated with a corresponding multi-instance basic annotation; the multi-instance basic annotation includes a classes attribute and a prefix attribute; based on a preset annotation processor, the classes attribute and the prefix attribute in the multi-instance basic annotation corresponding to each service instance are traversed to obtain the classes attribute value and the prefix attribute value; the classes attribute value is used to specify a first automatic assembly class, which is an official automatic assembly class; the prefix attribute value configures a prefix for each service instance; the first configuration class corresponding to the first automatic assembly class is obtained; the first configuration class is annotated in the first automatic assembly class. In the @EnableConfigurationProperties annotation of the assembly class; the bytecode of the first automatic assembly class is parsed through the ASM bytecode operation library, and the bytecode of the first configuration class is parsed through the ASM bytecode operation library; according to the prefix attribute value, the bytecode of the first configuration class is processed to obtain the bytecode of the second configuration class; and, according to the prefix attribute value, the bytecode of the first automatic assembly class is processed to obtain the bytecode of the second automatic assembly class; according to the bytecode of the second automatic assembly class, a second automatic assembly class file of the second automatic assembly class is dynamically generated; and, according to the bytecode of the second configuration class, a second configuration class file of the second configuration class is dynamically generated; the second configuration class file and the second automatic assembly class file are appended to the resource directory so that the second automatic assembly class can be loaded when Spring Boot is started to configure the service instance. Thus, the embodiment of the present application provides a declarative way to define and manage multi-instance requirements by introducing special multi-instance configuration annotations, namely multi-instance container annotations and multi-instance basic annotations, making full use of the automatic assembly characteristics of Spring Boot, and cleverly expanding and customizing the official automatic assembly class, thereby realizing multi-service instance configuration, and being able to flexibly create and manage multiple instance objects in Spring Boot applications, that is, to integrate multiple instances in the Spring Boot framework, effectively making up for the shortcomings of the Spring framework in this area. In addition, since the creation of multiple service instances basically continues the official automatic assembly class provided by the Spring Boot official, the technical solution of the embodiment of the present application is highly consistent with the core mechanism of the Spring Boot framework in terms of underlying logic, which not only makes our solution more stable and reliable, but also allows the direct use of the comprehensive testing of these automatic assembly classes by the Spring Boot official, ensuring the correctness and stability of the automatic assembly class in various environments. BRIEF DESCRIPTION OF THE DRAWINGS

[0070] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present application. For ordinary technicians in this field, other drawings can also be obtained based on these drawings.

[0071] Figure 1 A flowchart of a method for implementing multi-service instance configuration in an embodiment of the present application;

[0072] Figure 2 A flowchart of an embodiment of a method for implementing multi-service instance configuration in an embodiment of the present application;

[0073] Figure 3 A schematic diagram of the structure of an apparatus 300 for implementing multi-service instance configuration provided in an embodiment of the present application;

[0074] Figure 4 A schematic diagram of the structure of an electronic device 400 provided in an embodiment of the present application. DETAILED DESCRIPTION

[0075] The multiple involved in the embodiments of the present application means greater than or equal to two. It should be noted that in the description of the embodiments of the present application, the words "first", "second", etc. are only used for the purpose of distinguishing the description, and cannot be understood as indicating or implying relative importance, nor can they be understood as indicating or implying an order.

[0076] In order to make the above-mentioned purposes, features and advantages of the present application more obvious and easy to understand, the embodiments of the present application are further described in detail below in conjunction with the accompanying drawings and specific implementation methods. It is understood that the specific embodiments described herein are only used to explain the present application and are not intended to limit the present application. In addition, it should be noted that, for ease of description, only the parts related to the present application are shown in the accompanying drawings, not all structures.

[0077] The following is an explanation of the terms used in this application:

[0078] 1. Spring Boot framework is an open source J2EE (Java 2 Platform Enterprise Edition) application framework. Inversion of control and dependency injection together form the basis of the Spring framework, enabling developers to develop applications more flexibly and efficiently.

[0079] 2.Bean is an object managed by the inversion of control Spring container in the Spring framework, representing any object instance created and managed by the Spring container. The Spring container is responsible for creating, configuring, assembling and managing these objects, which are usually called Spring Beans.

[0080] 3. Bytecode is the target code after Java program is compiled. It is a low-level intermediate code between source code and machine code. Bytecode is read and executed by JVM (Java Virtual Machine) and is platform-independent. Bytecode processing mainly involves optimizing, enhancing or modifying bytecode to improve program performance, add new features or fix security vulnerabilities. For example, using Java bytecode enhancement technology can achieve performance optimization or add new features to the program without modifying the source code.

[0081] 4. Automatic assembly is an important feature of the Spring framework, especially in Spring Boot. Automatic assembly reduces the need for manual configuration. Through the principle of annotation and convention over configuration, the automatic assembly mechanism can automatically configure and initialize components. In Spring Boot, automatic assembly is mainly implemented through the spring.factories file, which defines a series of automatically configured classes. When the project starts, Spring Boot automatically loads these configuration classes and initializes components as needed, greatly simplifying the development process.

[0082] 5. An annotation processor is a tool that can read, understand and process Java annotations. It allows developers to generate additional source code and resource files based on annotations at compile time, or check the code at the compile stage. An annotation processor processes annotations by implementing specific interfaces and being called by Java at compile time.

[0083] 6. Service Provider Interface (SPI) is a set of interface mechanisms provided by Java for third-party implementation or extension. The Spring framework has expanded and optimized it to form the Spring SPI mechanism.

[0084] SPI is a service provider discovery mechanism built into JDK that can be used to enable framework extensions and component replacements. It is mainly used for framework development. For example, Dubbo, Spring, JDBC, etc. use the SPI mechanism to provide different implementations of the same interface to different users, thereby improving the scalability of the framework.

[0085] 7. Database, a system for storing and managing data; Redis (Remote Dictionary Server, a remote dictionary server, an open source memory data structure storage system that supports multiple types of data structures; MongoDB, a database based on distributed file storage, aims to provide a scalable, high-performance data storage solution for WEB applications; Kafka, a distributed stream processing platform for building real-time data pipelines and streaming applications; RabbitMQ, an open source message broker software that implements the Advanced Message Queuing Protocol and is used to send messages in a distributed system through a lightweight proxy.

[0086] The current design of Spring Boot itself does not take into account the needs of multiple instances of the same service. When you need to create multiple client instances connected to different Redis servers in the same application, the default configuration of Spring Boot is somewhat inadequate.

[0087] The applicant found that the above situation mainly stems from two limitations:

[0088] First, in the Spring Boot configuration file, the configuration items prefixed with spring.data.redis only support the configuration of the connection information of one Redis client instance by default. This means that through a simple configuration file, it is not possible to directly support the parallel connection of multiple Redis instances.

[0089] Secondly, if developers try to bypass this limitation by manually creating RedisConnectionFactory and RedisTemplate client instances, they may encounter another problem: Spring Boot's automatic configuration mechanism will give in. This is the design of Spring Boot. It adds the @ConditionalOnMissingBean annotation to the @Bean definition method. The purpose of this annotation is to not register this bean if the same type of bean already exists in the IoC container. The effect achieved in this way is equivalent to allowing the application to override the beans provided by the built-in framework.

[0090] Currently, the common practice of integrating multiple instances in the Spring Boot framework tends to bypass Spring Boot's automatic configuration mechanism and instead explicitly create multiple instances through custom factory methods and configuration classes and register them in the IoC container. This approach has some disadvantages:

[0091] 1. Increased maintenance costs: The instance creation process of the official default configuration of Spring Boot is protected by its comprehensive test cases, ensuring the stability and reliability of the configuration. In contrast, custom instance creation factory classes require developers to write and maintain additional test cases to cover various possible usage scenarios. Although this effort can improve code quality to a certain extent, it is inevitable that there will be omissions, which increases potential maintenance costs and risks.

[0092] 2. Challenges of framework upgrades: With the iteration and update of Spring Boot versions, client classes or configuration interfaces may undergo reconstruction or upgrades. In this case, applications that rely on custom implementations may require more complex adjustments to adapt to the new version, increasing the complexity and difficulty of framework upgrades. Especially in automated testing and continuous integration environments, these adjustments may require additional time and resources to verify and stabilize.

[0093] 3. Increased code coupling: Custom factory methods and configuration classes may increase the coupling between codes. Because the creation and management of each instance needs to be implemented through a specific factory method, this may lead to frequent references to these factory classes or methods in multiple components or services, thereby increasing the complexity of the system.

[0094] 4. Lack of official support: Since the custom method of creating instances bypasses the default configuration mechanism of Spring Boot, you may encounter some problems that are not directly supported by the official. Although the Spring Boot community and official documentation provide rich resources and support, for specific problems of custom implementation, developers may need to find solutions on their own or seek help from the community.

[0095] Based on this, the embodiments of the present application provide a method, apparatus, device and medium for implementing multi-service instance configuration. In the method, a Java compiler is called to scan source code to obtain a class annotated with a multi-instance container annotation, wherein the class defines multiple service instances, and each service instance is annotated with a corresponding multi-instance basic annotation; the multi-instance basic annotation includes a classes attribute and a prefix attribute; based on a preset annotation processor, the classes attribute and the prefix attribute in the multi-instance basic annotation corresponding to each service instance are traversed to obtain the classes attribute value and the prefix attribute value; the classes attribute value is used to specify a first automatic assembly class, which is an official automatic assembly class; the prefix attribute value configures a prefix for each service instance; the first configuration class corresponding to the first automatic assembly class is obtained; the first configuration class is annotated in the first In the @EnableConfigurationProperties annotation of the auto-assembly class; the bytecode of the first auto-assembly class is parsed through the ASM bytecode operation library, and the bytecode of the first configuration class is parsed through the ASM bytecode operation library; according to the prefix attribute value, the bytecode of the first configuration class is processed to obtain the bytecode of the second configuration class; and, according to the prefix attribute value, the bytecode of the first auto-assembly class is processed to obtain the bytecode of the second auto-assembly class; according to the bytecode of the second auto-assembly class, a second auto-assembly class file of the second auto-assembly class is dynamically generated; and, according to the bytecode of the second configuration class, a second configuration class file of the second configuration class is dynamically generated; the second configuration class file and the second auto-assembly class file are appended to the resource directory to implement the configuration of the service instance.

[0096] Thus, the embodiment of the present application provides a declarative way to define and manage multi-instance requirements by introducing special multi-instance configuration annotations, namely multi-instance container annotations and multi-instance basic annotations, making full use of the automatic assembly characteristics of Spring Boot, and cleverly expanding and customizing the official automatic assembly class, thereby realizing multi-service instance configuration, and being able to flexibly create and manage multiple instance objects in Spring Boot applications, that is, to integrate multiple instances in the Spring Boot framework, effectively making up for the shortcomings of the Spring framework in this area. In addition, since the creation of multiple service instances basically continues the official automatic assembly class provided by the Spring Boot official, the technical solution of the embodiment of the present application is highly consistent with the core mechanism of the Spring Boot framework in terms of underlying logic, which not only makes our solution more stable and reliable, but also allows the direct use of the comprehensive testing of these automatic assembly classes by the Spring Boot official, ensuring the correctness and stability of the automatic assembly class in various environments.

[0097] To facilitate understanding of the specific implementation of the method for implementing multi-service instance configuration provided in the embodiment of the present application, it will be described below with reference to the accompanying drawings.

[0098] It should be noted that the subject implementing the method for implementing multiple service instance configuration may be a device for implementing multiple service instance configuration provided in the embodiment of the present application, and the device for implementing multiple service instance configuration may be carried in an electronic device or a functional module of an electronic device. The electronic device in the embodiment of the present application may be any device capable of implementing the method for implementing multiple service instance configuration in the embodiment of the present application, for example, an Internet of Things (IoT) device.

[0099] See also Figure 1 , provides a method for implementing multi-service instance configuration, the embodiment of the present application is applied to SpringBoot, for example, may include the following steps:

[0100] S1: Call the Java compiler to scan the source code and obtain the class annotated with the multi-instance container annotation.

[0101] It should be noted that a class defines multiple service instances, and each service instance is annotated with the corresponding multi-instance basic annotation; the multi-instance basic annotation includes the classes attribute and the prefix attribute, and multiple service instances are multiple service instances under the same service. The multi-instance container annotation is associated with the multi-instance basic annotation through @Repeatable.

[0102] Among them, multi-instance container annotations can be expressed by @AdditionalAutoConfigurations annotations, and multi-instance basic annotations can be expressed by @AdditionalAutoConfiguration annotations. @AdditionalAutoConfigurations annotations are designed to accommodate multiple @AdditionalAutoConfiguration annotated service instances.

[0103] The code could look like this:

[0104] public@interface AdditionalAutoConfigurations{

[0105] AdditionalAutoConfiguration[]value();

[0106] }

[0107] @Repeatable(AdditionalAutoConfigurations.class)

[0108] public@interface AdditionalAutoConfiguration{

[0109] Class<?>[]classes; / / Spring automatic assembly class

[0110] String prefix; / / Replace spring characters

[0111] }

[0112] Assuming that multiple service instances are represented by foo and bar, in the specific implementation process, you can create an AutoConfig class in the application and add the @AdditionalAutoConfigurations annotation to the class, as shown in the following code:

[0113] @AdditionalAutoConfiguration(classes=RedisAutoConfiguration.class,prefix="foo")

[0114] @AdditionalAutoConfiguration(classes=RedisAutoConfiguration.class,prefix="bar")

[0115] public class AppConfig{

[0116] }

[0117] Then add the following configuration to the application configuration file:

[0118] foo:

[0119] date:

[0120] redis:

[0121] host: 192.168.1.100

[0122] Port: 6379

[0123] bar:

[0124] date:

[0125] redis:

[0126] host: 192.168.1.130

[0127] Port: 6379

[0128] As an example, during the compilation process, the Java compiler scans all classes annotated with @AdditionalAutoConfigurations. Whenever such a class is detected, the process method of AdditionalAutoConfigurationAnnotationProcessor (preset annotation processor) is triggered.

[0129] In an embodiment of the present application, by introducing a special multi-instance configuration annotation (including multi-instance container annotation and multi-instance basic annotation), a declarative way is provided to define and manage multi-instance requirements. This method is both intuitive and flexible, greatly reducing the complexity of developers in configuring multiple instances.

[0130] S2: Based on the preset annotation processor, traverse the classes attribute and the prefix attribute in the multi-instance basic annotation corresponding to each of the service instances to obtain the classes attribute value and the prefix attribute value.

[0131] It should be noted that the classes attribute value is used to specify the first automatic assembly class, which is the official automatic assembly class, and the prefix attribute value is used to configure the prefix for each service instance.

[0132] As an example, get the @AdditionalAutoConfiguration annotation corresponding to each service instance. The first auto-assembly class below takes RedisAutoConfiguration as an example. In the @AdditionalAutoConfiguration annotation, the classes attribute receives a class object to explicitly specify the specific class that needs to be auto-assembled; the prefix attribute receives a string as the prefix to be replaced.

[0133] In a possible implementation, S2 provided in the embodiment of the present application may include:

[0134] Based on the process method in the preset annotation processor, the classes attribute and prefix attribute in the multi-instance basic annotation corresponding to each service instance are traversed to obtain the classes attribute value and the prefix attribute value; the process method includes processing logic for implementing automatic configuration.

[0135] It should be noted that the annotation processor class of the preset annotation processor inherits the abstract base class for processing annotations; the first parameter of the process method is a collection of multiple multi-instance basic annotations, so that the preset annotation processor processes the automatically configured processing logic related to the multi-instance basic annotations.

[0136] Among them, in the embodiment of the present application, the AdditionalAutoConfigurationAnnotationProcessor class inherits from the javax.annotation.processing.AbstractProcessor abstract class and is specifically used to process specific annotations. During the Java compilation process, the system automatically scans the source code for classes annotated with the @AdditionalAutoConfigurations annotation. Once these classes with specific annotations are found, the compiler triggers the process method in AdditionalAutoConfigurationAnnotationProcessor. The special thing about this process method is that its first parameter is a collection of all @AdditionalAutoConfigurations annotations, so that AdditionalAutoConfigurationAnnotationProcessor can centrally process the logic related to these annotations.

[0137] In the specific implementation process, inside the process method, multiple @AdditionalAutoConfigurations annotated service instances contained in the @AdditionalAutoConfigurations annotation are first parsed. Each service instance contains key information such as the classes attribute and the prefix attribute.

[0138] Among them, the specific logic code corresponding to the preset annotation processor can be as follows:

[0139] @SupportedAnnotationTypes("com.hncy58.phoenix.core.AdditionalAutoConfigurations")

[0140] public class AdditionalAutoConfigurationAnnotationProcessor extendsAbstractProcessor{

[0141] @Override

[0142] public boolean process(Set<? extends TypeElement>annotations,RoundEnvironment roundEnv){

[0143] / / ASM processing

[0144] return true;

[0145] }

[0146] }

[0147] S3: Obtain a first configuration class corresponding to the first automatic assembly class.

[0148] It should be noted that the first configuration class is annotated in the @EnableConfigurationProperties annotation of the first auto-wiring class. The @EnableConfigurationProperties annotation is used to enable the automatic wiring function of the first configuration class. Then when the Spring Boot application starts, it will automatically find and register these configuration property classes as beans in the Spring container.

[0149] In a possible implementation, S3 provided in the embodiment of the present application may include: if the first automatic assembly class is scanned and annotated with the @EnableConfigurationProperties annotation, then obtaining the first configuration class in the @EnableConfigurationProperties annotation.

[0150] As an example, read the class set in @EnableConfigurationProperties. For example, the value set in the @EnableConfigurationProperties annotation on RedisAutoConfiguration is RedisProperties.class, which is the first configuration class.

[0151] S4: parsing the bytecode of the first automatic assembly class through the ASM bytecode operation library, and parsing the bytecode of the first configuration class through the ASM bytecode operation library.

[0152] In the embodiment of the present application, the RedisProperties.class bytecode is loaded and parsed by the ASM tool, and the RedisAutoConfiguration bytecode is loaded and parsed by the ASM tool.

[0153] S5: According to the prefix attribute value, the bytecode of the first configuration class is processed to obtain the bytecode of the second configuration class; and, according to the prefix attribute value, the bytecode of the first automatic assembly class is processed to obtain the bytecode of the second automatic assembly class.

[0154] In a possible implementation, S5 provided in the embodiment of the present application may include:

[0155] S511, modifying a first configuration class name of a first configuration class according to a prefix attribute value, to obtain a second configuration class name of a second configuration class;

[0156] S512, modifying the @AdditionalAutoConfiguration annotation in the first configuration class according to the prefix attribute value;

[0157] S513, setting the second configuration class name in the @EnableConfigurationProperties annotation;

[0158] S514, completing the bytecode processing of the first configuration class, and obtaining the bytecode of the second configuration class.

[0159] In the embodiment of the present application, the prefix attribute value set in the @AdditionalAutoConfiguration annotation is read as a prefix, and a new configuration class name, that is, the second configuration class name, is generated according to the Java naming convention. For example, RedisProperties.class generates a new class name of FooRedisProperties.class. Modify @ConfigurationProperties on the RedisProperties class, and replace spring in the prefix attribute of @ConfigurationProperties with the prefix attribute value set in the @AdditionalAutoConfiguration annotation. The value of the @ConfigurationProperties annotation on RedisProperties.class is spring.data.redis, which eventually becomes foo.data.redis; the generated second configuration class name FooRedisProperties.class is set to the @EnableConfigurationProperties annotation.

[0160] In a possible implementation, S5 provided in the embodiment of the present application may include:

[0161] S521, scanning the methods annotated with @Bean in the first auto-assembly class.

[0162] S522: If the method contains a @ConditionalOnMissingBean annotation, remove the @ConditionalOnMissingBean annotation.

[0163] It should be noted that in Spring Boot, the role of the @ConditionalOnMissingBean annotation is to ensure that the Bean modified by the annotation is registered if and only if the specified Bean does not exist in the Spring container. After scanning the method annotated with the @Bean annotation, the embodiment of the present application needs to annotate the @ConditionalOnMissingBean to determine the registration of the Bean.

[0164] S523, modify the name attribute and the autowireCandidate attribute in the @Bean annotation of the method according to the prefix attribute value and the return type of the method to obtain a second autowire class.

[0165] Among them, the name attribute is used to specify the name of the Bean; the autowireCandidate attribute is used to indicate whether the Bean is used for automatic assembly.

[0166] In the embodiment of the present application, according to the return value type of the method and the prefix attribute value set in the @AdditionalAutoConfiguration annotation, it is set to the name attribute of the @Bean annotation according to the Java naming convention. At the same time, the autowireCandidate attribute of the @Bean annotation is set to false. For example, the @Bean on the method that creates RedisTemplate eventually becomes @Bean(name="fooRedisTemplate", autowireCandidate=false). It should be noted that setting autowireCandidate to false can prevent Spring Boot from "giving in" to the automatic assembly class.

[0167] S524: If the method has input parameters, configure the input parameters with @Qualifier annotation.

[0168] S525, generating the annotation value of @Qualifier according to the type of the parameter and the value of the prefix attribute.

[0169] In the embodiment of the present application, if the method has input parameters, the @Qualifier annotation needs to be configured for the input parameters, and the @Qualifier value is generated according to the type of the parameter and the prefix attribute value in the @AdditionalAutoConfiguration annotation according to the Java naming convention.<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) is processed into RedisTemplate<Object,Object> redisTemplate(@Qualifier("fooRedisConnectionFactory")RedisConnectionFactory redisConnectionFactory).

[0170] S526, completing the bytecode processing of the first automatic assembly class, and obtaining the bytecode of the second automatic assembly class.

[0171] When the application is started, you can register it in the business code by specifying the name using the @Qualifier annotation, as shown below:

[0172] @Service

[0173] public class BusinessService {

[0174] @Qualifier("fooRedisTemplate")

[0175] private RedisTemplate fooRedisTemplate; / / Inject fooredisTemplate instance according to name

[0176] @Qualifier("barRedisTemplate")

[0177] private RedisTemplate barRedisTemplate; / / Inject barredisTemplate instance according to name

[0178] @Autowired

[0179] private RedisTemplate redisTemplate; / / Inject the RedisTemplate instance created by native autoassembly

[0180] / / ...

[0181] }

[0182] In the embodiment of the present application, the bytecode processing of the first auto-assembly class and the bytecode processing of the first configuration class are realized. For example, the configuration prefix, the newly generated class name prefix and the bean name prefix are modified. For example, when prefix is ​​set to foo, the configuration prefix spring.data.redis will be modified to foo.data.redis, and a new automatically generated configuration class FooRedisAutoConfiguration will be added, and the bean name of RedisTemplate will be fooRedisTemplate. Similarly, when prefix is ​​set to bar, the configuration prefix spring.data.redis will be modified to bar.data.redis, and a new automatically generated configuration class BarRedisAutoConfiguration will be added, and the bean name of RedisTemplate will be barRedisTemplate.

[0183] S6: dynamically generate a second automatic assembly class file of the second automatic assembly class according to the bytecode of the second automatic assembly class; and dynamically generate a second configuration class file of the second configuration class according to the bytecode of the second configuration class.

[0184] It should be noted that the second configuration class defines a Bean object; the second automatic assembly class references the Bean object declared in the second configuration class.

[0185] In the embodiment of the present application, Java Annotation Processing and ASM technology are used to automatically process annotations and generate new classes, namely the second automatic assembly class and the second configuration class, during the compilation stage. This automatic processing at compile time avoids runtime overhead and improves the consistency and accuracy of the configuration.

[0186] S7: Append the second configuration class file and the second auto-assembly class file to the resource directory so that the second auto-assembly class is loaded when SpringBoot is started to configure the service instance.

[0187] As an example, the generated new class, FooRedisAutoConfiguration, can append the second autowire class name of the second autowire class to the path of the resource directory and append the second autowire class file to the resource directory:

[0188] In the file under META-INF / spring / org.springframework.boot.autoconfigure.AutoConfiguration.im ports, this ensures that Spring Boot can automatically identify and load the second auto-assembly class at startup. At the same time, when loading the second auto-assembly class, the second configuration class corresponding to the second auto-assembly class will be loaded.

[0189] It should be noted that the embodiment of the present application deeply processes the bytecode of the Spring Boot auto-assembly class, and seamlessly integrates the processed second auto-assembly class into the Spring Boot auto-assembly mechanism. The core implementation of this process is cleverly encapsulated in the process method of the annotation processor class AdditionalAutoConfigurationAnnotationProcessor.

[0190] In the embodiment of the present application, by adding the generated second automatic assembly class and second configuration class to the Spring SPI file, it is ensured that Spring Boot can automatically identify and load these assembly classes and configuration classes when it is started, and seamless integration with the Spring Boot framework is achieved. In this way, developers can easily create and manage multiple instances without making any modifications or extensions to the automatic configuration mechanism of Spring Boot.

[0191] See also Figure 2 , a flowchart of an embodiment of a method for implementing multi-service instance configuration is also provided for the embodiment of the present application. In the embodiment of the present application, before S7, the method provided by the embodiment of the present application may also include: if the @Import annotation is marked on the first auto-assembly class, the @Import annotation value is read, and the @Import annotation application indicates the auto-assembly class to be imported. The @Import annotation value is obtained as the auto-assembly class to be imported, and the auto-assembly class to be imported is used as the first auto-assembly class, and the first configuration class corresponding to the first auto-assembly class is obtained. The bytecode of the first auto-assembly class is parsed and obtained through the ASM bytecode operation library. According to the prefix attribute value, the bytecode of the first auto-assembly class is processed to obtain the bytecode of the second auto-assembly class, and according to the bytecode of the second auto-assembly class, a second auto-assembly class file of the second auto-assembly class is dynamically generated; the @Import annotation value is replaced with the second auto-assembly class, and the second auto-assembly class is the auto-assembly class that imports the auto-assembly class to be imported.

[0192] That is to say, in an embodiment of the present application, when scanning the first automatic assembly class marked with the @Import annotation, steps S3 to S7 are executed to import the automatic assembly class to be imported according to the @Import annotation value, and then the second automatic assembly class finally obtained is the automatic assembly class combined with the automatic assembly class to be imported, so as to achieve a more complete automatic configuration solution.

[0193] In the embodiment of the present application, by automatically generating configuration classes, the amount of code related to the creation of specific instances in the application code is reduced, thereby reducing the coupling between codes and helping to improve the maintainability and scalability of the code.

[0194] In addition, in the embodiment of the present application, since the creation of multiple instances basically continues the automatic assembly class provided by Spring Boot officially, this means that our solution is highly consistent with the core mechanism of the Spring Boot framework in terms of underlying logic, which not only makes our solution more stable and reliable, but more importantly, allows direct use of the comprehensive tests conducted by Spring Boot officially on these automatic assembly classes. These tests cover various usage scenarios and boundary conditions, ensuring the correctness and stability of the automatic assembly class in various environments.

[0195] Moreover, the official test protection also means that our solution can automatically evolve with the update and iteration of the Spring Boot framework. Whenever Spring Boot officially releases a new version, its auto-wiring classes will be strictly tested and verified to ensure that they are compatible with the new version of the framework. Since our solution relies on these auto-wiring classes, we can also indirectly enjoy the benefits of this update and iteration without worrying about compatibility issues caused by framework upgrades.

[0196] Thus, the embodiment of the present application provides a declarative way to define and manage multi-instance requirements by introducing special multi-instance configuration annotations, namely multi-instance container annotations and multi-instance basic annotations, making full use of the automatic assembly feature of Spring Boot, and cleverly extending and customizing the official automatic assembly class, thereby realizing multi-service instance configuration, and being able to flexibly create and manage multiple instance objects in Spring Boot applications, that is, to integrate multiple instances in the Spring Boot framework, effectively making up for the shortcomings of the Spring framework in this area. The method provided by the embodiment of the present application makes it simple and efficient to create and manage multiple instances in Spring Boot applications without complex configuration or coding, providing more possibilities for functional expansion and performance optimization of applications, and enhancing the flexibility and scalability of the system.

[0197] See also Figure 3The embodiment of the present application also provides a device 300 for implementing multi-service instance configuration, which is applied to the Spring Boot framework, and the device 300 includes:

[0198] The compiling unit 301 is used to call a Java compiler to scan source code and obtain a class annotated with a multi-instance container annotation, wherein the class defines multiple service instances, and each of the service instances is annotated with a corresponding multi-instance basic annotation; the multi-instance basic annotation includes a classes attribute and a prefix attribute;

[0199] The annotation processing unit 302 is used to traverse the classes attribute and the prefix attribute in the multi-instance basic annotation corresponding to each of the service instances based on a preset annotation processor to obtain the classes attribute value and the prefix attribute value; the classes attribute value is used to specify the first automatic assembly class, which is the official automatic assembly class; the prefix attribute value configures a prefix for each of the service instances;

[0200] An acquisition unit 303 is used to acquire a first configuration class corresponding to the first automatic assembly class; the first configuration class is annotated in the @EnableConfigurationProperties annotation of the first automatic assembly class;

[0201] The parsing unit 304 is used to parse the bytecode of the first automatic assembly class through the ASM bytecode operation library, and to parse the bytecode of the first configuration class through the ASM bytecode operation library;

[0202] The processing unit 305 is used to process the bytecode of the first configuration class according to the prefix attribute value to obtain the bytecode of the second configuration class; and to process the bytecode of the first automatic assembly class according to the prefix attribute value to obtain the bytecode of the second automatic assembly class;

[0203] A generating unit 306 is used to dynamically generate a second automatic assembly class file of the second automatic assembly class according to the bytecode of the second automatic assembly class; and dynamically generate a second configuration class file of the second configuration class according to the bytecode of the second configuration class;

[0204] The appending unit 307 is used to append the second configuration class file and the second automatic assembly class file to the resource directory, so as to load the second automatic assembly class when Spring Boot is started to implement the configuration of the service instance.

[0205] Optionally, the device further comprises an introduction unit 308;

[0206] The import unit 308 is used to:

[0207] If the first auto-assembly class is scanned and annotated with @Import, the @Import annotation value is read, and the @Import annotation should indicate the auto-assembly class to be imported;

[0208] Obtain the @Import annotation value as the auto-assembly class to be imported, use the auto-assembly class to be imported as the first auto-assembly class, execute the step of obtaining the first configuration class corresponding to the first auto-assembly class, parse and obtain the bytecode of the first auto-assembly class through the ASM bytecode operation library, process the bytecode of the first auto-assembly class according to the prefix attribute value, obtain the bytecode of the second auto-assembly class, and dynamically generate the second auto-assembly class file of the second auto-assembly class according to the bytecode of the second auto-assembly class;

[0209] The @Import annotation value is replaced with the second automatic assembly class, where the second automatic assembly class is the automatic assembly class that imports the automatic assembly class to be imported.

[0210] Optionally, the processing unit 305 is specifically used for:

[0211] According to the prefix attribute value, modify the first configuration class name of the first configuration class to obtain the second configuration class name of the second configuration class;

[0212] Modify the @AdditionalAutoConfiguration annotation in the first configuration class according to the prefix attribute value;

[0213] Set the second configuration class name in the @EnableConfigurationProperties annotation;

[0214] The bytecode processing of the first configuration class is completed to obtain the bytecode of the second configuration class.

[0215] Optionally, the processing unit 305 is specifically used for:

[0216] Scan the methods annotated with @Bean in the first auto-assembly class;

[0217] If the method contains the @ConditionalOnMissingBean annotation, remove the @ConditionalOnMissingBean annotation.

[0218] According to the prefix attribute value and the return type of the method, the name attribute and the autowireCandidate attribute in the @Bean annotation of the method are modified to obtain the second autowire class; the name attribute is used to specify the name of the Bean; the autowireCandidate attribute is used to indicate whether the Bean is used for autowire;

[0219] If the method has input parameters, the @Qualifier annotation is configured for the input parameters;

[0220] Generate the @Qualifier annotation value based on the parameter type and the prefix attribute value;

[0221] The bytecode processing of the first automatic assembly class is completed to obtain the bytecode of the second automatic assembly class.

[0222] Optionally, the acquiring unit 303 is specifically configured to:

[0223] If the first automatic assembly class is scanned and annotated with the @EnableConfigurationProperties annotation, the first configuration class in the @EnableConfigurationProperties annotation is obtained.

[0224] Optionally, the annotation processing unit 302 is specifically configured to:

[0225] Based on the process method in the preset annotation processor, the classes attribute and the prefix attribute in the multi-instance basic annotation corresponding to each of the service instances are traversed to obtain the classes attribute value and the prefix attribute value; the process method includes processing logic for implementing automatic configuration.

[0226] Optionally, the annotation processor class of the preset annotation processor inherits an abstract base class for processing annotations; the first parameter of the process method is a collection of multiple multi-instance basic annotations, so that the preset annotation processor processes the automatically configured processing logic related to the multi-instance basic annotations.

[0227] It should be noted that the specific implementation method of the device 300 and the technical effects achieved can be found in Figure 1 or Figure 2 The relevant description in the method shown.

[0228] In addition, the present application embodiment also provides an electronic device 400, such as Figure 4 As shown, the electronic device 400 includes a processor 401 and a memory 402:

[0229] The memory 402 is used to store computer programs;

[0230] The processor 401 is used to execute according to the computer program Figure 1 or Figure 2 The method provided.

[0231] In addition, an embodiment of the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium is used to store a computer program, and the computer program is used to execute the method provided by the embodiment of the present application.

[0232] Through the description of the above implementation methods, it can be known that those skilled in the art can clearly understand that all or part of the steps in the above-mentioned embodiment method can be implemented by means of software plus a general hardware platform. Based on such an understanding, the technical solution of the present application can be embodied in the form of a software product, which can be stored in a storage medium, such as a read-only memory (ROM) / RAM, a magnetic disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network communication device such as a router) to execute the methods described in each embodiment of the present application or some parts of the embodiments.

[0233] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device embodiment, since it 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 above is only schematic, in which the modules described as separate components may or may not be physically separated, and the components displayed as modules may or may not be physical modules, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules can be selected according to actual needs to achieve the goals of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative work.

[0234] The above is only a preferred embodiment of the present application and is not intended to limit the protection scope of the present application. It should be noted that, for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the present application, and these improvements and modifications should also be regarded as the protection scope of the present application.

Claims

1. A method for implementing multi-service instance configuration, characterized in that: Applied to Spring Boot, the method includes: Calling a Java compiler to scan source code to obtain a class annotated with a multi-instance container annotation, wherein the class defines multiple service instances, and each of the service instances is annotated with a corresponding multi-instance basic annotation; the multi-instance basic annotation includes a classes attribute and a prefix attribute; Based on the preset annotation processor, traverse the classes attribute and prefix attribute in the multi-instance basic annotation corresponding to each of the service instances to obtain the classes attribute value and the prefix attribute value; the classes attribute value is used to specify the first automatic assembly class, which is the official automatic assembly class; the prefix attribute value configures a prefix for each of the service instances; Obtain a first configuration class corresponding to the first automatic assembly class; the first configuration class is annotated in the @EnableConfigurationProperties annotation of the first automatic assembly class; Parsing the bytecode of the first automatic assembly class through the ASM bytecode operation library, and parsing the bytecode of the first configuration class through the ASM bytecode operation library; According to the prefix attribute value, the bytecode of the first configuration class is processed to obtain the bytecode of the second configuration class; and according to the prefix attribute value, the bytecode of the first automatic assembly class is processed to obtain the bytecode of the second automatic assembly class; Dynamically generate a second automatic assembly class file of the second automatic assembly class according to the bytecode of the second automatic assembly class; and dynamically generate a second configuration class file of the second configuration class according to the bytecode of the second configuration class; The second configuration class file and the second auto-assembly class file are appended to the resource directory so that the second auto-assembly class is loaded when the Spring Boot is started to configure the service instance.

2. The method according to claim 1, characterized in that: Before appending the second configuration class file and the second automatic assembly class file to the resource directory, the method further includes: If the first auto-assembly class is scanned and annotated with @Import, the @Import annotation value is read, and the @Import annotation should indicate the auto-assembly class to be imported; Obtain the @Import annotation value as the auto-assembly class to be imported, use the auto-assembly class to be imported as the first auto-assembly class, execute the step of obtaining the first configuration class corresponding to the first auto-assembly class, parse and obtain the bytecode of the first auto-assembly class through the ASM bytecode operation library, process the bytecode of the first auto-assembly class according to the prefix attribute value, obtain the bytecode of the second auto-assembly class, and dynamically generate the second auto-assembly class file of the second auto-assembly class according to the bytecode of the second auto-assembly class; The @Import annotation value is replaced with the second automatic assembly class, where the second automatic assembly class is the automatic assembly class that imports the automatic assembly class to be imported.

3. The method according to claim 1, characterized in that The step of processing the bytecode of the first configuration class according to the prefix attribute value to obtain the bytecode of the second configuration class includes: According to the prefix attribute value, modify the first configuration class name of the first configuration class to obtain the second configuration class name of the second configuration class; Modify the @AdditionalAutoConfiguration annotation in the first configuration class according to the prefix attribute value; Set the second configuration class name in the @EnableConfigurationProperties annotation; The bytecode processing of the first configuration class is completed to obtain the bytecode of the second configuration class.

4. The method according to claim 1, characterized in that: The step of processing the bytecode of the first automatic assembly class according to the prefix attribute value to obtain the bytecode of the second automatic assembly class includes: Scan the methods annotated with @Bean in the first auto-assembly class; If the method contains the @ConditionalOnMissingBean annotation, remove the @ConditionalOnMissingBean annotation. According to the prefix attribute value and the return type of the method, the name attribute and the autowireCandidate attribute in the @Bean annotation of the method are modified to obtain the second autowire class; the name attribute is used to specify the name of the Bean; the autowireCandidate attribute is used to indicate whether the Bean is used for autowire; If the method has input parameters, the @Qualifier annotation is configured for the input parameters; Generate the @Qualifier annotation value based on the parameter type and the prefix attribute value; The bytecode processing of the first automatic assembly class is completed to obtain the bytecode of the second automatic assembly class.

5. The method according to claim 1, characterized in that The obtaining of the first configuration class corresponding to the first automatic assembly class includes: If the first automatic assembly class is scanned and annotated with the @EnableConfigurationProperties annotation, the first configuration class in the @EnableConfigurationProperties annotation is obtained.

6. The method according to claim 1, characterized in that Based on the preset annotation processor, traversing the classes attribute and the prefix attribute in the multi-instance basic annotation corresponding to each of the service instances, obtaining the classes attribute value and the prefix attribute value, including: Based on the process method in the preset annotation processor, the classes attribute and the prefix attribute in the multi-instance basic annotation corresponding to each of the service instances are traversed to obtain the classes attribute value and the prefix attribute value; the process method includes processing logic for implementing automatic configuration.

7. The method according to claim 6, characterized in that The annotation processor class of the preset annotation processor inherits the abstract base class for processing annotations; the first parameter of the process method is a collection of multiple multi-instance basic annotations, so that the preset annotation processor processes the automatically configured processing logic related to the multi-instance basic annotations.

8. A device for implementing multi-service instance configuration, characterized in that: Applied to the Spring Boot framework, the device includes: A compilation unit, used for calling a Java compiler to scan source code, and obtaining a class annotated with a multi-instance container annotation, wherein the class defines multiple service instances, and each of the service instances is annotated with a corresponding multi-instance basic annotation; the multi-instance basic annotation includes a classes attribute and a prefix attribute; An annotation processing unit is used to traverse the classes attribute and the prefix attribute in the multi-instance basic annotation corresponding to each of the service instances based on a preset annotation processor to obtain the classes attribute value and the prefix attribute value; the classes attribute value is used to specify a first automatic assembly class, which is an official automatic assembly class; the prefix attribute value configures a prefix for each of the service instances; An acquisition unit, used for acquiring a first configuration class corresponding to the first automatic assembly class; the first configuration class is annotated in the @EnableConfigurationProperties annotation of the first automatic assembly class; A parsing unit, configured to parse the bytecode of the first automatic assembly class through an ASM bytecode operation library, and parse the bytecode of the first configuration class through the ASM bytecode operation library; a processing unit, configured to process the bytecode of the first configuration class according to the prefix attribute value to obtain the bytecode of the second configuration class; and to process the bytecode of the first automatic assembly class according to the prefix attribute value to obtain the bytecode of the second automatic assembly class; A generating unit, configured to dynamically generate a second automatic assembly class file of the second automatic assembly class according to the bytecode of the second automatic assembly class; and dynamically generate a second configuration class file of the second configuration class according to the bytecode of the second configuration class; The appending unit is used to append the second configuration class file and the second automatic assembly class file to the resource directory, so as to load the second automatic assembly class when Spring Boot is started to realize the configuration of the service instance.

9. An electronic device, characterized in that: The electronic device comprises a processor and a memory: The memory is used to store computer programs; The processor is configured to execute the method according to any one of claims 1 to 7 according to the computer program.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium is used to store a computer program, and the computer program is used to execute the method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Automatic disposition and maintenance method based on virtualization platform Redis service

    CN106506191A

  • Service instance configuration method and device, electronic equipment and storage medium

    CN112306568A