Showing posts with label Aspect Oriented Programming (AOP). Show all posts
Showing posts with label Aspect Oriented Programming (AOP). Show all posts

Tuesday, August 12, 2014

Spring AOP POJO Way

In Spring pure POJO way of defining aspects we can convert any POJO as aspects. The POJO itself does not knows that it is being used as Aspect.
Write a pure POJO class
PojoHelloWorldDecorator
public class PojoHelloWorldDecorator {
    // Message we intend to print before entering

Spring AOP in Programmatic Way

Spring 1.2 introduced ((AOP|Aspect Oriented Programming)) in a programmatic way. The AOP is applied by using ProxyFactory class. The factory class is provided with both the advice and the taregt.
Let's write an advice which will print a message before and after a HelloWorld program. Let's assume HelloWorld has a printMessage function.

HelloWorldDecorator

//Notice that MethodInterceptor comes from AOP alliance packages and

Spring Annotations AOP

Spring supports AOP by using ((Annotation)). Let's write an advice which will print a message before and after a HelloWorld program. Let's assume HelloWorld has a printMessage function.

Aspect class : AroundHelloWorldDecorator 

//Annotation to mark that this is an aspect class
@Aspect
public class AroundHelloWorldDecorator{
    
   //Around advice with pointcut which says that this advice needs to be applied to 

Spring AOP Concepts

AOP stands for Aspect Oriented Programming. For details see AOP. Spring AOP is based on dynamic AOP. Spring provides the AOP feature by using proxies at runtime. Also Spring is limited to method execution joinpoint. What it means is that Spring only provides AOP feature on method execution.

Spring supports proxy creation either by using JDK dynamic proxy or using cglib proxy. The default is JDK dynamic proxy. The proxy is based on the interface that the target class implements. For a method to be advised the interface should also have the method in it. If the class do not implements any interface than Spring resorts to cglib based proxy mechanism. In this the class definition is

AOP in Spring based Web Applications

Recently I was working on a AOP based application and was trying to put a performance measurement aspects on the service layer. With the aspect, the idea was to measure how much time it takes for the service layer to execute. The aspect looked like

public class MyAspect {
    @Around("within(com.lalit.service..*)")
    public Object doLogging(ProceedingJoinPoint pjp) throws Throwable {
        //start the timer
        Object obj = pjp.proceed();
        //stop the timer and display the time
        return obj;
    }
}

Sunday, July 27, 2014

Aspect Oriented Programming (AOP)

Suppose your manager comes a day before you are going to ship the code to customer and asks you to put a logging code at each method calls exit and entry. There are couple of hundred class files and each class file on an average twenty methods. What you do? Go for a long vacation, if possible. These are the kinds of problem that AOP aims to solve.
AOP stands for Aspect Oriented Programming. AOP is not a a supplement or replacement of OO Programming. Rather it compliments with OO programming. When we develop an application,