AspectJ(3)Examples

本文详细介绍了如何利用AspectJ实现面向切面编程,包括下载安装、配置环境、编写示例代码、理解关键概念如JoinPoint和aspect等,并讨论了在开发和生产阶段如何合理使用AspectJ。此外,文章还探讨了可重用方面、性能优化、角色和观察者模式的应用,以及如何通过AspectJ实现系统的可维护性和灵活性。
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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值