Aspect-oriented programming-based class implementation method and device, equipment and medium

By using proxy objects in aspect-oriented programming and the custom annotation @DI technology, the problems of cumbersome code writing and high coupling in existing technologies are solved, enabling simple and clear business code writing and independent execution of multiple implementation classes.

CN115658187BActive Publication Date: 2026-06-19HANGZHOU DBAPPSECURITY CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
HANGZHOU DBAPPSECURITY CO LTD
Filing Date
2022-10-20
Publication Date
2026-06-19

AI Technical Summary

Technical Problem

Existing technologies require defining implementation class names and injecting interface calls multiple times during project development, resulting in cumbersome code writing and high coupling, making it impossible to guarantee the independent execution of multiple business methods.

Method used

Aspect-Oriented Programming (AOP) technology is adopted to implement interfaces through proxy objects. The custom annotation @DI is used to collect and inject configuration information of multiple implementation classes and perform reflection calls to reduce code coupling.

Benefits of technology

It enables simple and clear business code writing, reduces the coupling between code and business logic, and supports flexible injection and independent execution of multiple implementation classes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115658187B_ABST
    Figure CN115658187B_ABST
Patent Text Reader

Abstract

This application discloses a class implementation method, apparatus, device, and medium based on aspect-oriented programming (AOP), relating to the field of Spring framework technology. The method includes: receiving an operation request sent by a user terminal and determining the target interface to be called in the operation request; wherein the target interface is an interface that uses a proxy object to implement the calling of different types of classes based on AOP; collecting pre-built custom annotations for determining the class based on the target interface, and performing dependency injection on the proxy object to obtain the configuration information of the custom annotations; when the proxy object is called, parsing the configuration information and performing reflection based on the attribute values ​​in the parsed configuration information to determine the target class to be called. Through the technical solution of this application, the coupling between code and business logic can be reduced, and multiple classes can be implemented based on a single interface.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of Spring framework technology, and in particular to a class implementation method, apparatus, device and medium based on aspect-oriented programming. Background Technology

[0002] In actual project development, various business scenarios exist. Taking the notification scenario as an example, if user A submits a work order, user B needs to be notified. There are multiple notification methods: SMS, DingTalk, email, etc. Depending on the requirements, different notification methods can be customized; it might be one method, two methods, or even all methods. Similar scenarios include export functions, such as those that support exporting to Word, Excel, PDF, and HTML formats.

[0003] Let's take the business scenario of sending notifications as an example for processing, such as... Figure 1 As shown, the prerequisite is the definition of a notification delivery interface (NoticeService), which all notification methods implement. Furthermore, in existing technologies, to select the notification method based on requirements, aliases for the implementation classes need to be defined. This allows the corresponding notification method to be derived from known custom configurations, and then the methods of different notification implementation classes are called sequentially to deliver the notification, such as... Figure 2 As shown. While existing techniques can achieve the desired result by injecting multiple implementation classes of an interface and then sequentially calling their respective notification methods, this approach has several drawbacks: First, ensuring the simultaneous injection of multiple implementation classes of an interface requires defining aliases for each class; otherwise, a compilation error will occur during project startup. Second, defining aliases for different implementation classes, importing multiple classes, and then calling their notification methods makes the code cumbersome. Furthermore, during program execution, notifications are sent sequentially; if an earlier notification method encounters an error, subsequent methods cannot execute, resulting in high code coupling.

[0004] In summary, how to simplify the writing of business logic code during the implementation process and reduce the coupling between code and business logic is a problem that needs to be solved. Summary of the Invention

[0005] In view of this, the purpose of this invention is to provide a class implementation method, apparatus, device, and medium based on aspect-oriented programming, which enables simple writing of business code during class implementation and reduces the coupling between code and business logic. The specific solution is as follows:

[0006] Firstly, this application discloses a class implementation method based on aspect-oriented programming, including:

[0007] Receive an operation request sent by a user terminal and determine the target interface to be called in the operation request; wherein, the target interface is an interface that uses a proxy object to implement the calling of different types of classes based on aspect-oriented programming;

[0008] Based on the target interface, pre-built custom annotations for determining the class are collected, and dependency injection is performed on the proxy object to obtain the configuration information of the custom annotations;

