Accessing method arguments using Spring AOP

Posted on Updated on

Earlier we used Spring AOP to weave common logging functionality to our BookShelf add and remove methods. But those were plain log statements, what if we want to log which Book is being added/removed. Spring AOP provides @Around advice which can be used to get this use case working.

First we will enhance our Book.java and provide implementation for toString, so that we can log some valuable information.

Book.java


package com.mumz.test.spring.aop;

/**
 * The Class Book.
 * 
 * @author prabhat.jha
 */
public class Book {

	/** The name. */
	private String name = null;

	/** The author. */
	private String author = null;

	/** The publisher. */
	private String publisher = null;

	/**
	 * The Constructor.
	 */
	public Book() {

	}

	/**
	 * Gets the name.
	 * 
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name.
	 * 
	 * @param name
	 *            the name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Gets the author.
	 * 
	 * @return the author
	 */
	public String getAuthor() {
		return author;
	}

	/**
	 * Sets the author.
	 * 
	 * @param author
	 *            the author
	 */
	public void setAuthor(String author) {
		this.author = author;
	}

	/**
	 * Gets the publisher.
	 * 
	 * @return the publisher
	 */
	public String getPublisher() {
		return publisher;
	}

	/**
	 * Sets the publisher.
	 * 
	 * @param publisher
	 *            the publisher
	 */
	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	/**
	 * (non-Javadoc).
	 * 
	 * @return the int
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((author == null) ? 0 : author.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((publisher == null) ? 0 : publisher.hashCode());
		return result;
	}

	/**
	 * (non-Javadoc).
	 * 
	 * @param obj
	 *            the obj
	 * @return true, if successful
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Book)) {
			return false;
		}
		Book other = (Book) obj;
		if (author == null) {
			if (other.author != null) {
				return false;
			}
		} else if (!author.equals(other.author)) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (publisher == null) {
			if (other.publisher != null) {
				return false;
			}
		} else if (!publisher.equals(other.publisher)) {
			return false;
		}
		return true;
	}

	/** (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Book [name=" + name + ", author=" + author + ", publisher="
				+ publisher + "]";
	}
}

Second we will enhance our BookAdvice.java and introduce @Around advice.

BookAdvice

package com.mumz.test.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * The Class BookAdvice.
 * @author prabhat.jha
 */
@Aspect
public class BookAdvice {
	
	/**
	 * Before add advice.
	 */
	@Before(value="execution(* com.mumz.test.spring.aop.BookShelf.addBook(..))")
	public void beforeAddAdvice(){
		System.out.println("Before adding new book in the list");
	}
	
	/**
	 * After add advice.
	 */
	@After("execution(* com.mumz.test.spring.aop.BookShelf.addBook(..))")
	public void afterAddAdvice(){
		System.out.println("After adding new book in the list");
	}
	
	/**
	 * Before remove advice.
	 */
	@Before("execution(* com.mumz.test.spring.aop.BookShelf.removeBook(..))")
	public void beforeRemoveAdvice(){
		System.out.println("Before adding new book in the list");
	}
	
	/**
	 * After remove advice.
	 */
	@After("execution(* com.mumz.test.spring.aop.BookShelf.removeBook(..))")
	public void afterRemoveAdvice(){
		System.out.println("After adding new book in the list");
	}
	
	/**
	 * Around add advice.
	 * 
	 * @param pjp
	 *            the pjp
	 */
	@Around("execution(* com.mumz.test.spring.aop.BookShelf.addBook(..))")
	public void aroundAddAdvice(ProceedingJoinPoint pjp){
		Object[] arguments = pjp.getArgs();
		for (Object object : arguments) {
			System.out.println("Book being added is : " + object);
		}
		try {
			pjp.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Around remove advice.
	 * 
	 * @param pjp
	 *            the pjp
	 */
	@Around("execution(* com.mumz.test.spring.aop.BookShelf.removeBook(..))")
	public void aroundRemoveAdvice(ProceedingJoinPoint pjp){
		Object[] arguments = pjp.getArgs();
		for (Object object : arguments) {
			System.out.println("Book being removed is : " + object);
		}
		try {
			pjp.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
}

All other code remains the same from previous post.

One thought on “Accessing method arguments using Spring AOP

    LNTaw1BkVHsoN said:
    October 10, 2012 at 4:35 PM

    Hi there, just became aware of your blog through Google, and found that it’s truly informative. I’ll be grateful if you continue this in future. Lots of people will benefit from your writing. Cheers!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s