Easy to understand AOP ideas (aspect-oriented programming)

Source: Internet
Author: User

The purpose of this article is to help children who have not yet understood AOP understand AOP, and also welcome expert criticism.

First say a spring is what it is, everyone is it is a framework, but the framework of the word to the novice a little abstract, so that the more the more ambiguous interpretation, but it is really a framework, but that is defined from the perspective of the function, in essence, Spring is a library, a Java library, So I personally think that should be the answer to what spring is: Spring is a library, its function is to provide a software framework, the framework is designed to make the logic between the software clearer, more flexible configuration, the means to achieve this purpose using AOP and IOC, while AOP and IOC is an idea, What kind of thinking, and so on, first say that AOP in Java is the use of reflection mechanism implementation (you can also think of a dynamic agent, but dynamic agent is also reflected in the implementation of the mechanism, so do not control the dynamic agent, we are here to simplify the complex, not let it interfere with our understanding of AOP), How to use AOP, very simple drip, and so on the introduction.

First of all, what is the idea of AOP, we take a step-by-step, first look at the process of traditional procedures, such as the banking system will have a withdrawal process

We can put the process of the box into one, and the system will also have a query balance process, we first put these two processes together:

There is no discovery, the two have a same verification process, we first circled them to say the next step:

Have you ever thought that you can put this verification user's code is extracted, not put in the main process to go, this is the role of AOP, with AOP, you write code do not put this verification user step to write, that is, completely regardless of the authentication user, after you write, in another place, write a validation user's code, And then tell spring where you're going to add this code to, and spring will help you add it, not copy it yourself, here's two places, if you have multiple control flows, this way of writing code can greatly reduce your time, but the purpose of AOP is not, it's just a " Side Effects ", the real purpose is that you write code, in advance only to consider the main process, without regard to those unimportant process, understand C know, good style requirements at the beginning of the function to verify the parameters, if you can use AOP on C, you can first regardless of the validation parameters of the problem, Using AOP later, you can ge all functions with the check code once, and you only need to write the checksum code once. Do not know that C is ok, to give a general example, often in the debug log bar, you can also write the main code, write the code to another single place, and then command AOP to add your code over, notice that AOP does not add code to the source file, But it will correctly affect the final machine code.

Now probably understand the AOP, we come to the idea, the above box like a plane, you can take it as a board, the board inserted some control process, this board can be regarded as a facet of AOP. So the essence of AOP is in a series of vertical control flow, the same sub-process extracted into a horizontal surface, this sentence should be well understood, we draw the longitudinal process into a straight line, but the same part to the green prominent, such as the left, and AOP equivalent to the same place even a horizontal line, such as the right, this picture is not good, Everyone knows what it means.

This verification user this sub-process becomes a line, can also be understood as a section, aspect meaning I think is the aspect, you use what kind to analogy, as long as you can understand all can. There are only two or three processes in the section here, and if other processes require this sub-process, they can also be plugged into other places.

Reprinted from: https://my.oschina.net/yanquan345/blog/203415

Easy to understand AOP ideas (aspect-oriented programming)

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.