In this tutorial you will learn about Spring AOP Around Advice and its mapping in XML file.
In this tutorial you will learn about Spring AOP Around Advice and its mapping in XML file.Around Advice performs the custom behavior before and after method invocation
by surrounding a join point. It is called before and After method execution when
you need advice for both entry and exit then only use this advice. To implement
around advice you need to implement MethodInterceptor of import
org.aopalliance.intercept.MethodInterceptor package.
SimpleInterface.java
package roseindia.net.aop; public interface SimpleInterface { void testMethod(); }
SimpleInterfaceImpl.java
package roseindia.net.aop; public class SimpleInterfaceImpl implements SimpleInterface { public void testMethod() { System.out.println(" This is Test Method();"); } }
AroundAdviceExample.java
package roseindia.net.aop; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class AroundAdviceExample implements MethodInterceptor { public Object invoke(MethodInvocation method) throws Throwable { System.out.println(" Advice Before method"); Object obj = method.proceed(); System.out.println("Advice After Method"); return null; } }
AroundAdviceTest.java
package roseindia.net.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class AroundAdviceTest { public static void main(String[] args) { ApplicationContext applicationContext = new FileSystemXmlApplicationContext( "classpath:./springconfig/spring-config.xml"); SimpleInterface simpleInterface = (SimpleInterface) applicationContext .getBean("proxyBean"); simpleInterface.testMethod(); } }
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
|
Advice Before method This is Test Method(); Advice After Method |