After Returning Advise
The same bean program of AOP Advices – Types – Tutorial is used. Change comes in configuration XML file and also slightly in client program. Let us name the modified XML file as applicationContext2.xml.
The method afterReturning() executes after each bean method is returned with a result. Add a new class that implements AfterReturningAdvice interface.
1. The bean program used in AOP Advices – Types – Tutorial is used and reproduced here.
Now, the example using After Returning
Bean Program – File Name: Student.java
public class Student
{
String name; // just two variables
String college;
public void setName(String name) // just two set methods
{
this.name = name;
}
public void setCollege(String college)
{
this.college = college;
}
public void printName() // 3 print methods to print variables
{
System.out.println("Student Name is " + name);
}
public void printCollege()
{
System.out.println("Student College is " + college);
}
public void printThrowException()
{
throw new IllegalArgumentException();
}
}
Add a new class – PleaseCallAfterMethod.java
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class PleaseCallAfterMethod implements AfterReturningAdvice
{
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable
{
System.out.println("AfterReturning() method of PleaseCallAfterMethod is called");
}
}
afterReturning() is a predefined abstract callback method of interface org.springframework.aop.AfterReturningAdvice.
Configuration file: applicationContext2.xml
pcam
Client Program: Client3.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client3
{
public static void main(String[] args)
{
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext2.xml");
Student std1 = (Student) ac.getBean("pcamproxy");
std1.printName();
std1.printCollege();
try
{
std1.printThrowException();
}
catch(Exception e) { }
}
}

Output screenshot on After Returning Example
Observe, after executing each method of bean Student, the method afterReturning() is called.
When to use after advice (afterReturning() method):
After advice is useful when some code is to be executed after the successful execution of the business logic (successful means the business logic method should not throw any exception during its execution). For example, in our code, the best appropriate place to use after advice is cleanUp() method. Here, after advice can execute some clean up code like closing the sessions, JDBC connections, network connections etc.
Pass your comments of how far it is easy to you to understand the concept "After Returning".
Sir,
How would you specifically make the advice to execute for methods. Say,If I want to execute the before advice only for printName() method and not for other methods int he student class.
Thanks
Rajeswari K
Sir,
How would you specifically make the advice to execute for methods.
Say,If I want to execute the before advice only for printName() method and not for other methods int he student class.
Thanks
Rajeswari K