AOP (sectional-oriented programming) is a programming idea that can separate cross-cutting concerns in programs (such as logging, security checking, performance monitoring, etc.) from business logic for better management and maintenance.
Use scenarios
- Logging: During the execution of the program, record the execution status, error information, etc. of key operations.
- Safety check: During program execution, check the user's permissions or authentication to ensure that only authorized users can perform specific actions.
- Transaction Management: Ensure multiple operations are executed in one transaction to ensure data integrity and consistency.
- Performance monitoring: Monitor the performance of program execution, including execution time, memory usage and other indicators for optimization.
- Exception handling: Catch and handle exceptions during program execution to avoid program crashes or unknown errors.
- Cache Management: Manage cache in the program, including cache storage, refresh and delete operations.
- Internationalization: Manage international resources in programs so that programs can run correctly in different locales.
Common Terms
In AOP, there are some commonly used terms, including Aspect, Join Point, Advice, etc., which are explained below:
-
Aspect
: Slice, used to describe the concept of a cross-sectional concern, usually a class or module, which contains a common set of cross-sectional concerns. -
Join Point
: Connection point, used to describe an execution point in a program, such as a call to a method, assignment of a property, throwing an exception, etc. In AOP, Join Point is the execution point of the program that can be intercepted in the AOP framework. -
Advice
: Notification, used to describe the actions performed by a specific cross-cutting concern, including before, after, around, exception notification (after-Throwing), and return notification (after-Returning), etc. -
Pointcut
: Point-cut, used to describe a collection of one or more Join Points, usually defined by an expression. -
Target Object
: Target object, used to describe the object to be notified, that is, an object containing Join Point. -
Weaving
: Weaving, used to describe the process of inserting Advice defined in Aspect into the target object, usually completed during the program's run.
Design Pattern Application
In AOP, design patterns such as agents, decorators and interceptors are often used. The following are their applications in AOP:
- Agent Mode: The proxy mode is widely used in AOP. In AOP, the proxy mode is usually used to implement dynamic proxy, that is, a proxy object is generated when the program is running. The proxy object contains all methods of the target object, and additional logic can be inserted before and after the method is executed, such as logging, performance monitoring, transaction management, etc.
- Decorator mode: The decorator pattern is a structural design pattern that dynamically adds new behavior to an object without modifying the original class. In AOP, the decorator pattern is often used to implement Aspect, which is to wrap a common set of cross-cutting concerns in one, and then apply the Aspect to the target object.
- Interceptor mode: Interceptor mode is a structural design pattern that can achieve additional logic by intercepting and modifying method calls without modifying the original class. In AOP, the interceptor mode is usually used to implement Advice, that is, performing specific operations on a specific Join Point, such as recording logs before and after method execution, verifying permissions, handling exceptions, etc.
Sample code
Here is a simple Spring AOP sample code:
Contains how to define facets, define notifications, and how to use AOP to intercept and enhance the target object:
//Define a section@Aspect @Component public class LoggingAspect { //Define a pre-notification to intercept all public methods @Before("execution(public * .*.*(..))") public void logBefore(JoinPoint joinPoint) { ("Execution method:" + ().getName()); } //Define a post notification to intercept all save methods @AfterReturning(pointcut = "execution(* .*.save*(..))", returning = "result") public void logAfter(JoinPoint joinPoint, Object result) { ("Method execution ends:" + ().getName() + ", the return value is:" + result); } } //Define a business class@Service public class UserService { public void login(String username, String password) { ("User Login"); } public void saveUser(User user) { ("Save User Information"); } } //Open AOP in the configuration class@Configuration @EnableAspectJAutoProxy public class AppConfig { }
In the above example code, a section LoggingAspect is first defined, marked with @Aspect and @Component annotations. Then, in LoggingAspect, two notification methods are defined, one pre-notification logBefore, which intercepts all public methods and one post-notification logAfter, which intercepts all save methods.
Next, two business methods, login and saveUser, are defined in the UserService class. Finally, use the @EnableAspectJAutoProxy annotation in the AppConfig configuration class to enable AOP.
When the program is running, when the login or saveUser method of the UserService is called, the AOP framework will automatically intercept these methods and execute the corresponding enhancement logic based on the notification method defined in LoggingAspect.
For example, when the saveUser method is called, the AOP framework will first execute the logBefore method and output the log before the method is executed; then execute the saveUser method; finally execute the logAfter method, output the log after the method is executed.
In short, Spring AOP is a very powerful AOP framework that can greatly simplify developers' work and improve code maintainability and reusability. Developers can learn how to use Spring AOP to intercept and enhance target objects through the above sample code.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.