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

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"?>
<!DOCTYPE beans PUBLIC
    "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

    <!-- Bean configuration -->
   
    <bean id="proxyBean"
        class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces">
            <value>roseindia.net.aop.SimpleInterface</value>
        </property>
        <property name="target">
            <ref local="beanTarget" />
        </property>
        <property name="interceptorNames">
            <list>
                <value>theTracingThrowsAdvisor</value>
            </list>
        </property>
    </bean>

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

    <!-- Advisor pointcut definition for before advice -->
    <bean id="theTracingThrowsAdvisor"
        class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice">
            <ref local="theTracingThrowsAdvice" />
        </property>
        <property name="pattern">
            <value>.*</value>
        </property>
    </bean>

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

</beans>

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

Advertisement

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
 

 

 

DMCA.com