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 AOPIn 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"?>
|
************************* 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 ************************* |