In this tutorial you will learn about Spring After Throws Advice which executed when a method throws an exception
In this tutorial you will learn about Spring After Throws Advice which executed when a method throws an exceptionThis advice is executed when a method throws an exception. To implement
throws advice you need to implement ThrowsAdvice Interface of import
org.springframework.aop.ThrowsAdvice package.
SimpleInterface.java
package roseindia.net.aop; public interface SimpleInterface { void simpleMethod(); }
SimpleInterfaceImpl.java
package roseindia.net.aop; public class SimpleInterfaceImpl implements SimpleInterface { public void simpleMethod() { System.out.println(" ******* This is SimpleMethod() of Class " + this.getClass().getName() + " ******* "); throw new RuntimeException(); } }
AfterThrowingAdviceExample.java
package roseindia.net.aop; import java.lang.reflect.Method; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.aop.ThrowsAdvice; public class AfterThrowingAdviceExample implements ThrowsAdvice { public void afterThrowing(RuntimeException runtimeException) { System.out.println(" ******* Inside Throws Advice ******* "); } }
AfterThrowingAdviceTest.java
package roseindia.net.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class AfterThrowingAdviceTest { public static void main(String[] args) { ApplicationContext applicationContext = new FileSystemXmlApplicationContext( "classpath:./springconfig/spring-config.xml"); SimpleInterface simpleInterface = (SimpleInterface) applicationContext .getBean("proxyBean"); try { simpleInterface.simpleMethod(); } catch (Exception e) { } } }
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
|
******* This is SimpleMethod() of Class
roseindia.net.aop.SimpleInterfaceImpl ******* ******* Inside Throws Advice ******* |