The application and principles of Aspectjtools framework in the Java class library detailed explanation
ASPECTJ is a frame -based programming framework that can be applied in the Java library to enhance the code function.It is based on the expansion of the Java compiler and uses the code to realize the cut -off programming by implanting the code during compilation.
The principle of ASPECTJ is to enhance the Java library by defining the cutting surface and the Join Point.The cut surface is a class with specific functions, which contains a series of notifications and pointcut.The notification defines the code logic to be executed at a specific connection point, such as performing certain operations before and after or abnormally occur when the method executes.The cut point defines the matching rules of the connection point to determine which connection point will be influenced by notification.
Below is a simple example, demonstrating the application of Aspectj in the Java library.Suppose we have a Java class called Mathutil, which contains a method of calculating the square number.
public class MathUtil {
public int square(int num) {
System.out.println ("Calculating square number:" + num);
return num * num;
}
}
Now, we hope to output some log information before and after the method execution.We can use ASPECTJ to implement this feature.
First, we need to introduce ASPECTJ dependencies in the Java library.It can be achieved by adding the following in the pom.xml file:
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
</dependencies>
Next, we create a new class of Loggingaspect as the cut surface, which defines a front notice and a rear notification.
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
@Before("execution(* MathUtil.square(int))")
public void logBefore(JoinPoint joinPoint) {
System.out.println("Before执行:" + joinPoint.getSignature().getName());
}
@After("execution(* MathUtil.square(int))")
public void logAfter(JoinPoint joinPoint) {
System.out.println("After执行:" + joinPoint.getSignature().getName());
}
}
In the code above, the cut point is defined as "Execution (* mathutil.square (int))", which indicates that the notification notification is performed on the Square method of the Mathutil class.
Finally, use the ASPECTJ framework in our application to enhance the function of the Mathutil class:
import org.aspectj.lang.annotation.AnnotationUtils;
import org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter;
public class Application {
public static void main(String[] args) {
MathUtil mathUtil = new MathUtil();
ClassPreProcessorAgentAdapter.initialize();
AnnotationUtils.processMathUtil(mathUtil);
System.out.println(mathUtil.square(4));
}
}
In the above code, we use AnnotationUtils.ProcessMathutil method to apply surface to the Mathutil class.This will lead to output logs before and after the execution of the Square method.
Through the above example, we can see the application and principles of the Aspectj framework in the Java library.It enhances the function of the Java class library by defining the cutting surface and connection points.With ASPECTJ, we can easily insert universal functions in the existing code, such as log records and performance monitoring.