English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The application is typically developed in a multi-layered manner. A typical Java application has the following layers:
Web layer: It exposes the service using REST or Web applicationsservice. business layer: It implements the application'sbusiness logic. data layer: It implements the application'spersistence logic.
Each layer has different responsibilities, but there are some common aspects that apply to all layers, such as logging, security, validation, caching, etc.. These common aspects are known as cross-cutting concerns.
If we implement these concerns separately at each layer, the code will become more difficult to maintain. To overcome this problem, Aspect-Oriented Programming(AOP) provides a solution to cross-cutting problems.
Treat cross-cutting concerns as aspects. Define切入点 to indicate where aspects must be applied.
It ensures that cross-cutting concerns are defined within a cohesive code component.
AOP (Aspect-Oriented Programming)It is a through allowing Cross-cutting concernSeparation to improve modular programming patterns. These cross-departmental concerns are different from the main business logic. We can add other behaviors to existing code without modifying the code itself.
Spring's AOP framework can help us achieve these cross-cutting concerns.
With AOP, we can define the way and location to apply this feature in one place. We can freely define the way and location to apply this feature without modifying the class that introduces a new feature. Now, the cross-cutting concerns can be modularized into special classes called Aspect.
aspects have TwoAdvantages:
Firstly, the logic of each concern is now concentrated in one place, rather than scattered throughout the codebase. Secondly, the business module only contains the main code that we are concerned about. The secondary concerns have been moved toAspect.
Each aspect has responsibilities to be executed, called Advice. We can implement the functionality of the aspect into the program at one or more join points.
It is implemented in pure Java. No special compilation process is required. It only supports method execution join points. Only provides runtime weaving. There are two types of AOP proxies: JDK dynamic proxyand CGLIB proxy.
Cross-cutting concerns are the concerns that we need to implement at multiple locations in the application. It affects the entire application.
Aspect: An aspect is a module that encapsulatesadviceandpointcutsand providecross-cuttingThere can be many aspects. We can use aspects with @Aspect conventional classes with annotations to implement aspects. Pointcut: A pointcut is an expression that selects one or more join points where Advice is executed. We can useexpressionsorpatternsIt defines the pointcut. It uses different types of expressions that match the join point. In the Spring Framework, we use AspectJ Pointcut expression language. Join point: A join point is an application in the application where Advice is applied. AOP aspectat the point. Or it is a specific execution instance of Advice. In AOP, a join point can bemethod execution, exception handling, changing the value of object variablesetc. Advice: Advice is what we have before the method execution,beforeoraftermeasures taken. The action is a piece of code called during program execution. The Spring AOP framework hasfivetypes of Advice: in Advice before, after, after-returning, after-throwing andaround advice. It is for a specificjoin point's Advice. We will further discuss these Advice in this section. Target object: An object that applies Advice is calledtarget object. The target object is alwaysproxiedThis means that at runtime, a subclass that overrides the target method will be created, and Advice will be included according to its configuration. Weaving: This is the process of linking aspects with other application types.linking aspectsprocess. We canruntime, load-timeandCompile-timeWeaving.
Proxy: It is the object created after applying Advice to the target object, called proxy . Spring AOP implements JDK dynamic proxy, to create proxy classes using target class and Advice calls. These are called AOP proxy classes.
The difference between AOP and OOP is as follows:
AOP | OOP |
Aspect: A code unit encapsulating pointcut, Advice, and properties. | Class: A code unit encapsulating methods and properties. |
Pointcut: It defines a set of entry points for executing Advice. | Method signature: . It defines the entry point for executing the method body. |
Advice: This is the implementation of cross-cutting concerns. | Method bodies: It is the implementation of business logic issues. |
Waver: Constructs code (source or object) with Advice. | Compiler: It converts source code to target code. |
The difference between AOP and OOP is as follows:
Before implementing AOP in the application, we need to add the following to the pom.xml file. | AspectJ |
It requires a separate compilation process. | It requires the AspectJ compiler. |
It only supports method execution切入点. | It supports all切入点. |
It can be implemented on beans managed by the Spring Container. | It can be implemented on all domain objects. |
It only supports method-level weaving. | It can fluctuate fields, methods, constructors, static initializers, final classes, etc. |
There are five types of AOP Advice, as shown below:
Before Advice After Advice Around Advice After Throwing After Returning
Before Advice: Advice executed before the join point is called before the notification. We use @Before Annotations mark Advice as Before notification.
After Advice: Advice executed after the join point is called after the notification. We use @After Annotations mark Advice as After notification.
Around Advice: Advice executed before and after the join point is
After Throwing: Advice executed when an exception is thrown at the join point.
After Returning: After Returning:
Advice executed when the method executes successfully. Before implementing AOP in the application, we need to add the following to the pom.xml file. Spring AOP
Spring Boot Starter AOP
Example <dependency>/<groupId>org.springframework.boot <groupId>-<artifactId>spring-boot-starter/aop< artifactId>2<version>2<version>2./.RELEASE< version>/<
dependency>