[0009] When the proxy object is invoked, the configuration information is parsed and a reflection call is performed based on the attribute values ​​in the parsed configuration information to determine the target class that needs to be invoked.

[0010] Optionally, before collecting pre-built custom annotations for determining the class based on the target interface, the method further includes:

[0011] Define a custom annotation class and implement the MergedBeanDefinitionPostProcessor interface based on the custom annotation class;

[0012] Based on the MergedBeanDefinitionPostProcessor interface, the postProcessMergedBeanDefinition method is overridden to obtain the custom annotation.

[0013] Optionally, the step of performing dependency injection on the proxy object to obtain the configuration information of the custom annotation includes:

[0014] The InstantiationAwareBeanPostProcessorAdapter class is inherited based on the custom annotation class, and the postProcessProperties method is overridden according to the inherited custom annotation class to achieve dependency injection to the proxy object.

[0015] Optionally, when the proxy object is invoked, the configuration information is parsed and a reflection call is performed based on the attribute values ​​in the parsed configuration information to determine the target class to be invoked, including:

[0016] When the proxy object is invoked, the configuration information is parsed and the value used to determine the selection method of the class and the serviceIds value used to specify the class by array are obtained from the parsed configuration information.

[0017] Based on the value and the serviceIds value, a reflection call is performed to determine the target class that needs to be called.

[0018] Optionally, the step of performing reflection invocation based on the value of the attribute type to determine the target class to be invoked includes:

[0019] If the value is "all", then all classes in the custom annotation will be directly identified as the target class.

[0020] If the value is assign, then the target class to be called is determined based on the value of the array in serviceIds.

[0021] Optionally, the class implementation method based on aspect-oriented programming further includes:

[0022] When a new class is added to the class, the custom annotation is updated and dependency injection is performed on the proxy object to obtain the updated configuration information of the updated custom annotation;

[0023] Accordingly, when the proxy object is invoked, the configuration information is parsed and a reflection call is performed based on the attribute values ​​in the parsed configuration information to determine the target class to be invoked, including:

[0024] When the proxy object is invoked, the updated configuration information is parsed and a reflection call is performed based on the attribute values ​​in the parsed updated configuration information to determine the target class that needs to be invoked.

[0025] Optionally, when the proxy object is invoked, after parsing the configuration information and performing reflection based on the attribute values ​​in the parsed configuration information to determine the target class to be invoked, the following steps are included:

[0026] When the proxy object is invoked, the configuration information is parsed and the handler is used to make a reflection call based on the attribute values ​​in the parsed configuration information to determine the target class that needs to be called.

[0027] Secondly, this application discloses a class implementation device based on aspect-oriented programming, comprising:

[0028] The target interface determination module is used to receive an operation request sent by a user terminal and determine the target interface to be called in the operation request; wherein, the target interface is an interface that uses a proxy object to implement the calling of different types of classes based on aspect-oriented programming;

[0029] An annotation collection module is used to collect pre-built custom annotations for determining the class based on the target interface;

[0030] The dependency injection module is used to perform dependency injection on the proxy object to obtain the configuration information of the custom annotation;

[0031] The target class determination module is used to parse the configuration information and perform reflection calls based on the attribute values ​​in the parsed configuration information when the proxy object is invoked, so as to determine the target class to be invoked.

[0032] Thirdly, this application discloses an electronic device, which includes a processor and a memory; wherein the memory is used to store a computer program, which is loaded and executed by the processor to implement the class implementation method based on aspect-oriented programming as described above.

[0033] Fourthly, this application discloses a computer-readable storage medium for storing a computer program; wherein the computer program, when executed by a processor, implements the class implementation method based on aspect-oriented programming as described above.

[0034] In this application, an operation request sent by a user terminal is received, and the target interface to be called in the operation request is determined. The target interface is an interface that uses a proxy object to implement calls to different types of classes based on aspect-oriented programming. Based on the target interface, pre-built custom annotations for determining the class are collected, and dependency injection is performed on the proxy object to obtain the configuration information of the custom annotations. When the proxy object is called, the configuration information is parsed, and a reflection call is performed based on the attribute values ​​in the parsed configuration information to determine the target class to be called. It can be seen that after determining the target interface to be called in the business scenario, a custom annotation is defined to enhance interface injection, and multiple implementation classes are injected into it. Custom annotations are collected during code execution. After the annotation collection is complete, dependency injection needs to be performed based on the collected annotations, because there may be multiple implementation classes, and dependency injection can only be performed using a proxy object. In this way, when the proxy object is called, a reflection call is performed based on the configuration information of the custom annotation obtained through dependency injection, achieving the injection of multiple implementation classes of the same interface at once, making the writing of business code very simple and clear. This allows for the replacement of previously complex and redundant code, and eliminates concerns about multiple business processes failing and preventing other business processes from functioning properly, thus reducing the coupling between code and business logic. Attached Figure Description

[0035] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.

[0036] Figure 1 This application discloses an interface and its implementation class architecture diagram.

[0037] Figure 2 This is a flowchart of a prior art processing method disclosed in this application;

[0038] Figure 3 This is a flowchart of a class implementation method based on aspect-oriented programming disclosed in this application;

[0039] Figure 4 This is an ideal operation flowchart disclosed in this application;

[0040] Figure 5 This application discloses a specific class implementation method based on aspect-oriented programming (AOP).

[0041] Figure 6 This is a flowchart of a method for collecting annotations disclosed in this application;

[0042] Figure 7 This is an execution flowchart of a dependency injection method disclosed in this application;

[0043] Figure 8 This application discloses a reflection invocation flowchart;

[0044] Figure 9 This is a schematic diagram of a class implementation device structure based on aspect-oriented programming disclosed in this application;

[0045] Figure 10 This is a structural diagram of an electronic device disclosed in this application. Detailed Implementation

[0046] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0047] Currently, in implementing different classes through interfaces in different scenarios, multiple implementation classes of the interface are injected multiple times, and then each implementation class is called sequentially to satisfy the selection operation in the current scenario. However, this approach not only requires defining aliases for the implementation classes, but also importing multiple implementation classes after defining the aliases, and finally calling different implementation classes, making the code writing quite cumbersome. In addition, during program execution, notifications are sent out sequentially. If the preceding notification sending method encounters an error, the subsequent methods cannot be executed, resulting in high code coupling.

[0048] To address this, this application provides a class implementation scheme based on aspect-oriented programming, which allows for simple writing of business code during class implementation, reducing the coupling between code and business logic.

[0049] This invention discloses a class implementation method based on aspect-oriented programming. See [link to relevant documentation]. Figure 3 As shown, the method includes:

[0050] Step S11: Receive the operation request sent by the user terminal and determine the target interface to be called in the operation request; wherein, the target interface is an interface that uses a proxy object to implement the calling of different types of classes based on aspect-oriented programming.

[0051] In this embodiment of the application, the sending of notification services is used as an example to illustrate the embodiment of the application. Subsequent embodiments will refer to this scenario and will not be described in detail again.

[0052] In existing technologies, different aliases are defined for different implementation classes, and then interfaces are injected multiple times and called sequentially to send notifications according to requirements. This is not only cumbersome in terms of code writing but also results in high coupling. Therefore, by analyzing the shortcomings of existing technologies, the most ideal workflow is to directly call the notification sending method, such as... Figure 4 As shown.

[0053] Figure 4 The operation flow described is the simplest, most direct, and clearest. When writing the code, there's no need to worry about the different notification methods configured; you only need to directly call the notification sending interface. Therefore, in this embodiment, in the current scenario, when an operation request is received from a user terminal, if the user needs to send a notification, the target interface for sending the notification is determined, and then the notification sending method is called by calling that interface. Writing the business code is very simple and clear, replacing the originally complex and redundant code.

[0054] In this embodiment, the target interface for sending notifications is written based on Aspect-Oriented Programming (AOP), a technique that uses pre-compilation and runtime dynamic proxies to achieve unified maintenance of program functionality. AOP is a continuation of OOP, a hot topic in software development, and an important component of the Spring framework; it is a derivative paradigm of functional programming. AOP can isolate different parts of business logic, thereby reducing coupling between them, improving program reusability, and increasing development efficiency. Since the target interface is written based on AOP, when calling the notification sending method through the target interface, proxy objects are used to enhance the invocation of different types of classes.

