Spring AOP — After Returning Advice | Code Factory

Spring Tutorial Index Page: Link

  • AfterReturningAdvice example using Programmatic approach
  • AfterReturningAdvice: After Advice it is executed after the actual method call. If method returns a value, it is executed after returning value.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Other… -> Java Project
  • Create AOP-Programmatic2 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)
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;
}
@Override
public String toString() {
return "Bank [amount=" + amount + ", accNo=" + accNo + "]";
}
}
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.AfterReturningAdvice;
import com.codeFactory.business.Bank;/**
* @author code.factory
*
*/
public class LogAfterService implements AfterReturningAdvice {
@Override
public void afterReturning(Object obj1, Method method, Object[] objects, Object obj2) throws Throwable {
/*
* obj1 is return value
* obj2 is object
*/
Log l = LogFactory.getLog(Bank.class);
l.info("Deposit method " + obj1);
l.info("Deposit method " + obj2);
}
}
package com.codeFactory.business;/**
* @author code.factory
*
*/
public class AccnoNotFoundException extends RuntimeException {
@Override
public String toString() {
return "AccNo invalid";
}
}
package com.codeFactory.test;import org.springframework.aop.framework.ProxyFactoryBean;import com.codeFactory.business.Bank;
import com.codeFactory.service.LogAfterService;
/**
* @author code.factory
*
*/
public class Client {
public static void main(String... args) {
/* create target */
Bank b = new Bank();
/* create advice */
LogAfterService las = new LogAfterService();
/* add target + advice to proxy */
ProxyFactoryBean bean = new ProxyFactoryBean();
bean.setTarget(b);
bean.addAdvice(las);

/* get generated proxy object */
Bank bankProxy = (Bank) bean.getObject();
int amt = bankProxy.deposit(150, "SBI12345");
System.out.println(amt);
}
}
Dec 23, 2020 11:51:10 AM com.codeFactory.service.LogAfterService afterReturning
INFO: Deposit method 1150
Dec 23, 2020 11:51:10 AM com.codeFactory.service.LogAfterService afterReturning
INFO: Deposit method Bank [amount=1150, accNo=SBI12345]
1150

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Simple Toast in Swift

Best Coding Bootcamps of 2019

Figma on iPad Pro: A Survival Guide

Waypoint system for enemy movement

Meet the Team: Josh Allmann

KYVE Incentivised Testnet MISSION korellia

MatrixOne, a hyperconverged and one-size-fits-most database

ABC in Python (Abstract Base Class)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Responsibility Driven Object Oriented Design

SOLID Design Principle Java

Singleton Design Pattern

Builder Design Pattern