`
sillycat
  • 浏览: 2542445 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

AspectJ(1)Introduce and Examples

阅读更多
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


分享到:
评论

相关推荐

    Eclipse AspectJ: Aspect Oriented Programming with AspectJ and the Eclipse ADT (part 2/2)

    Eclipse AspectJ: Aspect Oriented Programming with AspectJ and the Eclipse ADT

    aspectj,aspectjrt,weaver等

    1. 如何创建切面:切面是封装横切关注点的类,包含通知(advice)和切点(pointcut)定义。 2. 通知类型:包括前置通知(before)、后置通知(after)、返回通知(after returning)、异常通知(after throwing)和...

    Android Studio AspectJ技术1

    1. 分析用户交互行为:AspectJ 可以帮助开发者分析用户交互行为,包括用户界面点击、触摸、切换等交互行为。这可以帮助开发者更好地理解用户行为,优化应用程序的用户体验。 2. 分析 APP 性能:AspectJ 可以帮助...

    Eclipse AspectJ: Aspect Oriented Programming with AspectJ and the Eclipse ADT (part 1/2)

    Eclipse Aspectj: Aspect Oriented Programming with AspectJ and the Eclipse AspectJ Development Tools

    Eclipse AspectJ: Aspect-Oriented Programming

    This book is an introduction to AOP with AspectJ and Eclipse and shows how to create a productive AO development environment by using the AspectJ Development Tools for Eclipse (AJDT). Tools have an ...

    Aspectj

    1. **面向切面编程(AOP)**: AOP是软件工程中的一种编程范式,旨在减少代码的冗余和提高可维护性。通过分离关注点,将横切关注点(如日志、异常处理等)从核心业务逻辑中解耦出来。 2. **AspectJ语法**: AspectJ提供...

    org.aspectj,aspectj项目库(org.aspectj).zip

    8. **examples**:示例代码,帮助用户理解如何使用AspectJ。 9. **.idea**或**.project**:IDE相关的配置文件,通常用于Eclipse或IntelliJ IDEA。 通过深入研究这些文件,开发者可以了解到AspectJ的实现细节,学习...

    aspectJ

    1. **切点(Pointcut)**:切点是程序中特定的执行点,例如方法的调用、异常的抛出等。AspectJ提供了强大的表达式语言来精确地指定切点,这样我们就可以在需要的地方插入我们的关注点。 2. **通知(Advice)**:...

    aspectj-1.9.6.jar

    1. **静态织入**:使用ajc编译器在编译时直接将切面织入到字节码中,生成的类可以直接在Java虚拟机(JVM)上运行。 2. **动态织入**:在类加载时或运行时,通过代理模式实现切面的织入。这使得可以在不重新编译源...

    aspectj-1.7.0.jar

    aspectj-1.7.0.jar aspectj的包

    精通AspectJ_源代码

    1. **切面(Aspect)**:切面是AspectJ的核心,它封装了系统中的一个关注点,例如数据验证、事务管理等。切面由通知(advice)、切点(pointcut)和引入(introduction)组成。 2. **通知(Advice)**:这是切面...

    aspectj-1.6.9.jar

    1. **织入机制**:AspectJ 提供了两种织入方式——编译时织入和运行时织入。编译时织入是通过AspectJ的编译器ajc将切面代码合并到目标类中,生成新的字节码;运行时织入则是在程序运行时,通过AspectJ的加载器动态地...

    aspectj.zip

    3. **examples** - 提供了一些示例代码,演示了如何使用AspectJ进行切面编程,包括切点定义、通知类型(前置通知、后置通知、环绕通知等)以及切面的编写。 4. **src** - 可能包含AspectJ的源代码,对于学习其内部...

    AspectJ的JAR包

    1. `aspectjweaver.jar`:这是AspectJ的核心库,它包含了一组编译器和类装载器,能够在运行时或者编译时处理AspectJ代码。AspectJ Weaver在Spring AOP中起到关键作用,它可以动态地在类加载时织入切面,即使目标类...

    Aop之AspectJ详解解读demo

    1. **引入依赖**:在Maven项目中,需要添加AspectJ的依赖库。 2. **编写切面**:创建一个类并使用`@Aspect`注解标记为切面。然后定义切点和通知。 ```java @Aspect public class LoggingAspect { @Before(...

    aspectj jar

    1. 添加依赖:首先,确保在项目的构建配置(如Maven的pom.xml或Gradle的build.gradle)中添加AspectJ的依赖。这通常包括`aspectjrt.jar`和`aspectjweaver.jar`,它们分别提供了运行时环境和织入功能。 2. 配置织入...

    AspectJ in Action

    HIGHLIGHT AspectJ in Action, Second Edition is a revised and udated edition, now covering AspectJ 6 and Spring 2.5. It guides Java developers through AOP and AspectJ using practical applications. ...

    AspectJ面向切面的例子

    1. `src`目录:源代码,包括Java类和AspectJ切面定义。 2. `lib`目录:可能包含AspectJ的库文件,如`aspectjrt.jar`和`aspectjweaver.jar`。 3. `build.xml`或`pom.xml`:构建脚本,用于编译和运行程序,可能使用Ant...

    AspectJ_Cookbook_English_edition

    1. **AspectJ基础**:介绍AspectJ的基本元素,包括切面、通知(advice)、连接点(join point)、切点(pointcut)和织入(weaving)。这些概念构成了AspectJ的基础,理解它们是使用AspectJ的前提。 2. **切面和...

Global site tag (gtag.js) - Google Analytics