[0055] Step S12: Based on the target interface, collect pre-built custom annotations for determining the class, and perform dependency injection on the proxy object to obtain the configuration information of the custom annotations.

[0056] In this embodiment of the application, in order to write business code simply and clearly, the target interface is directly called. However, when injecting the implementation class, the @Autowired or @Resource annotations cannot be used because currently, both @Autowired and @Resource annotations only support injecting one implementation of an interface at a time. It is not possible to inject multiple implementation classes through one interface. That is, if there is a need to select multiple notification methods, it cannot be achieved using the @Autowired or @Resource annotations.

[0057] Therefore, custom annotations are used in this application embodiment, and in one implementation, the custom annotation @DI is used. It is understood that annotations exist in the code with the annotation name marked @, and were introduced in JDK 1.5 and later versions. They can be used to create documentation, track dependencies in code, and even perform basic compile-time checks. Based on the number of annotation parameters, annotations can be divided into three categories: marker annotations, single-value annotations, and complete annotations. They do not directly affect the semantics of the program; they only exist as identifiers, and access to metadata is achieved through reflection mechanisms in programming.

[0058] In this embodiment, the core of the custom annotation @DI consists of two attributes: value and serviceIds. The value attribute determines whether to select all or specific classes. Typically, the default value for value is all, indicating all implementation classes to be injected; it can also be assign, which, when combined with serviceIds, specifies which implementation classes to inject. serviceIds is an array where the bean names of the implementation classes to be injected can be written. In this way, the annotation supports finding all implementation classes, finding a single implementation class, or prioritizing a specific implementation class based on the value of the value field.

[0059] In this embodiment, after determining that custom annotations are used in the program code and collecting them, dependency injection of the proxy object is completed based on the collected annotations. That is, after dependency injection of the proxy object, the configuration information of the custom annotations can be obtained, which is equivalent to putting classes with dependencies into a container and resolving instances of these classes, thus achieving class decoupling. It should be noted that dependency injection is performed on the proxy object because there may be multiple implementation classes; therefore, only the proxy object can be used for dependency injection. This ensures that reflection invocation will be performed based on the @DI configuration information when the proxy object is called.

[0060] Step S13: When the proxy object is invoked, the configuration information is parsed and a reflection call is performed based on the attribute values ​​in the parsed configuration information to determine the target class to be invoked.

[0061] In this embodiment, when invoking the notification method, a proxy object is needed because multiple classes capable of issuing notifications may be invoked. Then, it's necessary to determine which classes to call, parsing the configuration information of the `@DI` custom annotation to obtain the values ​​of the `value` and `serviceIds` attributes. If `value` is `all`, it indicates that all classes are called; that is, all classes are directly identified as target classes during the reflection invocation process. If `value` is `assign`, it indicates that an implementation class is specified. In this case, the implementation classes configured in the `serviceIds` array are the implementation classes to be called; that is, the target class to be called is determined based on the values ​​in the `serviceIds` array. Further, after determining the target class to be called, the corresponding handler is executed to invoke the notification method of the configured target class.

[0062] In this embodiment, when the proxy object is invoked, the configuration information is parsed, and a reflective invocation is performed using a handler based on the attribute values ​​in the parsed configuration information to determine the target class to be invoked. That is, after determining the value, the target class to be invoked is identified, and a reflective invocation is performed using the handler to execute the notification method, thus implementing the notification method for the target class.

[0063] In this application, an operation request sent by a user terminal is received, and the target interface to be called in the operation request is determined. The target interface is an interface that uses a proxy object to implement calls to different types of classes based on aspect-oriented programming. Based on the target interface, pre-built custom annotations for determining the class are collected, and dependency injection is performed on the proxy object to obtain the configuration information of the custom annotations. When the proxy object is called, the configuration information is parsed, and a reflection call is performed based on the attribute values ​​in the parsed configuration information to determine the target class to be called. It can be seen that after determining the target interface to be called in the business scenario, a custom annotation is defined to enhance interface injection, and multiple implementation classes are injected into it. Custom annotations are collected during code execution. After the annotation collection is complete, dependency injection needs to be performed based on the collected annotations, because there may be multiple implementation classes, and dependency injection can only be performed using a proxy object. In this way, when the proxy object is called, a reflection call is performed based on the configuration information of the custom annotation obtained through dependency injection, achieving the injection of multiple implementation classes of the same interface at once, making the writing of business code very simple and clear. This allows for the replacement of previously complex and redundant code, and eliminates concerns about multiple business processes failing and preventing other business processes from functioning properly, thus reducing the coupling between code and business logic.

[0064] This application discloses a specific class implementation method based on aspect-oriented programming. See [link to relevant documentation]. Figure 5 As shown, the method includes:

[0065] Step S21: Receive the operation request sent by the user terminal and determine the target interface to be called in the operation request; wherein, the target interface is an interface that uses a proxy object to implement the calling of different types of classes based on aspect-oriented programming.

[0066] For a more detailed description of the process of step S21, please refer to the relevant content disclosed in the foregoing embodiments, which will not be repeated here.

[0067] Step S22: Define a custom annotation class and implement the MergedBeanDefinitionPostProcessor interface based on the custom annotation class.

[0068] In this embodiment of the application, in order to inject multiple implementation classes of the same interface at one time, it is first necessary to pre-build a custom annotation. Specifically, define a custom annotation class. For example, if the custom annotation is defined as @DI, then define the DICannotationBeanPostProcessor class, and then implement the MergedBeanDefinitionPostProcessor interface based on the DICannotationBeanPostProcessor class.

[0069] Step S23: Based on the MergedBeanDefinitionPostProcessor interface, the postProcessMergedBeanDefinition method is overridden to obtain the custom annotation.

[0070] In this embodiment, the implementation of the Spring framework is referenced. Spring is an open-source J2EE application framework initiated by Rod Johnson. It is a lightweight container that manages the lifecycle of beans. It solves many common problems encountered by developers in J2EE development and provides powerful features such as IOC, AOP, and Web MVC.

[0071] It should be noted that the MergedBeanDefinitionPostProcessor interface has a postProcessMergedBeanDefinition method. Therefore, the postProcessMergedBeanDefinition method should be overridden based on the MergedBeanDefinitionPostProcessor interface.

[0072] In one specific implementation, referring to Spring's implementation, one can refer to the findAutowiringMetadata method to write the findDiMetadata method, which is an intermediate method used to handle annotation cache data; refer to the buildAutowiringMetadata method to write the buildDiMetadata method, which is used to collect annotations; refer to the AutowiredFieldElement inner class to write the DIFieldElement inner class, which is used for property injection; and refer to the AutowiredMethodElement inner class to write the DIMethodElement inner class, which is used for method injection, etc.

[0073] like Figure 6 The diagram shows a flowchart for collecting annotations. The process of collecting annotations is based on the Spring framework, and the AutowiredAnnotationBeanPostProcessor class completes the work of collecting annotations.

[0074] Step S24: Inherit the InstantiationAwareBeanPostProcessorAdapter class based on the custom annotation class, and override the postProcessProperties method according to the inherited custom annotation class to achieve dependency injection of the proxy object.

[0075] In Spring, objects that form the backbone of an application and are managed by the Spring IoC container are called beans. A bean is an object instantiated, assembled, and managed by the Spring IoC container. In this embodiment, Spring's BeanPostProcessor is used to enhance the properties of beans annotated with @DI, injecting multiple classes into them.

[0076] In this embodiment, dependency injection also references Spring's implementation, but the dependency injection uses a proxy object. We will first focus on the implementation of dependency injection, and then on the implementation of the proxy object. The execution flow of dependency injection is as follows: Figure 7 As shown, inherit the InstantiationAwareBeanPostProcessorAdapter class and override the postProcessProperties method. Then, perform dependency injection based on the findDiMetadata method and / or buildDiMetadata method written in the custom annotation, and / or the DIFieldElement inner class, and / or the DIMedoElement inner class.

