In -depth analysis
In -depth analysis
Overview:
In the process of software development, as a programming paradigm for the programming (AOP), it can provide a non -invasive way to solve the common attention point across more modules.Spring Framework is a very popular Java class library. Among them, the AOP module provides powerful features to allow developers to implement AOP programming.
What is cutting noodles?
ASPECT is a code used to cut multiple application modules in AOP programming.It can be regarded as a modular focus, which can be applied to multiple methods or categories.The cut surface can process a common focus through different objects and modules by defining a set of cross -cut logic, such as logging, transaction processing, and security verification.
The key concept of SpringFramework Aop:
1. Join Point: The specific points of the program execution, such as the execution of the method or the abnormally throwing.
2. PointCut Expression: Define the entry point by expression, such as selecting all methods to execute or only select a specific class method to execute.
3. Notification (advice): The action to be performed when the entry point is executed. For example, some additional operations are performed before and after the method call.
4. ASPECT: A whole that combines the entry point and notification, including the entry point and related notifications.
5. Join Point: The place where the notification is actually triggered during the program execution process, such as the call of the method.
6. Target Object: Object notified by one or more surfaces, that is, the object to be enhanced.
Basic example:
Suppose we have a simple Java class that contains one method to calculate the sum of two numbers.
public class MathUtils {
public int add(int a, int b) {
return a + b;
}
}
Now, we want to record some logs before and after calling this method.We can implement this through the AOP module of Spring Framework.
First of all, we need to define a cut surface to handle log records.The cut surface should include a starting point and a notification.
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.AfterReturning;
@Aspect
public class LoggingAspect {
@Before("execution(* MathUtils.add(int, int))")
public void logBefore(JoinPoint joinPoint) {
System.out.println("Before method: " + joinPoint.getSignature().getName());
}
@AfterReturning(pointcut = "execution(* MathUtils.add(int, int))", returning = "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
System.out.println("After method: " + joinPoint.getSignature().getName());
System.out.println("Result: " + result);
}
}
The above loggingaspect class uses @Aspect annotations to indicate that it is a cut.In its definition, we define two notification methods: logBeface and logaFTERRETURNING.In the logBeFore method, we use the @BeFore annotation to mark it before the entry point method is executed.In the LogaFTERRETURNING method, we use @Aterreturning annotations to mark it after executing it after the entry point method.
Now, we need to apply the cut surface to the target object.
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@ComponentScan
@EnableAspectJAutoProxy
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(App.class);
MathUtils mathUtils = context.getBean(MathUtils.class);
int result = mathUtils.add(2, 3);
System.out.println("Result: " + result);
context.close();
}
}
The above APP class uses @ComponentScan annotations to indicate that Spring should scan the package where the class is located to find components.Through @EnableaspectjautoProxy, we enabled Spring AOP automatic proxy.
In the main method, we obtained the mathutill instance and called its ADD method.At this time, the cut surface will be automatically applied to the ADD method, and the output is as follows:
Before method: add
After method: add
Result: 5
Through the above examples, we successfully realized the function of recording logs before and after the method call.
Summarize:
Spring Framework's AOP module provides convenient and powerful tools for Java developers to achieve cut -oriented programming.By defining the cutting surface, entry point, and notification, we can decompose horizontal cut logic and business logic, and provide better reuse and maintenance.In actual development, AOP is often used to deal with common attention points such as logs, affairs, and security to help us build more elegant and reliable applications.