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 the last three previous tutorials. Only little changes in LoggingService file.

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

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 MethodInterceptor interface.

LoggingService.java
package org.websparrow.service;

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

public class LoggingService implements MethodInterceptor {

	@Override
	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.

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 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");
		bank.deposit(500, "XYZ123");

		context.close();
	}
}
Output:

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

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.