How to declare Around Advice

This example shows how to write an aspect as a POJO with no AOP related info defined in it. All the AOP related info is defined in XML configuration file.

Following example contains an aspect named which contains one advice named ourAroundAdvice().

Around advice is a combination of before, after-returning and after-throwing advices. Around advice is wrapped around a method call and called both before and after of a method execution. The difference is that in before and after advices there are two separate advices but here there is only one advice containing logic of all three (before, after-returning and after-throwing  ) advices.


1) Write EmployeeService interface and its implementation EmployeeServiceImpl.
2) Write MainApp class which use methods of  EmployeeService interface.
3) Write aspect class named
4) Define pointcuts(methods) where advices would be applied in XML file.

Technologies used

1) Spring-framework-3.0.6.RELEASE
2) Eclipse Java EE IDE Helios Release (Eclipse version 3.6)
3) Java SE 6


 package examples.springaop.xml;

public interface EmployeeService{
    public void addEmployee();
    public String getEmployeebyName(String id);

package examples.springaop.xml;

public class EmployeeServiceImpl implements EmployeeService{
    public void addEmployee(){
        System.out.println("In the addEmployee() method.");

    public String getEmployeebyName(String id){
        System.out.println("In the getEmployeebyName() method.\n");

            throw new NullPointerException("ID cannot be null!");
        String empName = "Name"+id;
        return empName;

 package examples.springaop.xml;

import org.springframework.context.ApplicationContext;

public class MainApp{
public static void main(String args[]){
    ApplicationContext appContext = new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
    EmployeeService empService = (EmployeeService)appContext.getBean("empService");
    System.out.println("First Call to method :");
    /*Causing exception to be thrown by this method*/
    System.out.println("Second Call to method :");


 package examples.springaop.xml;

import org.aspectj.lang.ProceedingJoinPoint;

public class OurAspect{

public void ourAroundAdvice(ProceedingJoinPoint method){
          System.out.println("Before-Advice Part:This is called before the method exceution.\n");
     try {
        System.out.println("After-Returning-Advice Part: This is called after the method returns nomally.\n");
    } catch (Throwable e) {
        System.out.println("After-Throwing-Advice Part: This is called after the method throws exception.\n");

Note that unlike other advices, Around advice takes one parameter of type ProceedingJoinPoint interface as:

 public void ourAroundAdvice(ProceedingJoinPoint method){
// logic for before advice
// logic for After advice

This parameter represents the target method on which advice has been applied.

Also note how we called the target method inside the around advice using proceed() method.

 <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""




<bean id="empService" class="examples.springaop.xml.EmployeeServiceImpl"/>

<bean id="empAspect" class="examples.springaop.xml.OurAspect"/>


    <aop:aspect ref="empAspect">

   <aop:pointcut id="getEmp" expression="execution(* examples.springaop.xml.EmployeeService.getEmployeebyName(..))" />

    <aop:around pointcut-ref="getEmp" method="ourAroundAdvice"/>





First Call to method :

Before-Advice Part:This is called before the method exceution.

In the getEmployeebyName() method.

After-Returning-Advice Part: This is called after the method returns nomally.
Second Call to method :

Before-Advice Part:This is called before the method exceution.

In the getEmployeebyName() method.

After-Throwing-Advice Part: This is called after the method throws exception.

Note that in second call to getEmployeebyName() method instead  of After-returning advice After-Throwing advice has been called because here getEmployeebyName() method has thrown an exception.

Jars Used

I would like to know your comments and if you liked the article then please share it on social networking buttons.

No comments:

Post a Comment