Understanding Spring AOP aspect-oriented programming

Table of contents

1. Aspect-oriented programming thinking (AOP)

2. Main application scenarios of AOP thinking

3. Introduction to AOP terminology

4. Introduction to Spring AOP framework and relationship sorting


1. Aspect-oriented programming thinking (AOP)

AOP: Aspect Oriented Programming Aspect-oriented programming

        AOP can be said to be the supplement and improvement of OOP (Object Oriented Programming). OOP introduces concepts such as encapsulation, inheritance, and polymorphism to establish an object hierarchy that is used to simulate a collection of public behaviors. However, OOP allows developers to define vertical relationships, but it is not suitable for defining horizontal relationships, such as logging functions. Logging code is often spread horizontally across all object hierarchies and has nothing to do with the core functionality of its corresponding object. The same is true for other types of code, such as security, exception handling, and transparent persistence. Irrelevant code everywhere is called cross cutting. In OOP design, it leads to the duplication of a large amount of code and is not conducive to the reuse of various modules.

        AOP technology is just the opposite. It uses a technology called "cross-cutting" to dissect the inside of the encapsulated object and encapsulate the public behaviors that affect multiple classes into a reusable module and name it " Aspect", that is, cut. The so-called "aspects" are simply encapsulated logic or responsibilities that have nothing to do with the business but are jointly called by the business modules, so as to reduce the duplication of code in the system, reduce the coupling between modules, and facilitate future operability. and maintainability.

OOP (Object Oriented Programming) understanding of OOP vertical programming:

  1. By inheriting the method call, the subclass can use the parent class method. This is vertical and vertical programming.
  2. Disadvantages: Local modification cannot be achieved, because the subclass has only two operating modes for the parent class's methods, one is to completely inherit the parent class's methods, and the other is to completely rewrite the parent class's methods. This means that if you want to add the same function to all methods, you need to manually add the code for the corresponding function to all methods one by one. This leads to code redundancy and maintenance difficulties.

 AOP (aspect-faced programming) understanding of AOP aspect programming

  1. Non-core business code can be inserted horizontally into business code. This is aspect-faced programming. The most critical operation is the extraction and insertion of redundant code.
  2. AOP solves the core problem: solving the redundant problem of non-core business code.

AOP and dynamic proxies

  1. The core of AOP object-oriented programming: Object-oriented programming is a simplification of dynamic agents. There are two implementation methods of dynamic agents in Java, one is JDK mode and the other is cglib mode, but the implementation of both modes is very complex.
  2. Why implement a dynamic proxy class? Because traditional OOP (object-oriented programming) has many shortcomings, for example, the output of an interface that implements calculations requires a large number of non-business operations, such as outputting information about incoming parameter values ​​and outputting result information. Each method requires these non-business operations, so in order to decouple and separate non-business and core business, dynamic agents need to be used to achieve this.

2. Main application scenarios of AOP thinking

AOP (Aspect-Oriented Programming) is a programming paradigm that separates common cross-cutting concerns (such as logging, transactions, permission control, etc.) from business logic, making the code clearer, more concise, and easier to maintain. AOP can be applied to various scenarios. The following are some common AOP application scenarios:

  1. Logging: It is very important to record logs in the system. AOP can be used to implement the logging function. Logs can be recorded before, after, or when an exception is thrown.
  2. Transaction processing: Using transactions in database operations can ensure data consistency. You can use AOP to implement transaction processing functions. You can start a transaction before the method starts, and commit or rollback the transaction after the method is completed.
  3. Security control: The system contains certain operations that require security control, such as login, password change, authorization, etc. AOP can be used to implement security control functions. Permissions can be judged before the method is executed. If the user does not have permissions, an exception will be thrown or redirected to an error page to prevent unauthorized access.
  4. Performance monitoring: During system operation, it is sometimes necessary to monitor the performance of certain methods to find system bottlenecks and optimize them. AOP can be used to implement the performance monitoring function. The timestamp can be recorded before the method is executed. After the method is executed, the method execution time is calculated and output to the log.
  5. Exception handling: Various exceptions may occur in the system, such as null pointer exceptions, database connection exceptions, etc. AOP can be used to implement the exception handling function. During method execution, if an exception occurs, exception handling (such as logging) , send emails, etc.).
  6. Cache control: Some data in the system can be cached to improve access speed. AOP can be used to implement the cache control function. You can query whether there is data in the cache before executing the method. If so, return it. Otherwise, execute the method and return the method. The value is stored in the cache.
  7. Dynamic proxy: One of the ways to implement AOP is through dynamic proxy, which can proxy all methods of a certain class to implement various functions. To sum up, AOP can be applied to various scenarios. Its function is to separate general cross-cutting concerns from business logic, making the code clearer, more concise, and easier to maintain.

3. Introduction to AOP terminology

1-Crosscutting concerns

The same type of non-core business extracted from each method. Within the same project, we can use multiple cross-cutting concerns to enhance related methods in many different ways.

This concept does not exist naturally at the grammatical level, but is based on the logical needs of additional functions: there are ten additional functions, and there are ten cross-cutting concerns.

2-Notification (enhanced)

Each cross-cutting concern needs to write a method to implement it. Such a method is called a notification method.

  • Pre-notification: executed before the proxied target method
  • Return notification: executed after the delegated target method successfully ends ( ends of life )
  • Exception notification: executed after the proxy target method ends abnormally ( death )
  • Post-notification: executed after the delegated target method finally ends ( final conclusion )
  • Surround notifications: Use the try...catch...finally structure to surround the entire proxied target method, including all locations corresponding to the above four notifications.

3-Joinpoint

This is also a purely logical concept, not defined by grammar.

Refers to those intercepted points. In Spring, methods of target classes can be intercepted by dynamic proxies.

4-pointcut

The way to locate the connection point, or it can be understood as the selected connection point!

It is an expression, such as execution(* com.spring.service.impl. . (..)). Each method that meets the criteria is a concrete join point.

5-Aspect

A combination of entry points and notifications. is a class.

6-target target

The target object being proxied.

7-proxy

A proxy object created after applying notifications to the target object.

8-Weave into weave

Refers to the process of applying notifications to targets and generating proxy objects. It can be woven at compile time or at runtime, and Spring uses the latter.


4. Introduction to Spring AOP framework and relationship sorting

  1. AOP is a programming thinking that is different from OOP, used to improve and solve OOP's non-core code redundancy and inconvenient unified maintenance problems!
  2. Proxy technology (dynamic proxy | static proxy) is a specific technology for realizing AOP thinking programming, but using dynamic proxy to implement the code yourself is more cumbersome!
  3. Spring AOP framework, based on AOP programming thinking, encapsulates dynamic proxy technology and simplifies the implementation of dynamic proxy technology! SpringAOP helps us implement dynamic proxy internally. We only need to write a small amount of configuration and specify the effective range to complete the implementation of aspect-oriented programming!


Summary of key points in this chapter:

1. Aspect-oriented programming thinking (AOP)

        Deepen your understanding of OOP object-oriented programming and AOP aspect-oriented programming

2. Introduction to AOP terminology

        Familiar with AOP terminology crosscutting concerns, notifications (enhancements), targets, join points, pointcuts, aspects, weaving, proxies

3. Introduction to Spring AOP framework and relationship sorting

        Know the relationship between core issues, AOP thinking, agent technology, and AOP framework

Guess you like

Origin blog.csdn.net/dogxixi/article/details/132520032