Aspect Oriented Programming with AspectJ and Spring AOP – A simple example to create logging aspect


Here’s the source code for this example: Download

Calculator.java

package singz.spring.aop.aopdemo;

public interface Calculator {
public int sum (int a, int b);
public int difference (int a, int b);
}

CalculatorImpl.java

package singz.spring.aop.aopdemo;

public class CalculatorImpl implements Calculator {

public int sum(int a, int b) {
System.out.println("sum() invoked - params: " + a + "," + b);
return (a+b);
}

public int difference(int a, int b) {
System.out.println("difference() invoked - params: " + a + "," + b);
return Math.abs(a-b);
}

}

LoggingAspect.java

package singz.spring.aop.aopdemo;

import org.aspectj.lang.ProceedingJoinPoint;

public class LoggingAspect {

public Object log(ProceedingJoinPoint call) throws Throwable {
System.out.println("Intercepting call to '" + call.getSignature().getName() + "' method in " + call.getTarget().getClass());
System.out.println("Proceeding with the '" + call.getSignature().getName() + "' method invocation");
Object point = call.proceed();
System.out.println("Done with intercepting call to '" + call.getSignature().getName() + "' method in " + call.getTarget().getClass());
return point;
}

}

AOPMain.java

package singz.spring.aop.aopdemo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPMain {

public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext("spring-appcontext.xml");
Calculator calculator = (Calculator) appContext.getBean("calculator");
System.out.println("5 + 4 = " + calculator.sum(5, 4));
System.out.println("5 ~ 4 = " + calculator.difference(5, 4));
}

}

spring-appcontext.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" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<bean id="calculator"
class="singz.spring.aop.aopdemo.CalculatorImpl" />

<bean id="loggingAspect"
class="singz.spring.aop.aopdemo.LoggingAspect" />

<aop:config>
<aop:aspect ref="loggingAspect">

<aop:pointcut id="loggingPointCut"
expression="execution(* singz.spring.aop.aopdemo.Calculator.*(..))" />

<aop:around pointcut-ref="loggingPointCut" method="log" />

</aop:aspect>
</aop:config>
</beans>
About these ads

2 comments

  1. [...] is a fantastic way to add cross-cutting functionality to your application. This post introduces a very simple example using Spring’s AOP support. Check it [...]

  2. [...] Aspect Oriented Programming with AspectJ and Spring AOP – A simple example to create logging a… (singztechmusings.wordpress.com) [...]

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

Follow

Get every new post delivered to your Inbox.

Join 26 other followers

%d bloggers like this: