Spring AOP With Annotation

The common AspectJ annotations are

  • @Before Run before the method execution
  • @After Run after the method returned a result
  • @AfterReturning Run after the method returned a result and also intercept the returned result as well.
  • @AfterThrowing Run after the method throws an exception.
  • @Around Run around the method execution, combine all three advices above.

@Before Example

StudentBO.java File
package com.jtechies.bo;

public class StudentBO {

	public void addStudent() {
		System.out.println("addStudent() is running...");
	}	
	public String addStudentReturnValue(){
		System.out.println
			("addStudentReturnValue() is running ");
		return "parker";
	}
 
	public void addStudentThrowException()
		throws Exception{
		System.out.println
			("addStudentThrowException() is running ");
		throw new Exception("Some Exception has occured");
	}
 
	public void addStudentAround(String name){
		System.out.println("addStudentAround() 
			is running, args : " + name);
	}
}
AspectDemo.java File
package com.jtechies.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AspectDemo {
	
	@Before("execution
		(* com.jtechies.bo.StudentBO.addStudent(..))")
	public void logBefore(JoinPoint joinPoint) {
		System.out.println("@Before() is running! ");
		System.out.println("Method Name is  " + 
			joinPoint.getSignature().getName());
	}
}
Test.java File
package com.jtechies.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support
		.ClassPathXmlApplicationContext;

import com.jtechies.bo.StudentBO;

public class Test {
	public static void main(String[] args)
		throws Exception{
	  ApplicationContext context = new 
	  	ClassPathXmlApplicationContext("spring.xml");
	  StudentBO student = 
	  	(StudentBO)context.getBean("studentBo");
	  student.addStudent();
	}
}
spring.xml File
<?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:aop=
		"http://www.springframework.org/schema/aop"
	xsi:schemaLocation=
		"http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
   <aop:aspectj-autoproxy />

   <bean id="studentBo"
   	class="com.jtechies.bo.StudentBO"/>
    
   <!-- Aspect -->
    
    <bean id="logAspect"
    	class="com.jtechies.aspect.AspectDemo" />
</beans>
Output
@Before() is running! 
Method Name is  addStudent
addStudent() is running...

@After Example

All Other Files are same except AspectDemo.java file
AspectDemo.java File
package com.jtechies.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AspectDemo {
	
	@After("execution
		(* com.jtechies.bo.StudentBO.addStudent(..))")
	public void logAfter(JoinPoint joinPoint)
	{	
		System.out.println("Method Name is = "+
			joinPoint.getSignature().getName());
		System.out.println("@After is running...");	
	}
}
Output
addStudent() is running...
Method Name is = addStudent
@After is running...

@AfterReturning Example

All Other Files are same except AspectDemo.java and Test.java file
AspectDemo.java File
package com.jtechies.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AspectDemo {
	
	@AfterReturning(pointcut = "execution
			(* com.jtechies.bo.StudentBO
			.addStudentReturnValue(..))",
			returning= "result")
	public void logAfterReturning
		(JoinPoint joinPoint, Object result) {

		System.out.println("@AfterReturning is running!");
		System.out.println("Method name =  " + 
			joinPoint.getSignature().getName());
		System.out.println
			("Method returned value is : " + result);
	}
}
Test.java File
package com.jtechies.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support
		.ClassPathXmlApplicationContext;

import com.jtechies.bo.StudentBO;

public class Test {
	public static void main(String[] args)
		throws Exception {
	  ApplicationContext context = new
	  	ClassPathXmlApplicationContext("spring.xml");
	  StudentBO student = (StudentBO)
	  	context.getBean("studentBo");
	  student.addStudentReturnValue();
	}
}
Output
addStudentReturnValue() is running 
@AfterReturning is running!
Method name =  addStudentReturnValue
Method returned value is : parker

@AfterThrowing Example

All Other Files are same except AspectDemo.java and Test.java file
AspectDemo.java File
package com.jtechies.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AspectDemo {
	
	@AfterThrowing(pointcut = "execution
		(* com.jtechies.bo.StudentBO
		.addStudentThrowException(..))",
			throwing= "error")
	public void logAfterThrowing
		(JoinPoint joinPoint, Throwable error) {

		System.out.println("@AfterThrowing is running!");
		System.out.println("Method name is = " +
			joinPoint.getSignature().getName());
		System.out.println("Exception : " + error);
	}
}
Test.java File
package com.jtechies.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support
		.ClassPathXmlApplicationContext;

import com.jtechies.bo.StudentBO;

public class Test {
	public static void main(String[] args)
		throws Exception {
	  ApplicationContext context = new
	  	ClassPathXmlApplicationContext("spring.xml");
	  StudentBO student = (StudentBO)
	  	context.getBean("studentBo");
	  student.addStudentThrowException();
	}
}
Output
addStudentThrowException() is running 
@AfterThrowing is running!
Method name is = addStudentThrowException
Exception : java.lang.Exception: Some Exception has occured

@Around Example

All Other Files are same except AspectDemo.java and Test.java file
AspectDemo.java File
package com.jtechies.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AspectDemo {
	
	@Around("execution
	(* com.jtechies.bo.StudentBO.addStudentAround(..))")
	public void logAround(ProceedingJoinPoint joinPoint)
		throws Throwable {

		System.out.println("@Around is running!");
		System.out.println("Method Name = " +
		joinPoint.getSignature().getName());
		System.out.println("Arguments Name = : " +
		Arrays.toString(joinPoint.getArgs()));
		
		System.out.println("Around before is running!");
		joinPoint.proceed();
		System.out.println("Around after is running!");
	}
}
Test.java File
package com.jtechies.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support
		.ClassPathXmlApplicationContext;

import com.jtechies.bo.StudentBO;

public class Test {
	public static void main(String[] args)
		throws Exception {
	  ApplicationContext context = new
	  ClassPathXmlApplicationContext("spring.xml");
	  StudentBO student = (StudentBO)
	  	context.getBean("studentBo");
	  student.addStudentAround("jtechies.com");
	}
}
Output
@Around is running!
Method Name = addStudentAround
Arguments Name = : [jtechies.com]
Around before is running!
addStudentAround() is running, args : [jtechies.com]
Around after is running!