1. Basic concepts
AspectJ is a Java-based implementation for aspect programming. It adds the new concept of Join Point to Java, but in fact it is just the name of an existing Java concept. It adds a few new structures to the Java language: pointcut, notification (Advice), Inter-type declaration (Inter-type declaration) and Aspect (Aspect). Tilts and notifications dynamically affect the program flow, inter-type declarations are static class hierarchical structures of the program, and tangents are encapsulations of all these new structures.
The concepts based on sections, connection points, tangent points, and notifications are as follows:
Aspect: The Aspect declaration is similar to the class declaration in Java, and it contains some Pointcuts and corresponding Advices in Aspect.
Joint point: represents a point clearly defined in the program. Typically, it includes method calls, access to class members, and execution of exception handling program blocks, etc. It can also nest other joint points by itself.
Pointcut: Represents a group of joint points. These joint points are either combined through logical relationships, or concentrated through wildcards, regular expressions, etc. It defines where the corresponding Advice will occur.
Advice: Advice defines the specific operations to be done by the program point defined in pointcut. It distinguishes before, after and around whether to execute the code before, after or after each joint point or instead.
The connection point is an appropriate point in the program flow. The tangent collects a specific set of connection points and the values in these points. A notification is the code executed when a connection point arrives, and these are dynamic parts of AspectJ. In fact, the connection point is like a statement in a program, and the tangent point is a breakpoint set at a specific statement. It collects information about the program stack at the breakpoint, and the notification is what you want to join before and after this breakpoint. Program generation. There are also many different types of inter-type declarations in AspectJ, which allows programmers to modify the static structure, name, class members, and relationships between classes. The aspect in AspectJ is the module unit that cross-cuts the focus. Their behavior is similar to classes in Java, but also encapsulates point-cutting, notifications, and inter-type declarations.
2. How to develop AOP programs based on AJDT: AspectJ Development Tools?
Follow the instructions on the ajdt website to install the ajdt plugin in eclipse http://www.eclipse.org/ajdt/
Create an AspectJ Project project
Conduct code development (this article provides a simple example)
3. Simple example:
The code copy is as follows:
package aop.test;
public interface FigureElement {
public void setXY(int x,int y);
public void draw();
}
The code copy is as follows:
package aop.test;
public class Point implements FigureElement {
public int x;
private int y;
public int getX() {
return x;
}
public String setX(int x) {
System.out.println("Set x value: x="+x);
this.x = x;
return "The return value is x="+x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Point(){
}
public Point(int x,int y){
this.x=x;
this.y=y;
}
@Override
public void setXY(int x,int y) {
this.x=x;
this.y=y;
System.out.println("Point setXY: x="+x+",y="+y);
}
@Override
public void draw() {
System.out.println("Point draw");
}
@Override
public String toString(){
return "Point: x="+x+",y="+y;
}
}