AspectJ(1)Introduce and Examples
Chapter 1. Getting Started with AspectJ
Aspect-Oriented Programming(AOP)
1.1. Introduction to AspectJ
AspectJ is an implementation of aspect-oriented programming for java.
A piece of advice is code that is executed when a join point is reached.
The dynamic Join Point Model
AspectJ provides for many kinds of join points, but this chapter discusses only one: method call join points.
A method call includes all the actions that comprise a method call, starting after all arguments are evaluated up to and including return.
Pointcuts
In AspectJ, pointcuts pick out certain join points in the program flow.
call(void Point.setX(int)) picks out each join point that is a call to a method that has the signature void Point.setX(int)
We can use &&, || and |
call(void Point.setX(int)) || call(void Point.setY(int))
Pointcuts can identify join points from many different types:
call(void FigureElement.setXY(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)) ...snip...
We can decalre a new, named pointcut:
pointcut move():
call... || call...
The previous pointcuts are all based on explicit enumeration of a set of method signatures. This is called name-based crosscutting.
We also have property-based crosscutting, for example, wildcard.
call(void Figure.make*(..)) picks out void method defined on Figure whose name begins with "make" regardless of the method's parameters, for example makePoint and makeLine.
call(public * Figure.*(..)) all public methods in Figure.
Advice
So pointcuts pick out join points. Advice brings together a pointcut and a body of code.
Before advide runs as a join point is reached, before the program proceed with the join point. Before advice on a method call join point runs before the actual method starts running, just after the arguments to the method call are evaluated.
before(): greeting(){
System.out.print("Carl, ");
}
After advice runs after the program proceeds with that join point. It runs after the method body has run, just before before control is returned to the caller.
There are 3 kinds of after advice: after returning, after throwing, and plain after( which runs after returning or throwing, like finally).
after() returning:greeting(){
System.out.println(" world!");
}
I create a aspectJ project in STS called easyaspectj. And tested this with Hello World example:
pointcut greeting():
call(void HelloWorld.say*(..));
before(): greeting(){
System.out.print("Carl, ");
}
after() returning:greeting(){
System.out.println(" world!");
}
Around advice will be discussed later.
Exposing Context in Pointcuts
Values exposed by a pointcut can be used in the body of advice declarations.
Method in HelloWorld:
public void study(String bookName, String userName){
}
Advisor and pointcut in AspectHelloWorld.aj:
pointcut study(HelloWorld className, String book, String user):
call(void HelloWorld.study(..)) && target(className)&&args(book,user);
...snip...
after(HelloWorld className, String book, String user) returning: study(className,book,user){
System.out.println(className.getClass().getName() + ":" + user
+ " will read <" + book + ">!");
}
Inter-type declarations
Inter-type declarations in AspectJ are declarations that cut across classes and their hierarchies.
...snip...
Aspects
Aspects wrap up pointcuts, advice, and inter-type declarations.
1.2 Development Aspects
Tracing
pointcut draw():
call(void HelloWorld.draw(..));
before(): draw(){
System.out.println("Print Picture Info:" + thisJoinPoint);
}
thisJoinPoint is bound to an object that describes the current join point.
Profiling and Logging
int count = 0;
...snip...
pointcut count():
draw() || greeting();
before():count(){
count++;
System.out.println("counting......" + count);
}
...snip...
This can log how many times these methods are called.
Pre- and Post- Conditions
check the parameter in aspect.
Contract Enforcement
I can not understand withincode(), I will check this later.
1.3 Production Aspects
Change Monitoring
...snip...
Context Passing
...snip...
Providing Consistent Behavior
...snip...
references:
http://hi.baidu.com/luohuazju/blog/item/d61347889abe319ea4c27286.html
http://hi.baidu.com/luohuazju/blog/item/155948f9be3b7b4f252df21a.html
http://www.eclipse.org/aspectj/
http://www.eclipse.org/aspectj/doc/released/progguide/index.html
http://www.eclipse.org/aspectj/sample-code.html