Home Tutorial Spring Spring3 Aop Spring AOP After Throws Advice


Share on Google+Share on Google+
Spring AOP After Throws Advice
Posted on: September 3, 2010 at 12:00 AM
In this tutorial you will learn about Spring After Throws Advice which executed when a method throws an exception

Throws Advice Example

This 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.


package roseindia.net.aop;

public interface SimpleInterface {
	void simpleMethod();


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


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



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(

		SimpleInterface simpleInterface = (SimpleInterface) applicationContext
		try {
		} catch (Exception e) {




<?xml version="1.0" encoding="UTF-8"?>

    <!-- Bean configuration -->
    <bean id="proxyBean"
        <property name="proxyInterfaces">
        <property name="target">
            <ref local="beanTarget" />
        <property name="interceptorNames">

    <!-- Bean Classes -->
    <bean id="beanTarget" class="roseindia.net.aop.SimpleInterfaceImpl" />

    <!-- Advisor pointcut definition for before advice -->
    <bean id="theTracingThrowsAdvisor"
        <property name="advice">
            <ref local="theTracingThrowsAdvice" />
        <property name="pattern">

    <!-- Advice classes -->
    <bean id="theTracingThrowsAdvice"
        class="roseindia.net.aop.AfterThrowingAdviceExample" />


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

******* This is SimpleMethod() of Class roseindia.net.aop.SimpleInterfaceImpl *******
******* Inside Throws Advice *******

Download this example code


Related Tags for Spring AOP After Throws Advice:

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.

Posted on: September 3, 2010

Recommend the tutorial

Advertisements Advertisements