|
[aop]
Spring and AspectWerkz: A Happy Marriage v2
[
jonas
]
The AspectWerkz 2 architecture has been designed to be an Extensible Aspect Container, which can deploy and run arbitrary aspects. You can read more about the details in this article. In this article I will show you how you can make use of this container to run Spring AOP aspects more performant plus utilize the annotation-driven AOP in AspectWerkz along with its more expressive pointcut pattern language. In this article I am using Spring as an example but everything covered here applies to all frameworks that implements the AOP Alliance interfaces (e.g. dynaop, JoyAop, JAC etc.). Write a regular Spring adviceFirst we write a regular spring advice that implements authentication:
public class AuthenticationAdvice implements MethodBeforeAdvice {
public void before(Method m, Object[] args, Object target) throws Throwable {
// try to authenticate the user
// if not authenticated throw a SecurityException
}
}
Define the advice using Java 5 annotations
In this example we will not define this advice using the slightly verbose Spring config file. But make use of Java 5 annotations. In AspectWerkz you have a set of predefined annotations that we can use, f.e. one for each advice type. The advice we have implemented here is a before advice so we will use the
public class AuthenticationAdvice implements MethodBeforeAdvice {
@Before("authenticationPoints")
public void before(Method m, Object[] args, Object target) {
// try to authenticate the user
// if not authenticated throw a SecurityException
}
}
Here we bind the advice to the pointcut named "authenticationPoints"
, this pointcut will pick out all the points where we want authentication to take place. However we do not define this pointcut yet. Since we want to make this aspect reusable, then it is better to just compile it, put it in a jar and then at deployment time resolve this definition by defining the pointcut in the external META-INF/aop.xml
file.
We can of course choose to not define the pointcut in an external XML file but directly in the
@Before("call(@RestrictedOperation * *.*(..))")
public void before(Method m, Object[] args, Object target) {
...
}
if we think that that is beneficial. (Then the META-INF/aop.xml
only have to define the aspect class name, see below for details).
Define the pointcut in the external aop.xml file
Now all we need to do put this advice to work is to write the little
<aspectwerkz>
<system id="spring-extension-sample">
<aspect class="my.application.aspects.AuthenticationAdvice">
<pointcut name="authenticationPoints" expression="call(@RestrictedOperation * *.*(..))" />
</aspect>
</system>
</aspectwerkz>
Here we defined the pointcut to pick out all method calls to a method that is marked with the annotation @RestrictedOperation
.
Note that we are using a call pointcut here, which means that this advice will execute on the client side and not the server (something that is not possible with regular spring aop which only supports execution pointcuts). This can be beneficial in many situations, it can f.e. can save us a remove call from the client to the server, or take some load off the server if the client is executing in a separate VM, etc. Start up the applicationThe last thing we need to do is to add two VM options:
aspectwerkz-core-RC2.jar
, aspectwerkz-RC2.jar
, aw-ext-spring-0.1.jar
and aw-ext-aopalliance-0.1.jar
plus the regular AspectWerkz dependency jars on the classpath. (You find the aspectwerkz jars in the aspectwerkz distribution and the aw-ext-*.jar jars you need to build yourself (see Resources for details).
The you can start up the application as usual using
We have been thinking of providing a way of defining the spring aspect model on the application level (per What about my Spring bean config file, dependency injection etc?Good news is that you can still use your regular Spring bean config file and let Spring do its job, with dependency injection, bean configurations etc. AspectWerkz has a pluggable factory mechanism for the aspect instantiation and life-cycle management. We have written a factory for the Spring framework that allows you to use Spring to configure your aspects/advice just as usual. Read more about that here. Much better performanceApart from allowing defining your aspects using Java5 annotations and using the semantics of the AspectWerkz pointcut language and weaver, you will also get much better performance (ranging between 1300 to 50 percent). I won't go into detail on that here but you can read more about it in this article. DrawbacksEverything has tradoffs and there are of course some drawbacks in using the AspectWerkz Extensible Aspect Container as the runtime environment for your Spring aspects/advice:
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
The I tried to modify tests from distribution of aspectwerkz-2.0 to use annotation approach described here, but without success. Looks like What have I missed ? Regards, --Igor Poteryaev, July 5, 2005 01:36 PM
Post a comment
|