AspectJ(3)Examples
Chapter 3. Examples
3.1 Obtaining, Compiling and Running the Examples
I download the aspectj stable version with these URLs
http://mirror.neu.edu.cn/eclipse/tools/aspectj/aspectj-1.6.12-src.jar
http://download.actuatechina.com/eclipse/tools/aspectj/aspectj-1.6.12.jar
Double click aspectj-1.6.12.jar, install aspectj in my local machine. I install it in d:/tool directory.
And I add this location D:\tool\aspectj1.6.12\bin to my system PATH.
Add location D:\tool\aspectj1.6.12\lib\aspectjrt.jar to my system CLASSPATH
And all the example is in this directory D:\tool\aspectj1.6.12\doc\examples\.
>cd D:\tool\aspectj1.6.12\doc\examples\
>ajc -argfile telecom/billing.lst
>java telecom.BillingSimulation
3.2. Basic Techniques
Join Points and thisJoinPoint
(examples/tjp)
When using a pointcut that picks out join points of a single kind by name, typicaly the advice will know exactly what kind of join point it is associated with.
thisJoinPoint JoinPoint
Object[] args = jp.getArgs();
String[] names = ((CodeSignature)jp.getSignature()).getParameterNames();
Class[] types = ((CodeSignature)jp.getSignature()).getParameterTypes();
cflow(this(Demo) && execution(void go()));
So that only executions made in the control flow of Demo.go are iintercepted. !execution(* go()) to exclude go itself.
Roles and Views
examples/introduction
Unlike advice, inter-type declarations affect not only the behavior of the application, but also the structural relationship between an application's classes.
The Point class -----> The CloneablePoint aspect
public aspect CloneablePoint{
...snip...
declare parents: Point implements Cloneable;
public Object Point.clone() throws CloneNotSupportedException{
makeRectangular();
makePolar();
return super.clone();
}
...snip...
}
The ComparablePoint aspect
The HashablePoint aspect
3.3. Development Aspects
Tracing using aspects
examples/tracing
Sometimes, tracing is one of those things that slows the system down, so these calls should often be pulled out of the system before the product is shipped.
3.4. Production Aspects
A Bean Aspect
examples/bean
The Point class ----> The BoundPoint aspect
...snip..
private PropertyChangeSupport Point.support = new PropertyChangeSupport(this);
...snip...
class Demo implements PropertyChangeListener
...snip...
The Subject/Observer Protocol
examples/observer
A Simple Telecom Simulation
examples/telecom
3.5. Reusable Aspects
Tracing using Aspects, Revisited
references:
http://www.eclipse.org/aspectj/doc/released/progguide/examples.html
Chapter 3. Examples
3.1 Obtaining, Compiling and Running the Examples
I download the aspectj stable version with these URLs
http://mirror.neu.edu.cn/eclipse/tools/aspectj/aspectj-1.6.12-src.jar
http://download.actuatechina.com/eclipse/tools/aspectj/aspectj-1.6.12.jar
Double click aspectj-1.6.12.jar, install aspectj in my local machine. I install it in d:/tool directory.
And I add this location D:\tool\aspectj1.6.12\bin to my system PATH.
Add location D:\tool\aspectj1.6.12\lib\aspectjrt.jar to my system CLASSPATH
And all the example is in this directory D:\tool\aspectj1.6.12\doc\examples\.
>cd D:\tool\aspectj1.6.12\doc\examples\
>ajc -argfile telecom/billing.lst
>java telecom.BillingSimulation
3.2. Basic Techniques
Join Points and thisJoinPoint
(examples/tjp)
When using a pointcut that picks out join points of a single kind by name, typicaly the advice will know exactly what kind of join point it is associated with.
thisJoinPoint JoinPoint
Object[] args = jp.getArgs();
String[] names = ((CodeSignature)jp.getSignature()).getParameterNames();
Class[] types = ((CodeSignature)jp.getSignature()).getParameterTypes();
cflow(this(Demo) && execution(void go()));
So that only executions made in the control flow of Demo.go are iintercepted. !execution(* go()) to exclude go itself.
Roles and Views
examples/introduction
Unlike advice, inter-type declarations affect not only the behavior of the application, but also the structural relationship between an application's classes.
The Point class -----> The CloneablePoint aspect
public aspect CloneablePoint{
...snip...
declare parents: Point implements Cloneable;
public Object Point.clone() throws CloneNotSupportedException{
makeRectangular();
makePolar();
return super.clone();
}
...snip...
}
The ComparablePoint aspect
The HashablePoint aspect
3.3. Development Aspects
Tracing using aspects
examples/tracing
Sometimes, tracing is one of those things that slows the system down, so these calls should often be pulled out of the system before the product is shipped.
3.4. Production Aspects
A Bean Aspect
examples/bean
The Point class ----> The BoundPoint aspect
...snip..
private PropertyChangeSupport Point.support = new PropertyChangeSupport(this);
...snip...
class Demo implements PropertyChangeListener
...snip...
The Subject/Observer Protocol
examples/observer
A Simple Telecom Simulation
examples/telecom
3.5. Reusable Aspects
Tracing using Aspects, Revisited
references:
http://www.eclipse.org/aspectj/doc/released/progguide/examples.html
本文详细介绍了如何利用AspectJ实现面向切面编程,包括下载安装、配置环境、编写示例代码、理解关键概念如JoinPoint和aspect等,并讨论了在开发和生产阶段如何合理使用AspectJ。此外,文章还探讨了可重用方面、性能优化、角色和观察者模式的应用,以及如何通过AspectJ实现系统的可维护性和灵活性。
180

被折叠的 条评论
为什么被折叠?



