Advices
The
ImplementationThere are currently four types of Advices supported:
AroundAdvice
An
Please note: An
Here is a simple example of an
public class MyAroundAdvice extends AroundAdvice { public MyAroundAdvice() { super(); } public Object execute(final JoinPoint joinPoint) throws Throwable { // do some stuff Object result = joinPoint.proceed(); // do some more stuff return result; } } PreAdvice
A
Here is a simple example of a
public class MyPreAdvice extends PreAdvice { public MyPreAdvice() { super(); } public void execute(final JoinPoint joinPoint) throws Throwable { // do some stuff } } PostAdvice
A
Here is a simple example of a
public class MyPostAdvice extends PostAdvice { public MyPostAdvice() { super(); } public void execute(final JoinPoint joinPoint) throws Throwable { // do some stuff } } ThrowsAdvice
A
Here is a simple example of a
public class MyThrowsAdvice extends ThrowsAdvice { public MyThrowsAdvice() { super(); } public void execute(final JoinPoint joinPoint) throws Throwable { Throwable cause = ((ThrowsJoinPoint)joinPoint).getException()); // do some stuff } } DefinitionAttributes When defining the advices there are three attributes that needs to be specified:
Here you also have the possibility to pass parameters to the advices. See the Passing parameters to advices section for a detailed description. XML definition
<advice-def name="advices/caching" class="advices.CachingAdvice" deployment-model="perInstance"> <param name="timeout" value="10"/> </advice> Introductions
The
ImplementationBoth the interface and the implementation extensions are just regular Java interfaces and classes. There is no need to have them implement a specific interface or extend a certain class. All you have to do is define them. The only rules are that the introduced implementation has to implement the introduced interface and have a default no-argument constructor. When defining an interface introduction you only have to specify the interface, but when introducing a new implementation to a class, you must specifiy both the implementation class a matching interface. This is needed since if you don't specify an interface that the client can cast the target obje ct to, the introduced implementation will not be accessible.
Note: if you add more than one
DefinitionAttributes When defining the introductions there are four attributes that needs to be specified:
XML definition
<introduction-def name="java/io/Serializable" interface="java.io.Serializable"/> <introduction-def name="mixins/Mixin" interface="mixins.Mixin" implementation="mixins.MixinImpl" deployment-model="perThread"/>
(These examples only shows how to define the
Pointcuts
The
ImplementationThere are currently four different types of pointcuts supported:
DefinitionAttributes When defining the pointcuts there are three attributes that needs to be specified:
XML definition
<aspect ...> <pointcut-def name="pc1" type="method" pattern="* foo.Bar.method(..)"/> <pointcut-def name="pc2" type="setField" pattern="* foo.Bar.m_field"/> <pointcut-def name="pc3" type="throws" pattern="* foo.Bar.method(..)#java.lang.Exception"/> <pointcut-def name="pc4" type="callerSide" pattern="foo.Caller->String foo.Callee.method()"/> <pointcut-def name="pc5" type="cflow" pattern="* Transaction.begin(..)"/> ... </aspect> Aspects
The
DefinitionAbstract aspects - aspect inheritance
You have the possibility of defining abstract aspects that you
can reuse by letting aspects inherit the the abstract aspect using
the
Attributes When defining the aspects there is first one attribute that needs to be specified:
Adding the pointcuts definitions In the aspect definition you put the pointcut definitions (see the Pointcuts section ). Adding the introductions
Then you specify the introductions that you want to define
in this
This element has an attribute
Adding the advices
You also define which advices should be applied to which pointcuts.
This is done using the
The
This expression can be any (almost) kind of algebraic expression.
The only difference is that you have to use
Here you also define if the pointcut/expression should be a part of a
control flow using the
XML definition
<abstract-aspect name="MyAbstractAspect"> <advice cflow="facadeCalls" pointcut="setters AND !getters"> <advices-ref name="log_and_cache"/> </advice> <advice pointcut="persistentFields"> <advice-ref name="persistent"/> </advice> </aspect> <aspect name="MyAspect" extends="MyAbstractAspect"> <introduction class="domain.*"> <introduction-ref name="serializable"/> <introduction-ref name="mixin"/> </introduction> <pointcut-def name="facadeCalls" type="cflow" pattern="* *..facade.*.*(..)"/> <pointcut-def name="setters" type="method" pattern="String domain.*.set*(..)"/> <pointcut-def name="getters" type="method" pattern="String domain.*.get*(..)"/> <pointcut-def name="persistentFields" type="setField" pattern="* domain.*.*"> </aspect> Join points
The
ImplementationThere are four different types of join points:
You only have to deal with the different types of
join points when you in your
|