Spring AOP – Before Advice | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

This image has an empty alt attribute; its file name is image-1.png

Spring Tutorial Index Page: Link

  • MethodBeforeAdvice example using Programmatic approach
  • MethodBeforeAdvice: Before Advice it is executed before the actual method call.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Other… -> Java Project
  • Create AOP-Programmatic1 project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs
    • aopalliance.jar
    • aspectjrt.jar
    • aspectjweaver.jar
    • cglib-nodep-X.X.jar
    • commons-logging-X.X.jar
    • spring-aop-X.X.X.jar
    • spring-beans-X.X.X.jar
    • spring-context-X.X.X.jar
    • spring-core-X.X.X.jar
    • aspectjlib-X.X.X.jar
    • spring-aspects-X.X.X.jar
  • * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)

Bank.java

package com.codeFactory.business;

/**
 * @author code.factory
 *
 */
public class Bank {

	private int amount = 1000;
	private String accNo = "SBI12345";
	
	public int deposit(int amount, String accNo) {
		if(accNo.equals(this.accNo)) {
			this.amount += amount;
		} else {
			throw new AccnoNotFoundException();
		}
		return this.amount;
	}	
}

LogBeforeService.java

package com.codeFactory.service;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.MethodBeforeAdvice;

import com.codeFactory.business.Bank;

/**
 * @author code.factory
 *
 */
public class LogBeforeService implements MethodBeforeAdvice {

	@Override
	public void before(Method method, Object[] objects, Object object) throws Throwable {
		Log l = LogFactory.getLog(Bank.class);
		l.info("Deposit method");
	}
}

AccnoNotFoundException.java

package com.codeFactory.business;

/**
 * @author code.factory
 *
 */
public class AccnoNotFoundException extends RuntimeException {

	@Override
	public String toString() {
		return "AccNo invalid";
	}
}

Client.java

package com.codeFactory.test;

import org.springframework.aop.framework.ProxyFactoryBean;

import com.codeFactory.business.Bank;
import com.codeFactory.service.LogBeforeService;

/**
 * @author code.factory
 *
 */
public class Client {
	public static void main(String... args) {
		/* create target */
		Bank b = new Bank();
		/* create advice */
		LogBeforeService lbs = new LogBeforeService();
		/* add target + advice to proxy */
		ProxyFactoryBean bean = new ProxyFactoryBean();
		bean.setTarget(b);
		bean.addAdvice(lbs);
		
		/* get generated proxy object */
		Bank bankProxy = (Bank) bean.getObject();
		int amt = bankProxy.deposit(150, "SBI12345");
		System.out.println(amt);		
	}
}

Output:

INFO: Deposit method
1150

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 )

Connecting to %s