Spring AOP Implementation principle and Cglib application

Source: Internet
Author: User

AOP (Aspect Orient programming), as a complement to object-oriented programming, is widely used to deal with some cross-cutting system-level services, such as transaction management, security checks, caching, object pool management, etc. The key to AOP implementations is the AOP broker that the AOP framework automatically creates, AOP agents can be divided into static agents and dynamic proxies, where static proxies are compiled using the commands provided by the AOP framework to generate an AOP proxy class at compile time, which is also referred to as compile times enhancement, while dynamic proxies The runtime generates an AOP dynamic proxy class in memory "temporarily" with the help of JDK dynamic proxies, cglib, and so on, and is also known as Run-time enhancements.

The existence value of AOP

In traditional OOP programming, the object is the core, and the whole software system consists of interdependent objects, which are abstracted into one class and allow class inheritance to be used to manage the general to special relationships between classes and classes. With the increase of software scale, the application is gradually upgraded, and some problems that OOP is difficult to solve are slowly appearing.

We can analyze and abstract a series of objects with certain properties and behaviors, and form a complete software function through the collaboration among these objects. Because objects can inherit, we can abstract attributes that have the same functionality or same attributes into a hierarchical class-structured system. With the continuous expansion of the software specification, more and more specialized division of labor, as well as the increasing of OOP application practice, it also exposes some problems that OOP can't solve very well.

Now assume that there are 3 completely similar code in the system, which is usually done in a "copy", "Paste" way, and the software developed through this "copy" and "Paste" method is shown in Figure 1.

Figure 1 software that contains the same code in multiple places

Looking at the schematic shown in Figure 1, some readers may have discovered the drawbacks of this approach: if one day, the dark section of code in Figure 1 needs to be modified, is it necessary to open 3 local code to modify it? If not 3 places contain this code, but 100 places, or even 1000 places contain this piece of code, what would be the consequences?

To solve this problem, we typically define a method with the darker code section shown in Figure 1, and then call the method separately in 3 code snippets. In this way, the structure of the software system is shown in Figure 2.

Figure 2 Implementing system functionality through a method call

For the software system shown in Figure 2, if you need to modify the darker part of the code, as long as you modify a place, no matter how many places in the system call the method, the program does not need to modify these places, just modify the called method--in this way, greatly reduce the complexity of late maintenance software.

For Method 1, Method 2, Method 3, which is shown in Figure 2, you still need to explicitly call the Dark method, which solves most scenarios. But for some more special cases: application needs Method 1, Method 2, Method 3 thoroughly and dark method Separation--Method 1, Method 2, Method 3 do not need to call the dark method directly, how to solve?

Because the software system requirements change is very frequent things, the system design Method 1, Method 2, Method 3 when only the core business functions, after a period of time, we need to approach 1, Method 2, Method 3 all increase transaction control; After a period of time, the customer proposed Method 1, Method 2, Method 3 need to User Legal verification, only legitimate users can execute these methods; and after a while, the customer proposed Method 1, Method 2, Method 3 should increase the log record; and after a while, the client proposed ... In the face of such a situation, what shall we do? There are usually two ways to do this:

According to the requirement specification, directly rejects the customer request.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.