[0077] In this embodiment, because dependency injection involves a proxy object, a proxy object must be generated before dependency injection can be implemented. To inject the proxy object, it must first be generated; specifically, an `InvokeProxy` class is defined, and a `newProxy()` function is written to create a new proxy object. Figure 7As can be seen, dependency injection calls the `metadata.inject()` method. It's important to note that injection can be divided into field injection and method injection, corresponding to the inner classes `DIFieldElement` and `DIMethodElement`. In fact, dependency injection calls the `DIFieldElement.inject()` and `DIMethodElement.inject()` methods. That is, because different inner classes are defined when using custom annotations, including the inner classes `DIFieldElement` and `DIMethodElement`, the `DIFieldElement.inject()` and `DIMethodElement.inject()` methods will also be called during dependency injection.

[0078] Step S25: When a new class is added to the class, update the custom annotation and perform dependency injection on the proxy object to obtain the updated configuration information of the updated custom annotation.

[0079] In this embodiment, if new business functions are added, business expansion is relatively convenient, as it does not require updating the business code; only the configuration of the @DI annotation needs to be updated. That is, when a new class is added to implement notification delivery, the updated configuration information of the custom annotation can be obtained during dependency injection.

[0080] Step S26: When the proxy object is invoked, the updated configuration information is parsed and a reflection call is performed based on the attribute values ​​in the parsed updated configuration information to determine the target class to be invoked.

[0081] In this embodiment of the application, when performing a reflection call, if the configuration information of the custom annotation is updated, the updated configuration information is parsed, and after determining the target class to be called, the corresponding handler is executed to call the notification method of the configured target class.

[0082] like Figure 8The diagram shows the flowchart of reflection invocation. When invoking the notification method, since multiple implementation classes' notification methods may be invoked, a proxy object is needed. The proxy object is then executed, parsing the configuration information of the `@DI` annotation to obtain its attributes. If the value is `all`, all implementation classes are invoked; if the value is `assign`, the implementation class to be invoked is determined based on the configuration in the `serviceIds` array. After determining the value, the corresponding handler is executed to invoke the notification method of the configured implementation class. By combining these steps, a method for invoking multiple implementation classes of the notification interface at once is implemented. The logic is clear, the operation is simple, and the extensibility is high.

[0083] In this application, an operation request sent by a user terminal is received, and the target interface to be called in the operation request is determined. The target interface is an interface that uses a proxy object to call different types of classes based on aspect-oriented programming. A custom annotation class is defined, and the MergedBeanDefinitionPostProcessor interface is implemented based on the custom annotation class. The postProcessMergedBeanDefinition method is overridden based on the MergedBeanDefinitionPostProcessor interface to obtain the custom annotation. The InstantiationAwareBeanPostProcessorAdapter class is inherited by the custom annotation class, and the postProcessProperties method is overridden according to the inherited custom annotation class to achieve dependency injection to the proxy object. When a new class is added to the class, the custom annotation is updated, and dependency injection is performed on the proxy object to obtain the updated configuration information of the updated custom annotation. When the proxy object is called, the updated configuration information is parsed, and reflection is performed based on the attribute values ​​in the parsed updated configuration information to determine the target class to be called. As we can see, after identifying the target interface to be called in the business scenario, a custom annotation is used to inject multiple implementation classes to enhance interface injection. This custom annotation is collected during code execution. After collection, dependency injection needs to be performed based on the collected annotations. Since there may be multiple implementation classes, dependency injection can only be performed using a proxy object. In this way, when the proxy object is called, reflection is used based on the configuration information of the custom annotation obtained through dependency injection, achieving the simultaneous injection of multiple implementation classes of the same interface. Writing business code is very simple and clear. This replaces the originally complex and redundant code, and eliminates concerns about other business processes failing due to the failure of one business process, reducing the coupling between code and business logic.

[0084] Accordingly, embodiments of this application also disclose a class implementation device based on aspect-oriented programming, see [link to relevant documentation]. Figure 9 As shown, the device includes:

[0085] The target interface determination module 11 is used to receive an operation request sent by a user terminal and determine the target interface to be called in the operation request; wherein, the target interface is an interface that uses a proxy object to implement the calling of different types of classes based on aspect-oriented programming;

[0086] Annotation collection module 12 is used to collect pre-built custom annotations for determining the class based on the target interface;

[0087] Dependency injection module 13 is used to perform dependency injection on the proxy object to obtain the configuration information of the custom annotation;

