Get started with AOP and learn how to get started

Source: Internet
Author: User

Get started with AOP and learn how to get started

I have been hearing about AOP for a long time. After several days of data query, I 'd like to know something about it. His full name is Aspect Oriented Programming ). It is initially understood that AOP is an implementation of "high cohesion and low coupling", which implements the decoupling between the caller and the called.

The origins of AOP:

Assume that the system has three methods. Now we need to add the same code to all the three methods. You can add things, logs, and so on. It may start with the following implementation method:

However, if one day, the same code segment needs to be modified, you need to open the code in three places for modification. If not, instead, the code segment is contained in 100 or even 1000 places, so you may feel tired when you think about the consequences. So we made the following changes:

In this way, it seems that we have solved the above problem, and the above changes occur again. We only need to modify the proposed public method. However, if one day the customer wants to remove the above Code and I do not add transactions or logs, and I want to add permissions, do I still have to modify the called code of each method, it still violates the "closed development" principle of development. At this point, someone proposed that: as long as we define this method, we do not need to explicitly call it in method 1, method 2, and method 3. The system will "automatically" execute this special method, in this way, we can decouple. method 1, method 2, and method 3 can do their own work. So there is:

AOP framework

1. AspectJ

Static AOP is the most mature and complete AOP solution. It is an extension of java. It relies on the compiler to implement compiling-time weaving or loading-time weaving, which is difficult to build, however, the execution efficiency is high (direct operation of bytecode) and is not limited by the application type. You can select two methods: code and annotation, the code method is more powerful and flexible (the annotation method is only a subset of the functions of the code method ).

2. SpringAOP

Dynamic AOP: intercepts the target code through the interceptor, uses an automatic proxy to change the behavior of the target code, which is easy to construct and requires more work during runtime. The flexibility and granularity defined by the entry point is not as good as that defined by AspectJ, however, it is extremely easy to use and can be closely integrated with AspectJ to learn from each other. operations that cannot be well implemented can be seamlessly rolled back to AspectJ implementation, which is part of the Spring framework and implements Transaction Management for Spring.


Conclusion: AOP breaks the boundaries of object layers, brings together common concerns, and disconnects a single responsibility code to make the program structure clearer. For example, the same code function just now is very intrusive. The core business logic and its logic are mixed and difficult to update. The method of using AOP focuses on one point of calls, disconnects the same code, and reverses the dependence of the business logic on him. The existing business code can be completely ignored or even unaware of how it is implemented, this is the design principle of "single responsibility.


Related Article

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.