Spring AOP — Befor, After, Around Advice | Code Factory

Spring Tutorial Index Page: Link

  • MethodBeforeAdvice, AfterReturningAdvice, MethodInterceptor example using XML

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Other… -> Java Project
  • Create AOP-Programmatic5 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)
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- target -->
<bean id="b" class="com.codeFactory.business.Bank" />
<!-- service -->
<bean id="lbs" class="com.codeFactory.service.LogBeforeService" />
<bean id="las" class="com.codeFactory.service.LogAfterService" />
<bean id="lars" class="com.codeFactory.service.LogAroundService" />
<!-- proxy -->
<bean id="pfb" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="b" />
<property name="interceptorNames">
<list>
<value>lbs</value>
<value>las</value>
<value>lars</value>
</list>
</property>
</bean>
</beans>
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");
}
}
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.service;import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.codeFactory.business.Bank;/**
* @author code.factory
*
*/
public class LogAroundService implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
Log l = LogFactory.getLog(Bank.class);
l.info("before deposite code");
Object obj = mi.proceed();
l.info("after deposite code");
return obj;
}
}
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) {
System.out.println("Bank.deposit()");
if(accNo.equals(this.accNo)) {
this.amount += amount;
} else {
throw new AccnoNotFoundException();
}
return this.amount;
}
}
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.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.codeFactory.business.Bank;/**
* @author code.factory
*
*/
public class Client {
public static void main(String... args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
Bank bankProxy = (Bank) context.getBean("pfb");
int amt = bankProxy.deposit(150, "SBI12345");
System.out.println(amt);
}
}
Dec 23, 2020 1:42:37 PM com.codeFactory.service.LogBeforeService before
INFO: Deposit method
Dec 23, 2020 1:42:37 PM com.codeFactory.service.LogAroundService invoke
INFO: before deposite code
Bank.deposit()
Dec 23, 2020 1:42:37 PM com.codeFactory.service.LogAroundService invoke
INFO: after deposite code
Dec 23, 2020 1:42:37 PM com.codeFactory.service.LogAfterService afterReturning
INFO: Deposit method 1150
Dec 23, 2020 1:42:37 PM com.codeFactory.service.LogAfterService afterReturning
INFO: Deposit method com.codeFactory.business.Bank@4667ae56
1150

--

--

--

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

Recommended from Medium

How-to guide: Install Animverse Testnet on PC using LDPlayer.

CDI in Java — Part 1 — Creating a Factory with @Produces Annotation

The agility of Agile

Python Morsel — count_calls

GoFrame 101: Add logging middleware

Advanced Docker Compose template for Golang

Building a Decoupled Architecture to Optimize our Mobile Apps

Community update for April 2019

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

Root of Number (Java solution)

Clean Code — Comments

Command Pattern for encapsulated invocation

What to look for in a Code Review