Spring AOP After Returning advice


 

Spring AOP After Returning advice

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

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.

SampleInterface.java

public interface SampleInterface {
	public void hello();

	public void date();

}

SampleInterfaceImp.java

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");

	}

}

AfterFinalyAdviceExample.java

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());
	}
}

TestAfterAdvice.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TestAfterAdvice {
	public static void main(String[] args) {
		ApplicationContext ctx = new FileSystemXmlApplicationContext(
				"classpath:./spring/springconfig.xml");

		SampleInterface sampleInterface = (SampleInterface) ctx
				.getBean("proxy");

		System.out.println("*************************");
		sampleInterface.date();
		System.out.println("*************************");
		sampleInterface.hello();
		System.out.println("*************************");
	}
}

springconfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
    "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   <!-- Bean configuration -->
   <bean id="proxy"
   class="org.springframework.aop.framework.ProxyFactoryBean">
      <property name="proxyInterfaces">
         <value>SampleInterface</value>
      </property>
      <property name="target">
         <ref local="beanTarget"/>
      </property>
      <property name="interceptorNames">
         <list>
                <value>theTracingAfterAdvisor</value>
         </list>
         </property>
   </bean>
   <!-- Bean Classes -->
   <bean id="beanTarget"
   class="SampleInterfaceImp"/>

      <!-- Advisor pointcut definition for after advice -->
  
   <bean id="theTracingAfterAdvisor"
      class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
      <property name="advice">
         <ref local="theTracingAfterAdvice"/>
      </property>
      <property name="pattern">
         <value>.*</value>
      </property>
   </bean>
   <!-- Advice classes -->
     <bean id="theTracingAfterAdvice"
      class="AfterFinalyAdviceExample"/>

</beans>

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

Ads