Aspect Oriented Programming with Spring

Introduction

In an ideal Object Oriented System, we would want to design each object to perform one specific task. However apart from performing its main task the objects also performs passive task like logging, transactions, security, caching etc. These passive activities which are necessary but not part of business logic are called “Cross Cutting Concerns“.

(Cross Cutting Concerns == Commonly Used Functionality across the system)

Separating Cross Cutting Concerns from business logic can be a major step towards writing an well designed decoupled code. Let us ponder over ways to deal with separating Cross Cutting Concerns

Inheritance

Inheritance pops up in the mind straight away, we can inherit the common functionality and use it in our objects. But Inheriting the common functionality requires us to design us a base class. If we reuse this class in multiple places then modifying the class could be a tough job later on.

Inheritance == Difficult to modify later(inelastic code)

Delegation

Delegation is a better way of dealing with the cross Cutting Concerns. Remember Composition over Inheritance, (delegation and composition share a common concerns). But Then we would have to make calls to delegate objects at many places thus making it cumbersome.

Delegation == Cumbersome

Aspect Oriented Programming

Does this mean we are in soup. Rather not, this leaves us with a third and the best approach of all, Aspect Oriented Programming. AOP spares us the brittleness of Inheritance and cumbersomeness of delegation. AOP shines in area of separting cross cutting concerns

What is AOP?

AOP allows us to modularize cross cutting concerns into special objects called Aspects, thereby creating a cleaner and decoupled code. With aspects in place objects no longer have to worry about performing the passive cross cutting concerns as AOP takes care of it all.

Terminologies related to AOP

Like any successful technologies AOP comes with its own sets of jargon and terminologies. Let us take a glance at those before moving to more serious business of understanding AOP.

Concerns – These are the part of system modularized based on their functions. There are two types of concerns. 1. Core concerns 2. Cross Cutting concerns. Core concerns are related to the business logic of the system i.e active tasks that system performs like generating salary slip, getting employee record, doing bank transfer etc. Cross cutting concerns are the passive tasks that are required to perform the active tasks like logging, caching etc.

Joinpoint – Joinpoint is a point in execution flow where some action takes place and a possibility arises to apply an Aspect(cross cutting concern). A joinpoint can be method being invoked, exception being thrown or change in state of an object.

Advice – Each Aspect in AOP has a purpose i.e. the job it has to do. This job has to be applied at a Joinpoint. Job or purpose of an Aspect is called Advice. Apart from defining the job of aspect, Advice also defines the time when the Aspect is to perform the job. Should the job be applied before or after or both before and after a core concern finishes its execution.

Pointcut – There can be many Joinpoints in the system, but not all are chosen to be advised by an Aspect. Aspect takes help from Pointcut to choose the Joinpoint where advise is to be woven.

Aspect – Advice and Pointcut defines an Aspect. As we saw that Advice defines the job of an Aspect and when to perform it. While Pointcut defines the location where aspect weaves it advice. So what, when and where of a job defines the Aspect.

Target – Target is the object which is being adviced. (Core Concern). With help of AOP this object is free to perform its primary task without worrying about cross cutting concerns.

Proxy – When advice is applied to a target object a Proxy object is created. AOP container creates and manages the lifecycle of object and programmers need not worry about them.

Weaving – Weaving is the process of applying Advice or Aspect to the target object to create the proxy object. Weaving can be done at compile time or classloading time or at runtime. Typically Spring AOP weaves aspect in the target object at runtime.

That’s a long list of terms to digest. Take your time in understanding them before moving on.

Types of Advice

One Final piece before indulging in an example is to learn about type of advice. Mainly there are 4 types of advice.

Before Advice – Before advice is applied before the Joinpoint starts execution. BeforeAdvice is created by implementing org.springframework.aop.MethodBeforeAdvice interface. The method to be implemented is public void before(Method m, Object args[], Object target) throws Throwable

After Returning Advice – After advice is applied after the Joinpoint completes executing. AfterReturningAdvice is created by implementing org.springframework.aop.AfterReturningAdvice interface. The method to be implemented is public void afterReturning(Method m, Object args[], Object target) throws Throwable

Around Advice – This advice surrounds the Joinpoint execution and is executed before and after Joinpoint execution. This can even be use to control the invocation of a Joinpoint.

Example

We will try to develop a simple cache with help of SpringAOP. Caching has three main core concerns.

Core Concerns

Save object in Cache.

Return object from Cache.

Delete object from Cache.

Now apart from these core concerns caching framework has other passive task. These passive tasks forms the cross cutting concern.

Cross Cutting Concerns

Re-sizing the cache when it reaches its size limit. (LRU) implementation.

Locking an object to prevent from deletion when it is being read.

Locking the cache to prevent and read/writes/deletes when it is getting re-sized.

Coding for all these cross cutting concerns can be time taking and tedious so let us simplify the example and we will just implement the re-size logic when the cache is full. So after example is done we will have a cache where we can put, get and delete objects. There is a max size of cache which has been set to 10 in example. Once the cache stores 10 object then any addition to the cache will result in deletion (re-sizing) of cache by deletion of first object. The re-sizing operation is controlled by an Aspect created using Spring AOP. Here are the steps to be followed in the example

There is nothing much to say about this class. There are two LinkedHashMaps one which stores the object and the other stores the timestamp when object was pushed in the cache. The max size is set to 10 and it has get, put and delete methods. Also there is a re-size method which will be called by Aspect as we will check later.

As you can see this is a method before advice. Class implements MethodBeforeAdvice interface which contains a single mthod before(). If you examine the method you will check that rezise method is called when ever we call a put method.

If you notice the above xml file, both MyCache and ResizeAdvice have been registered as spring bean. The main bean in the file is myAOPCache. This is the proxy object that spring aop creates after applying the advice on the core class. The proxy object is created by ProxyFactoryBean class. We pass a reference of myCache object to the proxy object and also register all the advice which are to be applied to the proxy classes.

In this class we start the spring container and load the beans present in spingaopdemo.xml. We push 10 objects in the cache and when we try to push the 11th object then the first one is deleted and 11th inserted. Output is big so I am not posting the output. Run the class and check the output to your satisfaction.

Summary

In this post we learnt how to better deal with cross cutting concerns using Aspect Oriented Programming. AOP is a powerful concept that allows us write cleaner and decoupled code. AOP does not provide any thing new. All it does is to segregate the business logic from other mundane tasks that system has to perform. It enables reuse of code implementing system wide cross cutting concerns. We also learnt the various terminologies associated with AOP. Last but not the least we saw a simple example where we created a simple before method advice using Spring AOP and applied it to manage our caching system.

Note

You can freely use and distribute the caching system developed in this code. Though using it in production system is not advisable.

As and always I intend this post as a launching platform for collective learning, feel free to drop in a comment or two about what you feel about AOP and how you to plan to use it in your code. Happy reading.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Join Us

With 1,043,221 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!