Spring AOP with Examples

Aspect Oriented Programming (AOP) deals with workflow management. Concept of AOP states that workflow must be separated from processing logic because the workflow changes by the time if workflow controlled programmatically then each time workflow changes code need to be modified.

In technical term AOP is an alternative programming model of implementing cross-cutting concerns. A concern represents an independent operation which is performed as part of a task if an operation participates in multiple tasks. Then it is said to be cross-cutting.

  • Maintenance is simplified because number or order of concerns can be changed without affecting or modifying code.

  • Productivity is increased as programmer only need to focus on business logic.

There are following terminologies used in AOP:

  • Advice
    Implementation of a concern is called an advice. Different types of advice include “around”, “before” and “after” advice.

  • Join point
    It is uniquely identifiable event in the application which can be used to associate advice to methods. Logging in class,creation of objects,method invocation etc are example of join point.

  • Pointcut
    It is collection of join points.

  • Aspect
    It is collection of pointcut and advice.

  • Target object
    It is referred to as the advised object. In Spring AOP implemented using runtime proxies, target object will always be a proxied object.

  • AOP proxy
    It is an object created by the Spring AOP framework in order to invoke method etc. In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.

  • Weaving
    It is process of associating advice to the methods. Spring AOP supports runtime weaving.

List of annotations used to create advices are given below:

  • @Before declares the before advice. It is applied before calling the actual method.

  • @After declares the after advice. It is applied after calling the actual method and before returning result.

  • @AfterReturning declares the after returning advice. It is applied after calling the actual method and before returning result. But you can get the result value in the advice.

  • @Around declares the around advice. It is applied before and after calling the actual method.

  • @AfterThrowing declares the throws advice. It is applied if actual method throws exception.

  • Spring Core module jars

  • Spring AOP module jars

For Before Advice we need to use @Before Annotation.

The “JbkStudent” class is target class which method m1() we need to invoke. Before invoking m1() method we want some preprocessing as below:

JbkMessageAspect.java

package com.javabykiran.springaop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class JbkMessageAspect {
    @Before("execution (* com.javabykiran.springaop.JbkStudent.m1())")
    public void mx() {
        System.out.println("Welcome To Java By Kiran");
    }
}

JbkStudent.java

package com.javabykiran.springaop;
public class JbkStudent {
    public void m1() {
	System.out.println("JavaByKiran");
    }
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop 
	   http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- annotaion enabling -->
	<context:annotation-config />

	<aop:aspectj-autoproxy />

	<bean id="stud" class="com.javabykiran.springaop.JbkStudent"></bean>
	<bean id="mes" class="com.javabykiran.springaop.JbkMessageAspect"></bean>
</beans>   

JbkClient.java

package com.javabykiran.springaop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JbkClient {
    public static void main(String[] args) {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	JbkStudent stu = ctx.getBean("stud", JbkStudent.class);
	stu.m1();
    }
}   

For After Advice we need to implement AfterReturningAdvice interface and then afterReturning() method need to define.

The “A” class is target class which method display() we need to invoke. Before invoking display() method we want some postprocessing as below.

For After Advice we need to use @After Annotation. The “JbkStudent” class is target class which method m1() we need to invoke. Before invoking m1() method we want some preprocessing as below.

JbkMessageAspect.java

package com.javabykiran.springaop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class JbkMessageAspect {
    @After("execution (* com.javabykiran.springaop.JbkStudent.m1())")
    public void mx() {
	System.out.println("Java By Kiran is Best Institute in Pune");
    }
}

JbkStudent.java

package com.javabykiran.springaop;
public class JbkStudent {
    public void m1() {
	System.out.println("JavaByKiran");
    }
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
	   http://www.springframework.org/schema/aop 
	   http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- annotaion enabling -->
	<context:annotation-config />

	<aop:aspectj-autoproxy />

	<bean id="stud" class="com.javabykiran.springaop.JbkStudent"></bean>
	<bean id="mes" class="com.javabykiran.springaop.JbkMessageAspect"></bean>
</beans> 

JbkClient.java

package com.javabykiran.springaop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JbkClient {
    public static void main(String[] args) {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	JbkStudent stu = ctx.getBean("stud", JbkStudent.class);
	stu.m1();
    }
}