Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

How to implement After Returning Advice using @AspectJ Annotation-Driven AOP in Java ?.

A simple application to demonstrate how to implement After Returning Advice using @AspectJ Annotation-Driven AOP in Java.

Step 1:- Create a Interface

package com.hubberspot.aspectj.annotation.afterreturningadvice;

// Its a simple interface for the Sum service.
// It contains one single method called as addition().
// This method takes in two arguments both of the type
// int.
public interface Sum {

 public int addition(int a , int b);


Step 2:- Service Implementation Class

package com.hubberspot.aspectj.annotation.afterreturningadvice;

// It is the implementation class for the 
// Sum service. It just calculates and returns 
// sum of two numbers passed to it as arguments.

public class SumImpl implements Sum {

 public int addition(int a, int b) {
  System.out.println("Addition method called ...");
  return (a + b);



Step 3:- After Returning Advice Implementation class

package com.hubberspot.aspectj.annotation.afterreturningadvice;

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

//@Aspect annotation treats this Java class as 
//Aspect. Its not ordinary POJO class.

public class AfterReturningSumAdvice {

 // Method afterReturning() is a After Returning advice
 // implemented by providing @AfterReturning annotation.
 // This annotation takes in Pointcut expression, which 
 // indicates when this advice executes.
 // This Pointcut expression tells that afterReturning() advice
 // will execute after successful return from the method 
 // addition Sum interface.
 // The after advice takes in JoinPoint and an Object. JoinPoint 
 // which here represents method execution and pointcut expression
 // takes in returning property and makes it able to store, available 
 // to the afterReturning method.

 @AfterReturning( pointcut="execution(* com.hubberspot.aspectj.annotation.afterreturningadvice.Sum.addition(..)))"
   , returning="result")
 public void afterReturning(JoinPoint joinPoint , Object result) {

  System.out.println("Sum of two integers after return is : " + result);



Step 4 :- Spring Configuration file

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


   <!-- Implementation Class -->
     <bean id="sum"
 class="com.hubberspot.aspectj.annotation.afterreturningadvice.SumImpl" />

     <bean id="afterReturningAdvice"
 class="com.hubberspot.aspectj.annotation.afterreturningadvice.AfterReturningSumAdvice" />


Step 5:- Test class

package com.hubberspot.aspectj.annotation.afterreturningadvice;

import org.springframework.context.ApplicationContext;

public class SumTest {

 public static void main(String[] args) {

  // ApplicationContext is a Spring interface which 
  // provides with the configuration for an application. 
  // It provides us with all the methods that BeanFactory 
  // provides. It loads the file resources in a older 
  // and generic manner. It helps us to publish events to the
  // listener registered to it. It also provides quick support
  // for internationalization. It provides us with the object 
  // requested, it reads the configuration file and provides
  // us with the necessary object required.
  // We are using concrete implementation of AbstractApplicationContext
  // here called as ClassPathXmlApplicationContext because this 
  // bean factory reads the xml file placed in the classpath of 
  // our application. We provide ClassPathXmlApplicationContext
  // with a configuration file called as before_advice.xml placed
  // at classpath of our application. 

  ApplicationContext context = 
    new ClassPathXmlApplicationContext("after_returning_advice.xml");

  // In order to get a object instantiated for a particular bean 
  // we call getBean() method of ClassPathXmlApplicationContext
  // passing it the id for which the object is to be needed. 
  // Here getBean() returns an Object. We need to cast it back 
  // to the Sum object. Without implementing new keyword we 
  // have injected object of Sum just by reading an xml 
  // configuration file.

  Sum sum = (Sum)context.getBean("sum");

  int result = sum.addition(5, 10);

  System.out.println("Result = " + result);



Output of the program : 

© 2021 Learn Java by Examples Template by Hubberspot