[0088] The target class determination module 14 is used to parse the configuration information and perform reflection calls based on the attribute values ​​in the parsed configuration information when the proxy object is called, so as to determine the target class to be called.

[0089] For more detailed information on the working process of each of the above modules, please refer to the relevant content disclosed in the foregoing embodiments, which will not be repeated here.

[0090] Therefore, the above-described scheme in this embodiment receives an operation request sent by a user terminal and determines the target interface to be called in the operation request. The target interface is an interface that uses a proxy object to implement calls to different types of classes based on aspect-oriented programming. Based on the target interface, pre-built custom annotations for determining the class are collected, and dependency injection is performed on the proxy object to obtain the configuration information of the custom annotations. When the proxy object is called, the configuration information is parsed, and a reflection call is performed based on the attribute values ​​in the parsed configuration information to determine the target class to be called. It is evident that after determining the target interface to be called in the business scenario, a custom annotation is defined to enhance interface injection, and multiple implementation classes are injected into it. Custom annotations are collected during code execution. After the annotation collection is complete, dependency injection needs to be performed based on the collected annotations, because there may be multiple implementation classes, and dependency injection can only be performed using a proxy object. In this way, when the proxy object is called, a reflection call is performed based on the configuration information of the custom annotation obtained through dependency injection, achieving the injection of multiple implementation classes of the same interface at once. Writing business code is very simple and clear. This allows for the replacement of previously complex and redundant code, and eliminates concerns about multiple business processes failing and preventing other business processes from functioning properly, thus reducing the coupling between code and business logic.

[0091] Furthermore, embodiments of this application also disclose an electronic device, Figure 10 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content of the diagram should not be construed as limiting the scope of this application.

[0092] Figure 10 This is a schematic diagram of the structure of an electronic device 20 provided in an embodiment of this application. Specifically, the electronic device 20 may include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 stores a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the class implementation method based on aspect-oriented programming disclosed in any of the foregoing embodiments. Alternatively, the electronic device 20 in this embodiment may specifically be a computer.

[0093] In this embodiment, the power supply 23 is used to provide operating voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and external devices, and the communication protocol it follows can be any communication protocol applicable to the technical solution of this application, and is not specifically limited here; the input / output interface 25 is used to acquire external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs, and is not specifically limited here.

[0094] In addition, the memory 22, as a carrier for resource storage, can be a read-only memory, random access memory, disk, or optical disk, etc. The resources stored on it can include an operating system 221, computer programs 222, and data 223, etc. The data 223 can include various types of data. The storage method can be temporary storage or permanent storage.

[0095] The operating system 221 is used to manage and control the various hardware devices on the electronic device 20 and the computer program 222, which may be Windows Server, Netware, Unix, Linux, etc. In addition to including computer programs capable of performing the class implementation method based on aspect-oriented programming disclosed in any of the foregoing embodiments, the computer program 222 may further include computer programs capable of performing other specific tasks.

[0096] Furthermore, this application also discloses a computer-readable storage medium, which includes random access memory (RAM), main memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disks, magnetic disks, optical disks, or any other form of storage medium known in the art. The computer program, when executed by a processor, implements the aforementioned class implementation method based on aspect-oriented programming. Specific steps of this method can be found in the corresponding content disclosed in the foregoing embodiments, and will not be repeated here.

[0097] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to in the method section.

[0098] The steps of the class implementation or algorithm based on aspect-oriented programming described in conjunction with the embodiments disclosed herein can be implemented directly by hardware, a software module executed by a processor, or a combination of both. The software module can be located in random access memory (RAM), main memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium known in the art.

[0099] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0100] The foregoing has provided a detailed description of a class implementation method, apparatus, device, and medium based on aspect-oriented programming provided by the present invention. Specific examples have been used to illustrate the principles and implementation methods of the present invention. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of the present invention. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of the present invention. Therefore, the content of this specification should not be construed as a limitation of the present invention.

Claims

