A method for customizing business components in a business development platform
By using AOP and IOC technologies in the Spring Boot framework, @ExtensionApi and @Extension annotations are defined, combined with the ExtensionBeanDefinitionEnhancer component, the business component logic is realized without intrusive customization and dynamic switching, solving the problem of difficult to meet specific business needs in the existing technology, and improving development flexibility and adaptability.
Patent Information
- Application Number
- CN202411912371.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-24
- Publication Date
- 2025-04-08
AI Technical Summary
The prior art is difficult to customize and dynamically switch business component logic in the Spring Boot framework without intrusiveness, making it difficult to meet specific business needs in customized project development.
By utilizing AOP and IOC technologies in Spring Boot and annotation technology, @ExtensionApi and @Extension annotations are defined, combined with the ExtensionBeanDefinitionEnhancer component to implement the enhanced processing of BeanDefinition, and dynamically switch the implementation classes of the component.
It realizes the non-invasive customization and dynamic switching of business component logic in the Spring Boot framework to meet specific business needs, and improves development flexibility and adaptability.
Smart Images

Figure CN119781764A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of business information development, and in particular relates to a method for customizing business components in a business development platform. Background Art
[0002] In general products, low-code platforms or microservice architectures, mature and stable business modules are integrated for project use, and several business implementation logics can be provided for a certain business type. In the process of customized project development or product deployment, there may be situations where the default implementation logic cannot meet specific business needs. Therefore, it is necessary to provide an effective mechanism in the platform to allow project developers to easily customize business component logic and replace the default implementation logic without making too many modifications to the development model, code or the entire platform architecture, which makes project development convenient, but also increases the learning curve and limits the flexibility and adaptability of the platform.
[0003] Spring boot is a mainstream business development framework in the industry, but there is no improvement solution to solve this problem. Whether it is its own Service Locator Pattern, Primaray or third-party solutions, such as Spring Plugin, Cola, etc., they cannot meet this demand. Existing technical solutions all require major changes to the development model and major modifications to existing program methods, which is difficult and cumbersome to develop, time-consuming and labor-intensive. Summary of the invention
[0004] To solve the above problems, the purpose of the present invention is to provide a flexible, transparent, and non-intrusive solution for implementing switching business logic for the Spring boot framework. The invention protects the technical points of transparent dynamic injection and dynamic switching of components by using ExtensionApi, Extension and similar annotations, and extension points in Spring boot.
[0005] To achieve the above objectives, the present invention makes full use of AOP and IOC technology and annotation technology in spring boot, and innovatively provides the following technical solutions, a method for customizing business components in a business development platform, comprising the following steps: Step 1: Add annotations to the Extension family First define @ExtensionApi and @Extension annotations, Then add the @ExtensionApi annotation to the AuthorService interface, add the @Extension annotation to the implementation class, and indicate the extension point coordinates.
[0006] Step 2: Configure Define the extension point coordinates to be used in application.yml.
[0007] Step 3: Implement automatic configuration Step 3.1. Automatically create components Create a configuration class ExtensionAutoConfiguration with @Configuration annotation. ExtensionAutoConfiguration: A class annotated with @Configuration that automatically configures the ExtensionBeanDefinitionEnhancer component. Note: This class, as the entry point for auto-configuration, can automatically create the ExtensionBeanDefinitionEnhancer component when the Spring Boot application starts. SpringBoot automatically loads this class based on the configuration in the classpath when it starts.
[0008] Step 3.2. Extension point implementation, customized operation The ExtensionBeanDefinitionEnhancer component implements an extension point of Springboot, the BeanDefinitionRegistryPostProcessor interface, which is used to enhance the BeanDefinition after the Spring container is initialized and before it is refreshed.
[0009] Description: This interface is a container-level postprocessor. The processor will be executed once after the Spring container is initialized and before it is refreshed. This interface provides an opportunity to modify the definition of the Bean in the Spring container. Specifically, this interface provides a postProcessBeanDefinitionRegistry() method, which can be used to add, delete, modify, and query BeanDefinition through BeanDefinitionRegistry.
[0010] Optimization solution: Through this extension point feature, some special and complex processing is performed on the existing BeanDefinition, so as to achieve non-invasive extension, replacement and dynamic update functions.
[0011] After all BeanDefinitions are loaded and before Beans are instantiated, BeanDefinitions can be customized by implementing the BeanDefinitionRegistryPostProcessor interface.
[0012] Step 3.3. Dynamically switch execution class Dynamically switch proxy beans at runtime by configuring the platform or monitoring configuration files; Get the ExtensionProxyFactoryBean instance and get the execution class to be replaced through the Value of the configuration information; Update the FactoryBean instance through the setTargetBeanName method and create a new proxy instance.
[0013] Furthermore, in step 3.2, a postProcessBeanDefinitionRegistry() method is provided to allow BeanDefinitionRegistry to add, delete, modify, and query BeanDefinition.
[0014] Furthermore, in step 3.2, the specific process of customization operation is as follows: (1) Get all BeanDefinition Get all registered BeanDefinitions through BeanDefinitionRegistry; (2) Determine and process BeanDefinition For each BeanDefinition, check if there is an @Extension annotation; If yes, get the extension point coordinates bizId attribute, and decide which implementation class instance to use based on the coordinates and configuration; Traverse all interfaces implemented by the class to determine whether there is an @ExtensionApi annotation. If so, establish an association between the interface, implementation class, and bizId; (3) Traverse the association relationship and process it For each interface annotated with @ExtensionApi, traverse all associated BeanDefinitions. For each interface annotated with @ExtensionApi, traverse all associated beanDefintions and set the setPrimary of the original beanDefintion to false, indicating that it will not be injected; If the bizId annotated in the original beanDefintion is the same as the extension point coordinates configured in application.yml, create a proxy BeanDefintion to indicate that this class needs to be injected. Associate the ExtensionProxyFactoryBean class as its instantiated bean, and rename the beanName to the prefix "extensionedTarget." + the original beanName. Finally, set setPrimary to true to ensure that the bean instance created by this BeanDefinition will be injected into the caller; (4) Call registry.registerBeanDefinition(proxy.getBeanName(), proxy.getBeanDefinition()) to register the proxy BeanDefinition.
[0015] Furthermore, in step (2), the association between the interface, implementation class and bizId is established and stored in the Map <interface, Map<bizId, classImp> > in.
[0016] Furthermore, in step (3), in spring boot, if a class is marked as primary, it will be injected first; if the extension point coordinates of the interface are not configured in application.yml, the default class implementation is used.
[0017] Furthermore, in step 3.3, during dynamic switching, a locking mechanism is used to prevent concurrency conflicts.
[0018] Beneficial effects: In the field of business information development, the present invention uses the spring framework for business code development, and in particular relates to a low-code platform, product or microservice architecture based on the framework, which can switch business logic implementation very flexibly and non-invasively. Specifically, a very flexible mechanism can be provided in these platforms, so that the project party can easily customize the implementation of business logic to replace the default implementation class to meet specific business needs, and the business logic can be switched freely at runtime.
[0019] 1. The code changes are small, and no changes are made to the existing implementation classes in the platform.
[0020] 2. Less invasiveness, still injecting specific implementation classes according to the existing Spring boot injection method. Reduce the learning curve, no learning cost is required.
[0021] 3. Observability: It can be combined with the actuator monitoring framework in springboot to list all interfaces with dynamic extension points, classes that implement the interface, extension point coordinates, and currently executing task extension classes, making it easier for developers to debug and analyze problems.
[0022] 4. The execution class can be switched dynamically, and the extension point coordinates of the interface can be changed dynamically during program running to achieve a dynamic switching effect. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] Figure 1 It is a structural schematic diagram of the present invention.
[0024] Figure 2 It is a flowchart of the present invention. DETAILED DESCRIPTION
[0025] The present invention will be further described below in conjunction with the accompanying drawings and specific embodiments. Example
[0026] like Figure 1 and Figure 2 A method for customizing business components in a business development platform includes the following steps: Step 1: Add annotations to the Extension family First, define @ExtensionApi and @Extension annotations, and add ExtensionApi annotation to AuthorService interface. You can provide different specific behaviors for the methods defined in the interface in different implementation classes, thus achieving polymorphism and extensibility; Add the Extension annotation to the implementation class and indicate the extension point coordinates. Use the polymorphism of the interface to use the object of this implementation class.
[0027] The representation without the BizId attribute is the default extension point.
[0028] Step 2: Configure Define the extension point coordinates to be used in application.yml.
[0029] Step 3: Implement automatic configuration, automatically identify configuration classes and configure various objects; 3.1. Automatically create components When the application starts, a configuration class ExtensionAutoConfiguration with the @Configuration annotation is created to automatically create the ExtensionBeanDefinitionEnhancer component.
[0030] The class annotated with @Configuration is used to automatically configure the ExtensionBeanDefinitionEnhancer component. Spring Boot automatically loads this class based on the configuration in the class path when it starts.
[0031] 3.2. Extension point implementation, customized operation Implement the BeanDefinitionRegistryPostProcessor interface of Springboot's extension point through the ExtensionBeanDefinitionEnhancer component. This interface is a container-level post-processor that is executed once after the Spring container is initialized and before it is refreshed to enhance the BeanDefinition. Provides a postProcessBeanDefinitionRegistry() method to allow the BeanDefinitionRegistry to add, delete, modify, and query BeanDefinition.
[0032] After all BeanDefinitions are loaded and before Beans are instantiated, by implementing the BeanDefinitionRegistryPostProcessor interface, you can customize BeanDefinitions, such as modifying the properties of a certain bean's BeanDefinition (bean definition) and manually registering some complex beans. This extension point feature allows special processing of existing BeanDefinitions to achieve non-invasive extensions, replacements, and dynamic updates. The specific process is as follows: (1) Get all BeanDefinition Get all registered BeanDefinitions through BeanDefinitionRegistry; (2) Determine and process BeanDefinition Determine whether each BeanDefinition has the @Extension annotation. If so, it is determined to be an extended implementation class, and the bizId attribute of the annotation is obtained to obtain the extension point coordinates. Based on the coordinates, the configuration determines which implementation class instance to use. Then, find all interfaces implemented by the class to determine whether they have the @ExtensionApi annotation. If so, establish the association between the interface, implementation class, and bizId. (These relationships are in the form of Map <interface,Map<bizId,classImp> > to save. If no interface meets the annotation after traversing, an error will be reported. ) (3) Traverse the association relationship and process it After all BeanDefinitions have been processed in step (2) above, the association relationships established above are traversed.
[0033] For each interface annotated with @ExtensionApi, traverse all associated beanDefintions and set the setPrimary of the original beanDefintion to false, indicating that it will not be injected. In spring boot, if a class is annotated with @primary, it will be injected first.
[0034] If the bizId annotated in the original beanDefintion is the same as the extension point coordinates configured in application.yml, create a proxy BeanDefintion to indicate that this class needs to be injected. Associate the ExtensionProxyFactoryBean class as its instantiated bean, and rename the beanName to the prefix "extensionedTarget." + the original beanName. Finally, set setPrimary to true to ensure that the bean instance created by this BeanDefinition will be injected into the caller.
[0035] Note: ExtensionProxyFactoryBean implements the FactoryBean interface, which allows the spring container to create the bean objects we need. In the actual process, the created bean object will be returned to the Spring container by calling the getObject method. What we return is the proxy object created by ProxyFactory.getProxy. Spring provides a programmatic AOP proxy method ProxyFactory. This method can be used to easily use JDK dynamic proxies or CGLIB proxies, and supports the use of Advisor chain. The object methods that need to be proxied can be enhanced through the advisor chain. The implementation allows ProxyFactory to create a proxy object of the class object that actually needs to be injected, so that its final method calls will be passed to the actual object.
[0036] If the extension point coordinates of the interface are not configured in application.yml, the default class implementation is used.
[0037] (4) Call registry.registerBeanDefinition(proxy.getBeanName(), proxy.getBeanDefinition()) to register the proxy BeanDefinition.
[0038] 3.3. Dynamically switch execution class By configuring the platform or monitoring the configuration file, the proxy bean can be switched dynamically when the program is running. The key value in the configuration information is the interface name annotated with ExtensionApi. The corresponding ExtensionProxyFactoryBean instance is obtained through the interface name, and then the execution class to be replaced is obtained through the Value value of the configuration information. The original BeanDefinition of the execution class is found and passed to the ExtensionProxyFactoryBean instance using the setTargetBeanName method to create a proxy instance of the corresponding execution class. When the execution method is called, the method of the replacement class will be executed. The goal of dynamically switching the execution class is achieved.
[0039] In order to prevent concurrency conflicts during dynamic switching, a locking mechanism can be implemented during the operation.
[0040] like Figure 1 As shown in the figure, if an interface has 4 implementation classes, the container will create 4 BeanDefinitions by default. Through the above operations, we will set the Primary attribute of these 4 original BeanDefinitions to false. At the same time, a proxy BeanDefintion will be created and the Primary attribute will be set to true. Therefore, the beans created by the proxy BeanDefintion will be injected first. When the bean method is actually called, the method of the actual class will be called through AOP and proxy technology, so as to achieve the effect of transparent dynamic replacement of beans.
[0041] The examples in the actual business development platform are as follows: In the specific development process, a self-developed OA office product software has an AuthorController class that provides API interface services to the outside world. The front-end or third-party software obtains data by calling this interface. Within the AuthorController, services are provided by calling AuthorService. With long-term research and development and in-depth understanding of the business, there are three implementation classes for the AuthorService interface: Biz1AuthorServiceImpl, Biz2AuthorServiceImpl, and Biz3AuthorServiceImpl, which respectively meet the business logic of different business scenarios in business scenario 1, business scenario 2, and business scenario 3. By default, Biz1AuthorServiceImpl is used to provide external services. In the actual software product deployment process, you may encounter the following situations 1: Deploy customer A, and use the Biz3AuthorServiceImpl class to meet customer needs 2: Deploy customer B. The three solutions provided by the product cannot meet customer needs, and CustomBizAuthorServiceImpl needs to be customized and developed.
[0042] 3: Deployment Customer C hopes to be able to switch business scenarios without stopping the product while it is running, that is, to be able to switch from Biz1AuthorServiceImpl to Biz2AuthorServiceImpl class, or vice versa, at any time.
[0043] In all three cases above, the code of the AuthorController class and the existing AuthorService implementation class should not be modified. Dynamic extension and dynamic injection can be achieved through simple annotations and configurations without changing the previous development paradigm and coding habits.
[0044] By using this method of the present invention, we can dynamically add new Bean definitions or modify existing Bean definitions before the Spring container is fully started, for example, to determine which implementation class to use based on the value in the configuration file, or to adjust the properties of the Bean based on certain runtime conditions. This mechanism provides great flexibility and scalability for Spring applications.
[0045] The business development platform plays an important role in business information development and has a wide range of applications. It can be used in the hydropower and water conservancy industry. For example, this platform can aggregate capabilities such as dam design, hydropower plant design, and irrigation system design. It can also be used for engineering planning and design, simulation and optimization of solutions, data analysis and decision-making in the hydropower and water conservancy field. It can also be used for software development in other fields to meet different business scenarios.
[0046] The above are only preferred technical solutions of the present invention. It should be pointed out that ordinary technicians in this technical field can make several improvements without departing from the principles of the present invention. These improvements should also be regarded as the protection scope of the present invention.
Claims
1. A method for customizing business components in a business development platform, characterized by: The steps include: Step 1: Add annotations to the Extension family First define @ExtensionApi and @Extension annotations, Then add the @ExtensionApi annotation to the AuthorService interface. Add the @Extension annotation to the implementation class and indicate the extension point coordinates; Step 2: Configure Define the extension point coordinates to be used in application.yml. Step 3: Implement automatic configuration Step 3.
1. Automatically create components Create a configuration class ExtensionAutoConfiguration with @Configuration annotation Step 3.
2. Extension point implementation, customized operation Implement a Springboot extension point BeanDefinitionRegistryPostProcessor interface through the ExtensionBeanDefinitionEnhancer component, which is used to enhance the BeanDefinition after the Spring container is initialized and before it is refreshed; After all BeanDefinitions are loaded and before Beans are instantiated, BeanDefinitions can be customized by implementing the BeanDefinitionRegistryPostProcessor interface; Step 3.
3. Dynamically switch execution class Dynamically switch proxy beans at runtime by configuring the platform or monitoring configuration files; Get the ExtensionProxyFactoryBean instance, and use the Value of the configuration information to get the execution class to be replaced; update the FactoryBean instance through the setTargetBeanName method to create a new proxy instance.
2. The method according to claim 1, characterized in that: In step 3.1, create a configuration class ExtensionAutoConfiguration with the @Configuration annotation. The class annotated with @Configuration is used to automatically configure the ExtensionBeanDefinitionEnhancer component. Spring Boot automatically loads this class according to the configuration in the class path at startup.
3. The method according to claim 1, characterized in that: In step 3.2, the postProcessBeanDefinitionRegistry() method is provided to allow BeanDefinitionRegistry to add, delete, modify and query BeanDefinition.
4. The method according to claim 1 or 3, characterized in that: In step 3.2, the specific process of customization operation is as follows: (1) Get all BeanDefinition Get all registered BeanDefinitions through BeanDefinitionRegistry; (2) Determine and process BeanDefinition For each BeanDefinition, check if there is an @Extension annotation; If yes, get the extension point coordinates bizId attribute, and decide which implementation class instance to use based on the coordinates and configuration; Traverse all interfaces implemented by the class to determine whether there is an @ExtensionApi annotation. If so, establish an association between the interface, implementation class, and bizId; (3) Traverse the association relationship and process it For each interface annotated with @ExtensionApi, traverse all associated BeanDefinitions. For each interface annotated with @ExtensionApi, traverse all associated beanDefintions and set the setPrimary of the original beanDefintion to false, indicating that it will not be injected; If the bizId annotated in the original beanDefinition is the same as the extension point coordinates configured in application.yml, create a proxy BeanDefinition to indicate that this class needs to be injected; associate the ExtensionProxyFactoryBean class as its instantiated bean, and rename the beanName to the prefix "extensionedTarget." + the original beanName; finally, setPrimary to true to ensure that the bean instance created by this BeanDefinition will be injected into the caller; (4) Call registry.registerBeanDefinition(proxy.getBeanName(), proxy.getBeanDefinition()) to register the proxy BeanDefinition.
5. The method according to claim 4, characterized in that: In step (2), establish the association between the interface, implementation class and bizId and store it in Map <interface, Map<bizId, classImp> > in.
6. The method according to claim 4, characterized in that: In step (3), in Spring Boot, if a class is marked as primary, it will be injected first.
7. The method according to claim 6, characterized in that: In step (3), if the extension point coordinates of the interface are not configured in application.yml, the default class implementation is used.
8. The method according to claim 1, characterized in that: In step 3.3, during dynamic switching, a locking mechanism is used to prevent concurrency conflicts.