Spring AOP 2.0 in a nutshell


Addresses cross-cutting concerns, i.e. functionality that affects application in many points, e.g. logging, security, transaction managament – many methods have logging functionality applied. Cross-cutting concerns can be modularized into special objects, called aspects. This functionality is defined in one place and you declaratively specify when and where it is used without changing the code of the classes (called target) for which you apply these new features. Let’s say that the business logic is to call a method the sings a song and we want to intercept this method before it is envoked, after it is returned, when it throws an exception and and new feature at that moment of program execution. The advice defines when to intercept the method (before,after-returning, after-throwing, around (effectively first three altogether) and introduction) and what to do when it is intercepted, i.e. what an aspect does and when it does it. In the next step, we need to specify the method to which the interception is applied. We can define where in the code the advice should be applied by specifying joinpoints. The regular expression matching a (sub)set of joinpoints is called pointcut, e.g. in AspectJ-style:

execution(<return_type> <class_name>.<method_name>(<set_of_params>) e.g.:

execution(* *.perform(..))’ matches method named ‘perform’ on any class with any number of arguments

execution(* Performer+.perform(..)) matches ant perform method which is executed on a Performer, returning any type.

An advisor specifies the advice and the pointcut.

In spring AOP pointcuts define only methods, the AspectJ pointcuts are more general. Internally once the target is advised, the proxy object is created, that wraps the String-managed beans. This process is called weaving. The weaving can be done during at compilation and classload time (AspectJ) and during the runtime, i.e. execution of the program (Spring AOP).

Sping AOP can be configured in 4 ways:

– classic proxy-based AOP (all versions of Spring)

– @AspectJ annotation-driven aspects (from Spring 2.0)

– pure-POJO aspects (from Spring 2.0)

– injected AspectJ aspects (all versions of Spring)

First three solutions are proxy-based AOP, limited to method interception. For other interception (e.g. property or contructor) consider injecting Spring beans into AspectJ aspects (defines more fine-grained control, but yet another language to learn 😉

In Spring AOP the aspects are implemented as proxies that handles method calls, perform additional aspect logic and then invokes the target methods. Spring does not create proxied classes until they are needed by the application, when the beans are loaded from BeanFactory.

If your target proxy implements an interface(s), that exposes required methods, then Spring will generate a new class that implements necessary interfaces, weave in any advice and proxy any calls to these interfaces to your target class (preferable solution).

Otherwise, Spring will generate subclass to your target class and delegate calls to the subclass to your target class. Methods marked as final in target class are not allowed

Examplary configuration


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s