1. A method for implementing a class based on aspect-oriented programming, characterized in that, include: Receive an operation request sent by a user terminal and determine the target interface to be called in the operation request; wherein, the target interface is an interface that uses a proxy object to implement the calling of different types of classes based on aspect-oriented programming; Based on the target interface, pre-built custom annotations for determining the class are collected, and dependency injection is performed on the proxy object to obtain the configuration information of the custom annotations; When the proxy object is invoked, the configuration information is parsed and a reflection call is performed based on the attribute values ​​in the parsed configuration information to determine the target class that needs to be invoked. Before collecting pre-built custom annotations for determining the class based on the target interface, the method further includes: Define a custom annotation class and implement the MergedBeanDefinitionPostProcessor interface based on the custom annotation class; Based on the MergedBeanDefinitionPostProcessor interface, the postProcessMergedBeanDefinition method is overridden to obtain the custom annotation.

2. The method of claim 1, wherein, The process of performing dependency injection on the proxy object to obtain the configuration information of the custom annotation includes: The InstantiationAwareBeanPostProcessorAdapter class is inherited based on the custom annotation class, and the postProcessProperties method is overridden according to the inherited custom annotation class to achieve dependency injection to the proxy object.

3. The class implementation method based on aspect-oriented programming according to claim 1, characterized in that, When the proxy object is invoked, the configuration information is parsed and a reflection call is performed based on the attribute values ​​in the parsed configuration information to determine the target class to be invoked, including: When the proxy object is invoked, the configuration information is parsed and the value used to determine the selection method of the class and the serviceIds value used to specify the class by array are obtained from the parsed configuration information. Based on the value and the serviceIds value, a reflection call is performed to determine the target class that needs to be called.

4. The class implementation method based on aspect-oriented programming according to claim 3, characterized in that, The step of performing a reflection call based on the value of the attribute type to determine the target class to be called includes: If the value is "all", then all classes in the custom annotation will be directly identified as the target class. If the value is assign, then the target class to be called is determined based on the value of the array in serviceIds.

5. The class implementation method based on aspect-oriented programming according to claim 1, characterized in that, Also includes: When a new class is added to the class, the custom annotation is updated and dependency injection is performed on the proxy object to obtain the updated configuration information of the updated custom annotation; Accordingly, when the proxy object is invoked, the configuration information is parsed and a reflection call is performed based on the attribute values ​​in the parsed configuration information to determine the target class to be invoked, including: When the proxy object is invoked, the updated configuration information is parsed and a reflection call is performed based on the attribute values ​​in the parsed updated configuration information to determine the target class that needs to be invoked.

6. The class implementation method based on aspect-oriented programming according to any one of claims 1 to 5, characterized in that, When the proxy object is invoked, the configuration information is parsed and a reflection call is performed based on the attribute values ​​in the parsed configuration information to determine the target class to be invoked, including: When the proxy object is invoked, the configuration information is parsed and the handler is used to make a reflection call based on the attribute values ​​in the parsed configuration information to determine the target class that needs to be called.

7. A class implementation device based on aspect-oriented programming, characterized in that, include: The target interface determination module is used to receive an operation request sent by a user terminal and determine the target interface to be called in the operation request; wherein, the target interface is an interface that uses a proxy object to implement the calling of different types of classes based on aspect-oriented programming; An annotation collection module is used to collect pre-built custom annotations for determining the class based on the target interface; The dependency injection module is used to perform dependency injection on the proxy object to obtain the configuration information of the custom annotation; The target class determination module is used to parse the configuration information and perform reflection calls based on the attribute values ​​in the parsed configuration information when the proxy object is called, so as to determine the target class to be called; The class implementation device based on aspect-oriented programming further includes: The interface implementation module is used to define custom annotation classes and implement the MergedBeanDefinitionPostProcessor interface based on the custom annotation classes; The annotation determination module is used to rewrite the postProcessMergedBeanDefinition method based on the MergedBeanDefinitionPostProcessor interface to obtain the custom annotation.

8. An electronic device, characterized in that, The electronic device includes a processor and a memory; wherein the memory is used to store a computer program, which is loaded and executed by the processor to implement the class implementation method based on aspect-oriented programming as described in any one of claims 1 to 6.

9. A computer-readable storage medium, characterized in that, Used to store computer programs; wherein the computer programs, when executed by a processor, implement the class implementation method based on aspect-oriented programming as described in any one of claims 1 to 6.