Spring AOP AfterThrowing Advice example using XML configuration


Spring AOP after throwing advice executed when a method exits by throwing an exception. Spring framework provide a marker interface ThrowsAdvice. There are not any methods on this interface, as methods are invoked by reflection.

Some valid method examples :

public void afterThrowing(Exception ex);
public void afterThrowing(RemoteException);
public void afterThrowing(Method method, Object[] args, Object target, Exception ex);
public void afterThrowing(Method method, Object[] args, Object target, ServletException ex);

In this example, I have used the same code as used in last two previous tutorials. Only little changes in LoggingService and spring.xml file.

1- Spring AOP Before Advice example using XML configuration

2- Spring AOP After Returning Advice example using XML configuration

Let's see the complete example.

Bussiness Class

Same as previous.

Bank.java
package org.websparrow.business;

import org.websparrow.exception.InvalidAcNoException;

public class Bank {

	private String accountNo = "XYZ123";
	private int amount = 1000;

	public int deposit(int amount, String acNo) {

		if (acNo.equals(this.accountNo)) {

			System.out.println("inside deposit method...");

			this.amount = this.amount + amount;
			return this.amount;

		} else {
			throw new InvalidAcNoException();
		}
	}
}

Exception Class

Same as previous.

InvalidAcNoException.java
package org.websparrow.exception;

public class InvalidAcNoException extends RuntimeException {

	private static final long serialVersionUID = 9087720614302482902L;

	@Override
	public String toString() {

		return "INVALID ACCOUNT NUMBER";
	}
}

Service Class

LoggingService class implements ThrowsAdvice interface.

LoggingService.java
package org.websparrow.service;

import org.springframework.aop.ThrowsAdvice;

public class LoggingService implements ThrowsAdvice {

	public void afterThrowing(Exception ex) {

		System.out.println("............I'M EXECUTED WHEN AN EXCEPTION OCCURS...................");

	}
}

XML Configuration

Same as previous.

spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- create target -->
	<bean id="b" class="org.websparrow.business.Bank" />

	<!-- create advice -->
	<bean id="ls" class="org.websparrow.service.LoggingService" />

	<!-- add target + advice to proxy -->
	<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="b" />
		<property name="interceptorNames">
			<list>
				<value>ls</value>
			</list>
		</property>
	</bean>
</beans>

Test it

To test the application, create a Client class, load the configuration file, call deposit method and pass invalid account number and run it.

Client.java
package org.websparrow.test;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.websparrow.business.Bank;

public class Client {

	public static void main(String[] args) {

		ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

		Bank bank = (Bank) context.getBean("proxy");
		int totalAmount = bank.deposit(500, "ABC321");

		System.out.println("Total Amount: " + totalAmount);

		context.close();
	}
}
Output:

When you enter the invalid account number, After Throwing service executed first.

NFO: Loading XML bean definitions from class path resource [spring.xml]
............I'M EXECUTED WHEN AN EXCEPTION OCCURS...................
Exception in thread "main" INVALID ACCOUNT NUMBER
	at org.websparrow.business.Bank.deposit(Bank.java:20)
	at org.websparrow.business.Bank$$FastClassBySpringCGLIB$$6e18a1e.invoke()
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747)

Share this article on: