Posts Tagged ‘point cut’

Spring’s Before and After Advice

November 30, 2010 1 comment


This article makes you bit more knowledgeable in one of the spring’s Aspect Oriented Programming concept advice. In this article we see the simple example for the advice aspect of Spring AOP framework. Before that we have a brief look at some AOP concepts that enrich your understanding.


It’s just like a class in OOPs concepts.

Joint point:

A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.

In real world example, it’s just like a menu items in the menu card.


Combination of joinpoints which are chosen for execution.

In real world example, it’s just like a meal which is combination of items chosen from menu card.


The class which is going to be advised.


Actions taken by an aspect at a particular joinpoint.The advices are classified into three major groups.

  • Before advice
  • After advice
  • Around advice
Before advice:

Before advice is executed before the execution of methods in the target.

After advice:

After advice is executed after the execution of methods in target. Normally before advice and after advices are used to logging feature. It’s classified into three subgroups.


  • After returning advice
  • After throwing advice
  • After (finally) advice
Around Advice:

Around advice is executed before and after execution. Its the powerful form of advice. We have a explanation in our next meet.

It is also responsible for choosing whether to proceed to the join point or to block the advised method execution by returning its own return value or throwing an exception. All other advices cannot block the execution flow.

Lets see the example for before and after returning advices.


JDK 1.5 and Above

Your favourite IDE

spring latest  jars



An ordinary interface.

public interface SpringAopInterface
public void sayHello(String name);

An ordinary implementation class of an interface SpringAOPInterface.

public class SpringAopImpl implements SpringAopInterface{
 public void sayHello(String name)


System.out.println("Hello ,"+name);


A class which represent the before advise.By implementing the MethodBeforeAdvice interface make the POJO class as a before advisor class.

The action which is wants to perform in the before advise class is define in the override method before().

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdvisor implements MethodBeforeAdvice

 public void before(Method method, Object[] os, Object o) throws Throwable


System.out.println("Before Advisor called");


A class which represent the after advise.Like a before advisor  class by implementing the AfterReturningAdvice interface make the POJO class as a after advisor class.

The action which is wants to perform in the after advise class is define in the override afterReturning().

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
public class AfterAdvisor implements AfterReturningAdvice

 public void afterReturning(Object o, Method method, Object[] os, Object o1) throws Throwable


System.out.println("After advisor called");


IOC Container Configuration:

This is the context configuration file of the application.Its have to be stored in META-INF folder in src package.

Target: represents the bean class which is going to be advised by the advisors.

Interceptor Names: Represent the advisor classes list.

advice: Represent the advisor class and the pattern of the bean classes which are going to be advised.


<beans xmlns=""

xmlns:xsi="" xmlns:p=""
xmlns:aop="" xmlns:tx=""

<!-- Bean configuration -->
<bean id="businesslogicbean">
<property name="proxyInterfaces">

<!--Target : Reprsent the bean class which is going to be advised-->
<property name="target" ref "beanTarget" />
<!--Denotes the list of advices-->
<property name="interceptorNames">

<!-- Bean Classes -->
<bean id="beanTarget" class=""/>

<!-- Advisor pointcut definition for before advice -->
<bean id="beforeAdvisor" class="">
 <!--Property which represent the advice-->
 <property name="advice" ref="beforeAdvice"/>

 <!--Pattern fot the class which is going to be advised.
 Here any kind of classes are going to be advised.-->
 <property name="pattern">
<!-- Advisor pointcut definition for before advice -->
<bean id="afterAdvisor" class="">
 <property name="advice" ref="afterAdvice"/>
 <property name="pattern">

  <!-- Advice classes -->
<bean id="beforeAdvice"
<bean id="afterAdvice"

By passing the path of the applicationcontext file to theclass ClassPathXmlApplicationContext,  we can get the instance of Application context.

By the method of getBean() of ApplicationContext,we can retrieve the instance of the bean’s interface.We have to pass the id of the bean as the argument to the method getBean().By using the instance of the bean’s interface we can call the method of the interface which is defined in the bean class.

import org.springframework.context.ApplicationContext;

public class Tester

 public static void main(String[] args)


// Read the configuration file

ApplicationContext ctx = new ClassPathXmlApplicationContext("META-INF/applicationContext.xml");

 // Instantiate an object

SpringAopInterface springAopInterface =  (SpringAopInterface) ctx.getBean("businesslogicbean");
 // Execute the public method of the bean



Thats all folks,I think its give some idea about the spring AOP’s advice aspect.If you feel this article is helpful to you,Don’t forget to leave your footprints(comments) here.