Spring AOP After Returning Advice example using XML configuration


This page will walk through the Spring AOP after returning advice example. After returning advice is invoked only on normal method return, not if an exception is thrown. Such advice can see the return value, but cannot change it.

Interface AfterReturningAdvice contains only one method.

void afterReturning(@Nullable Object returnValue, Method method, Object[] args, @Nullable Object target) throws Throwable;

returnValue: the value returned by the method, if any.

method: method being invoked.

args: arguments to the method.

target: target of the method invocation. May be null.

Suppose, we have a deposit method in Bank class and I want to print some logging message and its return value after executing the deposit method. To do this we need to override the afterReturning method.

I have used the same classes that used in the previous example.

Let’s see the complete example.

Business Class

Create a Bank class that has two parameters String accountNumber, int amount and a deposit method which will add the amount to the accountNumber.

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 deposite method...");

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

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

Exception Class

Create a class InvalidAcNoException that handles the exception when accountNumber does not match and it throws INVALID ACCOUNT NUMBER.

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

Create a service class LoggingService that implements AfterReturningAdvice interface and override it’s afterReturning method.

LoggingService.java
package org.websparrow.service;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class LoggingService implements AfterReturningAdvice {

	@Override
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {

		System.out.println("............I'M EXECUTED AFTER DEPOSITE METHOD................... ");
		System.out.println("Returing amount: " + returnValue);

	}
}

XML Configuration

In the configuration file, instantiate the target class i.e. Bank and advice class i.e. LoggingService and finally add the target + advice to the proxy class i.e. ProxyFactoryBean .

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>

The ProxyFactoryBean class is provided by Spring Framework. It contains two properties target and interceptorNames. The instance of Bank class will be considered as the target object and the instance of LoggingService class as an interceptor. You need to pass the LoggingService object as the list object.

Run it

Create a Client class, load the configuration file and runt 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");
		bank.deposit(500, "XYZ123");

		context.close();
	}
}
Output:

On your console log, you can see the LoggingService executed after executing the deposit method.

inside deposit method...
............I'M EXECUTED AFTER DEPOSIT METHOD................... 
Returing amount: 1500

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.