vernacular AOP

After working for more than a year, I watched the AOP lecture by Mr. Han Shunping for the second time (11 years of Spring teaching video)

AOP is still a relatively difficult thing. From the beginning of the Java project to look for books, to learn the interceptor and know that AOP is the mechanism for dealing with transactions, logs, security and other things, but still do not understand those terms.

 

content:

1. Start with eliminating duplicate code.

1.5 to focus on business

2. Graphical AOP

2.5 Design patterns can be done without AOP

3. The concept of human speech

 

1. Spring's official documentation introduces AOP as "Let us understand some core concepts of AOP", and it will confuse you with the concept: what aspect, dynamic proxy, join point, entry point, target object

  That's how Xiaobai was scared away (laughs), and he was talking about me.

  Starting from a simpler, more accessible perspective (aspect?), let's introduce AOP from a "duplicate code" perspective.

  In the highly regarded books <Refactoring: Improving the Design of Existing Code> and <How to Clean Code>, duplicate code is a sin. "No duplicate code" is second only to "pass All tests".

  (Note: Criteria for evaluating clean code 1. Pass all tests 2. No duplicate code 3. Reflect all design concepts in the system 4. Include as few entities (classes, methods, functions) as possible)

  

  Yes, AOP is a means of eliminating duplicate code.

  For example: A service needs a transaction, and B service also needs to open a transaction.

Class ServiceA {
   void methodA() {
     // Open the transaction
   // work
    // commit transaction 
  }   
}

  The same code is also available in ServiceB.

  This has a bad smell. The two method bodies are XAY and XBY. Obviously, this has repeated and violated the DRY principle (dont repeat yourself).  

  Here comes AOP.

      1. AOP extracts the "duplicate code" of transactions from ServiceA and ServiceB, and puts the duplicate code in only one place (in another class).

      2. AOP puts the extracted items back

   Through such a wave of operations, the repeated code (which can make logging, switching transactions, authentication, etc.) has been separated and there is no repetition.

 

1.5 How to focus on business?

  Trivial non-business code: operations such as logging, authentication, and transactions are not directly related to business code. By means of extraction, they are separated from business code, thereby separating the complexity of business.

       The original code becomes

 void methodA() {
     // Open the transaction    // work
    // commit transaction  }

       Only one line of business code, more convenient to read

 

 

2. Graphical AOP

  From the perspective of eliminating repetitive code, we can have a perceptual understanding of AOP. Then look at the picture to learn the principle of AOP.

   

 

2.5 Design patterns can be done without AOP

   If you just use the extraction of non-business code to separate the complexity , in fact, the design pattern is doing this.

  Template method pattern

   Even directly extracting the method to separate the non-business code into a method can achieve the same effect.

   Not necessarily AOP to achieve.

 

3. The concept of human speaking

  Finally, talk about the concepts in AOP

       Aspect: Aspect: log, transaction, authentication

       Target object: target: put business code

       Proxy object: dynamic proxy to target

       Advice:Advice: non-business code, used to weave into business code

       Weaving: weave puts advice into join points in proxy objects

       Joint point: The location where notification code can be inserted

       Insertion point Pointcut: It is called a connection point when it is not inserted, and it is called an insertion point after insertion.

                                    The insertion point and the join point just look at the problem from different angles

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324841753&siteId=291194637
AOP