Share on Google+Share on Google+

Spring AOP After Returning advice

In this tutorial you will learn how to invoke a method before Advice using AfterReturningAdvice interface of Spring AOP

Spring AOP After Returning Advice Example

In this tutorial you will learn how to invoke method after advice This advice is executed when program exits the joinpoints.

public interface SampleInterface {
	public void hello();

	public void date();


public class SampleInterfaceImp implements SampleInterface {
	public void date() {
		System.out.println("this is date() method " + "\nToday is "
				+ new java.util.Date());


	public void hello() {
		System.out.println("This is hello method of SampleInterfaceImp");



import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;

public class AfterFinalyAdviceExample implements AfterReturningAdvice {
	public void afterReturning(Object object, Method method, Object[] args,
			Object target) throws Throwable {

		System.out.println("Hello world! by " + this.getClass().getName());

import org.springframework.context.ApplicationContext;

public class TestAfterAdvice {
	public static void main(String[] args) {
		ApplicationContext ctx = new FileSystemXmlApplicationContext(

		SampleInterface sampleInterface = (SampleInterface) ctx



<?xml version="1.0" encoding="UTF-8"?>
   <!-- Bean configuration -->
   <bean id="proxy"
      <property name="proxyInterfaces">
      <property name="target">
         <ref local="beanTarget"/>
      <property name="interceptorNames">
   <!-- Bean Classes -->
   <bean id="beanTarget"

      <!-- Advisor pointcut definition for after advice -->
   <bean id="theTracingAfterAdvisor"
      <property name="advice">
         <ref local="theTracingAfterAdvice"/>
      <property name="pattern">
   <!-- Advice classes -->
     <bean id="theTracingAfterAdvice"


When you run this application it will display message as shown below:

this is date() method
Today is Thu Sep 02 17:38:28 GMT+05:30 2010
Hello world! by AfterFinalyAdviceExample
This is hello method of SampleInterfaceImp
Hello world! by AfterFinalyAdviceExample

Download this example code



Posted on: September 2, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null