先說一下AOSD的起源吧
傳統(tǒng)的軟件工程有一個(gè)不變的主題:對關(guān)注點(diǎn)的分解和局部化。將系統(tǒng)分解成為主要的功能模塊,識別出關(guān)注點(diǎn)的其他問題,確保所有關(guān)注點(diǎn)的問題都能在代碼的適當(dāng)位置得到解決。但是關(guān)注點(diǎn)的分散和混雜又給代碼編寫和后期維護(hù)帶來了很大的難度。
因此,必須有一種方法可以把關(guān)注點(diǎn)集中在一起,讓系統(tǒng)開發(fā)者可以使用關(guān)注點(diǎn)自身的模塊來描述每個(gè)關(guān)注點(diǎn)的行為。
AOSD,用以尋找軟件系統(tǒng)中新的模塊化特性,允許對系統(tǒng)中多個(gè)關(guān)注點(diǎn)進(jìn)行獨(dú)立描述,同時(shí)又能自動統(tǒng)一到系統(tǒng)中。
然后是一些常用的術(shù)語(from AOSD wiki):
concern(關(guān)注點(diǎn)):A concern is an area of interest or focus in a system. Concerns are the primary criteria for decomposing software into smaller, more manageable and comprehensible parts that have meaning to a software engineer.
crosscutting(橫切):Note that crosscutting is a relationship between representations of concerns. Note also that it is a symmetric relationship. Therefore, if:
1. A is a representation of one a concern,
2. B is a representation of another concern, and
3. A crosscuts B,
then B also crosscuts A.
This means that the term "crosscutting concerns" is often misused in two ways: To talk about a single concern, and to talk about concerns rather than representations of concerns. Consider "synchronization is a crosscutting concern": we don't know that synchronization is crosscutting unless we know what it crosscuts. And there may be representations of the concerns involved that are not crosscutting.
aspect(方面):Aspects are one kind of concern in software development.
joint point(聯(lián)接點(diǎn)):Join points are those elements of the programming language semantics which the aspects coordinate with. Nowadays, there are various join point models around and still new under development. They heavily depend on the underlying programming language and AO language.
In a number of presently available AOP languages, a join point is a region in the dynamic control flow of an application. Thus a join point can for instance represent
* a call to a method,
* execution of a method,
* the event of setting a field,
* the event of handling an exception ...
Join points can be picked up by an AOP program by using pointcuts to match on them. Depending on the pointcut language the AOP language provides, it may be possible to pick up more or less of those join points. Since join points are dynamic, it may be possible to expose runtime information such as the caller or callee of a method from a join point to a matching pointcut.
advice:In a number of AOP languages, advice consists of a pointcut and a body. The body executes at join points the pointcut matches. This pointcut may expose runtime information to the advice body.
pointcut:
(from Without EJB):A set of join points,defined to specify when an advice should fire.Pointcuts are often described using either regular expressions or another wildcard syntax.
(from Wiki)In a number of AOP languages, a pointcut is a predicate over dynamic join points, meaning that given a certain dynamic join point, a pointcut can either match this join point or not (at runtime). Another view of pointcuts is often, that they represent sets of join points. A pointcut may expose runtime information to a piece of advice.
Weaving:The process of coordinating aspects and non-aspects. Weaving can be done explicitly or implicitly, and can be done at a variety of times ranging from by-hand weaving when code is written, through compile-time, post-compile time and load time, up to runtime.
Without EJB中有個(gè)例子很好的解釋了一下上面的術(shù)語:
public class MyBusinessObject implements BusinessObject{
public void businessMethod1() throws UnauthorizedException{
doSecurityCheck();
}
public void businessMethod2() throws UnauthorizedException{
doSecurityCheck();
}
public void requiresNoSecurityCheck() {
}
public void doSecurityCheck() throws UnauthorizedException{
}
}
這里,安全檢查就是一個(gè)aspect,需要進(jìn)行安全檢查的這幾個(gè)方法就是join point。而由于不是所有的方法都需要進(jìn)行安全檢查,所以就需要用pointcut來進(jìn)行匹配。
下面使用了一個(gè)interceptor來將關(guān)注點(diǎn)模塊化:
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class SecurityInterceptor implements MethodInterceptor{
public Object invoke(MethodInvocation invocation)throws Throwable{
doSecurityCheck();
return invocation.proceed();
}
public void doSecurityCheck{}
}
這里的interceptor就是advice
傳統(tǒng)的軟件工程有一個(gè)不變的主題:對關(guān)注點(diǎn)的分解和局部化。將系統(tǒng)分解成為主要的功能模塊,識別出關(guān)注點(diǎn)的其他問題,確保所有關(guān)注點(diǎn)的問題都能在代碼的適當(dāng)位置得到解決。但是關(guān)注點(diǎn)的分散和混雜又給代碼編寫和后期維護(hù)帶來了很大的難度。
因此,必須有一種方法可以把關(guān)注點(diǎn)集中在一起,讓系統(tǒng)開發(fā)者可以使用關(guān)注點(diǎn)自身的模塊來描述每個(gè)關(guān)注點(diǎn)的行為。
AOSD,用以尋找軟件系統(tǒng)中新的模塊化特性,允許對系統(tǒng)中多個(gè)關(guān)注點(diǎn)進(jìn)行獨(dú)立描述,同時(shí)又能自動統(tǒng)一到系統(tǒng)中。
然后是一些常用的術(shù)語(from AOSD wiki):
concern(關(guān)注點(diǎn)):A concern is an area of interest or focus in a system. Concerns are the primary criteria for decomposing software into smaller, more manageable and comprehensible parts that have meaning to a software engineer.
crosscutting(橫切):Note that crosscutting is a relationship between representations of concerns. Note also that it is a symmetric relationship. Therefore, if:
1. A is a representation of one a concern,
2. B is a representation of another concern, and
3. A crosscuts B,
then B also crosscuts A.
This means that the term "crosscutting concerns" is often misused in two ways: To talk about a single concern, and to talk about concerns rather than representations of concerns. Consider "synchronization is a crosscutting concern": we don't know that synchronization is crosscutting unless we know what it crosscuts. And there may be representations of the concerns involved that are not crosscutting.
aspect(方面):Aspects are one kind of concern in software development.
joint point(聯(lián)接點(diǎn)):Join points are those elements of the programming language semantics which the aspects coordinate with. Nowadays, there are various join point models around and still new under development. They heavily depend on the underlying programming language and AO language.
In a number of presently available AOP languages, a join point is a region in the dynamic control flow of an application. Thus a join point can for instance represent
* a call to a method,
* execution of a method,
* the event of setting a field,
* the event of handling an exception ...
Join points can be picked up by an AOP program by using pointcuts to match on them. Depending on the pointcut language the AOP language provides, it may be possible to pick up more or less of those join points. Since join points are dynamic, it may be possible to expose runtime information such as the caller or callee of a method from a join point to a matching pointcut.
advice:In a number of AOP languages, advice consists of a pointcut and a body. The body executes at join points the pointcut matches. This pointcut may expose runtime information to the advice body.
pointcut:
(from Without EJB):A set of join points,defined to specify when an advice should fire.Pointcuts are often described using either regular expressions or another wildcard syntax.
(from Wiki)In a number of AOP languages, a pointcut is a predicate over dynamic join points, meaning that given a certain dynamic join point, a pointcut can either match this join point or not (at runtime). Another view of pointcuts is often, that they represent sets of join points. A pointcut may expose runtime information to a piece of advice.
Weaving:The process of coordinating aspects and non-aspects. Weaving can be done explicitly or implicitly, and can be done at a variety of times ranging from by-hand weaving when code is written, through compile-time, post-compile time and load time, up to runtime.
Without EJB中有個(gè)例子很好的解釋了一下上面的術(shù)語:
public class MyBusinessObject implements BusinessObject{
public void businessMethod1() throws UnauthorizedException{
doSecurityCheck();
}
public void businessMethod2() throws UnauthorizedException{
doSecurityCheck();
}
public void requiresNoSecurityCheck() {
}
public void doSecurityCheck() throws UnauthorizedException{
}
}
這里,安全檢查就是一個(gè)aspect,需要進(jìn)行安全檢查的這幾個(gè)方法就是join point。而由于不是所有的方法都需要進(jìn)行安全檢查,所以就需要用pointcut來進(jìn)行匹配。
下面使用了一個(gè)interceptor來將關(guān)注點(diǎn)模塊化:
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class SecurityInterceptor implements MethodInterceptor{
public Object invoke(MethodInvocation invocation)throws Throwable{
doSecurityCheck();
return invocation.proceed();
}
public void doSecurityCheck{}
}
這里的interceptor就是advice