Java aspectjf oriented programming coding sample sharing

  • 2020-04-01 02:58:10
  • OfStack

1. Basic concepts

AspectJ is a java-based implementation of tangent oriented programming. It adds the new concept of Join Point to Java, which is really just the name of an existing Java concept. It adds a few new constructs to the Java language: pointcuts, Advice, inter-type declarations, and aspects. Pointcuts and notifications affect the program flow dynamically, inter-type declarations affect the program's class-level structure statically, and facets encapsulate all of these new structures.

The concepts of cut surface, join point, cut point and notification are as follows:

Aspect: an Aspect declaration is similar to a class declaration in Java, where pointcuts and Advice are included.
Joint point: an explicitly defined point in a program that typically includes method calls, access to class members, and execution of an exception handler block. It can also nest other Joint points.
Pointcut: represents a set of joint points that are either logically combined or aggregated by means of wildcards, regular expressions, and so on, and that define where the corresponding Advice is going to take place.
Advice: the Advice defines exactly what the program points defined in the pointcut do, and it distinguishes between before, after, and around the code that is executed before, after, or instead of each joint point.
A join point is an appropriate point in the program flow. Pointcuts collect a specific set of join points and the values in those points. A notification is the code that executes when a join point arrives, which is the dynamic part of AspectJ. A join point is like a statement in a program. A pointcut is a breakpoint set at a particular statement that collects information about the stack at the breakpoint. AspectJ also has many different kinds of inter-type declarations, which allow programmers to modify the static structure of a program, its name, the members of a class, and the relationships between classes. Aspects in AspectJ are modular units of crosscutting concerns. They behave much like classes in the Java language, but aspects also encapsulate pointcuts, notifications, and inter-type declarations

How to develop AOP programs based on AJDT: AspectJ Development Tools?

According to the ajdt site, in eclipse ajdt plug-in installation at http://www.eclipse.org/ajdt/
Create the AspectJ Project Project
Code development (this article provides a simple example)

Iii. Simple example:


package aop.test;
public interface FigureElement {
 public void setXY(int x,int y);
 public void draw();
}


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 up the 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;
 }
}


Related articles: