|
[aop]
Interception == AOP (finally true ;-)
[
jonas
]
One of the new features in the AspectWerkz 2 architecture is that it comes with a full-blown interception framework that allows per instance programmatic deployment with most of the AOP semantics preserved. You can make use of this using both the regular load-time weaving or the AW Proxy (that I blogged about here)
In short you will get:
POJO pojo = new POJO();
// adds tracing to all methods in the 'pojo' instance
((Advisable) pojo).aw$addAdvice(
"* *.*(..)",
new BeforeAdvice() {
public Object invoke(JoinPoint jp) {
System.out.println("Entering: " + jp.getSignature().toString());
}
}
);
The Advisable interface
From a users perspective pretty much all you need to know about is in the
The
aw$
is just to minimize method clashes since these methods are added to your classes on-the-fly.
The different Advice interfacesThe intercept framework supports all main types of advice defined in AOP today:
JoinPoint
instance as a parameter. This class contains f.e. contextual information bout the join point (member) we are executing before/after/around. Such as caller and callee instances and types, argument values and types etc. You can also see that some of the advice takes an optional parameter which provides direct access to the return value or the exception instance.
Preparing your application
To make this work you finally need to tell AspectWerkz which classes you want to make "advisable" and to which extent. This is done in the
Example:
<aspectwerkz>
<system id="intercept-sample">
<advisable expression="within(my.application.domain.*)" pointcut-type="call|set|get"/>
</system>
</aspectwerkz>
Bringing it all together
So now we have talked about the
In this example we are taking a regular POJO and we are adding an advice that will be applied to all methods that are annotated with the annotation
...
POJO pojo = new POJO();
((Advisable) pojo).aw$addAdvice(
"@OneWay * *.*(..)",
new AroundAdvice() {
private Executor m_threadPool = Executors.newCachedThreadPool();
public Object invoke(JoinPoint jp) throws Throwable {
m_threadPool.execute(
new Runnable() {
public void run() {
try {
// proceed with the invocation in a new thread
jp.proceed();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}
);
return null;
}
}
);
...
The META-INF/aop.xml
file looks like this:
<aspectwerkz>
<system id="intercept-sample">
<advisable expression="within(sample.intercept.POJO)" pointcut-type="call"/>
</system>
</aspectwerkz>
Resources
I have not written any specific sample application for this article but if you want you can look at and run the tests in the AspectWerkz distribution. You can download the distribution here. The tests are in You might be interested in an old post of mine, entitled "Dynamic Proxies != AOP" - http://www.magpiebrain.com/archives/2004/07/13/interceptors - where I argue that the use of dynamic proxies (and therefore method interception) is an implementation detail of some AOL implementations, not the same thing as AOP. --Sam Newman, January 7, 2005 01:51 PM
Post a comment
|