- 浏览: 2542445 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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
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
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 468NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 330Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 377Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 468NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 414Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 332Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 243GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 445GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 321GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 307Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 286Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 303Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 281NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 257Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 565NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 257Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 361Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 365Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
Eclipse AspectJ: Aspect Oriented Programming with AspectJ and the Eclipse ADT
1. 如何创建切面:切面是封装横切关注点的类,包含通知(advice)和切点(pointcut)定义。 2. 通知类型:包括前置通知(before)、后置通知(after)、返回通知(after returning)、异常通知(after throwing)和...
1. 分析用户交互行为:AspectJ 可以帮助开发者分析用户交互行为,包括用户界面点击、触摸、切换等交互行为。这可以帮助开发者更好地理解用户行为,优化应用程序的用户体验。 2. 分析 APP 性能:AspectJ 可以帮助...
Eclipse Aspectj: Aspect Oriented Programming with AspectJ and the Eclipse AspectJ Development Tools
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 ...
1. **面向切面编程(AOP)**: AOP是软件工程中的一种编程范式,旨在减少代码的冗余和提高可维护性。通过分离关注点,将横切关注点(如日志、异常处理等)从核心业务逻辑中解耦出来。 2. **AspectJ语法**: AspectJ提供...
8. **examples**:示例代码,帮助用户理解如何使用AspectJ。 9. **.idea**或**.project**:IDE相关的配置文件,通常用于Eclipse或IntelliJ IDEA。 通过深入研究这些文件,开发者可以了解到AspectJ的实现细节,学习...
1. **切点(Pointcut)**:切点是程序中特定的执行点,例如方法的调用、异常的抛出等。AspectJ提供了强大的表达式语言来精确地指定切点,这样我们就可以在需要的地方插入我们的关注点。 2. **通知(Advice)**:...
1. **静态织入**:使用ajc编译器在编译时直接将切面织入到字节码中,生成的类可以直接在Java虚拟机(JVM)上运行。 2. **动态织入**:在类加载时或运行时,通过代理模式实现切面的织入。这使得可以在不重新编译源...
aspectj-1.7.0.jar aspectj的包
1. **切面(Aspect)**:切面是AspectJ的核心,它封装了系统中的一个关注点,例如数据验证、事务管理等。切面由通知(advice)、切点(pointcut)和引入(introduction)组成。 2. **通知(Advice)**:这是切面...
1. **织入机制**:AspectJ 提供了两种织入方式——编译时织入和运行时织入。编译时织入是通过AspectJ的编译器ajc将切面代码合并到目标类中,生成新的字节码;运行时织入则是在程序运行时,通过AspectJ的加载器动态地...
3. **examples** - 提供了一些示例代码,演示了如何使用AspectJ进行切面编程,包括切点定义、通知类型(前置通知、后置通知、环绕通知等)以及切面的编写。 4. **src** - 可能包含AspectJ的源代码,对于学习其内部...
1. `aspectjweaver.jar`:这是AspectJ的核心库,它包含了一组编译器和类装载器,能够在运行时或者编译时处理AspectJ代码。AspectJ Weaver在Spring AOP中起到关键作用,它可以动态地在类加载时织入切面,即使目标类...
1. **引入依赖**:在Maven项目中,需要添加AspectJ的依赖库。 2. **编写切面**:创建一个类并使用`@Aspect`注解标记为切面。然后定义切点和通知。 ```java @Aspect public class LoggingAspect { @Before(...
1. 添加依赖:首先,确保在项目的构建配置(如Maven的pom.xml或Gradle的build.gradle)中添加AspectJ的依赖。这通常包括`aspectjrt.jar`和`aspectjweaver.jar`,它们分别提供了运行时环境和织入功能。 2. 配置织入...
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. ...
1. `src`目录:源代码,包括Java类和AspectJ切面定义。 2. `lib`目录:可能包含AspectJ的库文件,如`aspectjrt.jar`和`aspectjweaver.jar`。 3. `build.xml`或`pom.xml`:构建脚本,用于编译和运行程序,可能使用Ant...
1. **AspectJ基础**:介绍AspectJ的基本元素,包括切面、通知(advice)、连接点(join point)、切点(pointcut)和织入(weaving)。这些概念构成了AspectJ的基础,理解它们是使用AspectJ的前提。 2. **切面和...