Spring AOP around advice (MethodInterceptor) example using XML configuration

On this page, you will learn about Spring AOP around advice (MethodInterceptor) example using XML configuration. Around advice surrounds a join point such as a method invocation. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.

Spring framework provides an interface MethodInterceptor that has one method:

Object invoke(MethodInvocation invocation) throws Throwable;

Interface MethodInvocation is a joinpoint and can be intercepted by a method interceptor. And it will help in introspection like to get the method, class name, etc.

System.out.println("method " + invocation.getMethod() + " is called on " + invocation.getThis() + " with args " + invocation.getArguments());

Object ret = invocation.proceed();

System.out.println("method " + invocation.getMethod() + " returns " + ret);

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

Let's see the complete example.

Bussiness Class

Same as previous.

package org.websparrow.business;

import org.websparrow.exception.InvalidAcNoException;

public class Bank {

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

	public void deposit(int amount, String acNo) {

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

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

			this.amount = this.amount + amount;

			System.out.println("Total Balance: " + this.amount);

		} else {
			throw new InvalidAcNoException();

Exception Class

Same as previous.

package org.websparrow.exception;

public class InvalidAcNoException extends RuntimeException {

	private static final long serialVersionUID = 9087720614302482902L;

	public String toString() {


Service Class

LoggingService class implements MethodInterceptor interface.

package org.websparrow.service;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class LoggingService implements MethodInterceptor {

	public Object invoke(MethodInvocation invocation) throws Throwable {

		System.out.println("............I'M EXECUTED BEFORE DEPOSIT METHOD...................");

		Object ret = invocation.proceed();

		System.out.println("............I'M EXECUTED AFTER DEPOSIT METHOD...................");

		return ret;

XML Configuration

Same as previous.

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

	<!-- 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">

Test it

To test the application, create a Client class, load the configuration file and run it.

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


You will see the logging messages executed before and after the deposit method.

............I'M EXECUTED BEFORE DEPOSIT METHOD...................
inside deposit method...
Total Balance: 1500
............I'M EXECUTED AFTER DEPOSIT METHOD...................

Share this article on: