Share on Google+Share on Google+

Spring AOP MethodBeforeAdvice

In this tutorial you will learn how use method before advice of spring aop and how to configure it in config.xml file.

Spring AOP MethodBeforeAdvice

The Method Before advice invoked before a joinpoint . It is used as am interceptor method. It does not have the ability to interrupt the execution flow proceeding at the joint point unless it throws an exception.

public interface SimpleInterface {
	public void hello();

	public void show();

public class SimpleInterfaceImpl implements SimpleInterface {
	public void hello() {
	System.out.println("This is Hello method of SimpleInterfaceImpl");

	public void show() {
		System.out.println("This is show method of SimpleInterfaceImpl class");


import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdviceExample implements MethodBeforeAdvice {
	public void before(Method method, Object[] args, Object target)
			throws Throwable {
		System.out.println("Hello world! (by " + this.getClass().getName()
				+ ")");

import org.springframework.context.ApplicationContext;

public class MainApplication {
	public static void main(String[] args) {
		ApplicationContext appContext = new FileSystemXmlApplicationContext(
		SimpleInterface simpleInterface = (SimpleInterface) appContext


<?xml version="1.0" encoding="UTF-8"?>

  <!-- Bean configuration -->
  <bean id="proxyBean" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
    <property name="target">
      <ref local="beanTarget" />
    <property name="interceptorNames">

  <!-- Bean Classes -->

  <bean id="beanTarget" class="SimpleInterfaceImpl" />
  <!-- Advisor pointcut definition for before advice -->
  <bean id="theTracingBeforeAdvisor"
    <property name="advice">
      <ref local="theTracingBeforeAdvice" />
    <property name="pattern">

  <!-- Advisor pointcut definition for after advice -->

  <!-- Advice classes -->
  <bean id="theTracingBeforeAdvice" class="BeforeAdviceExample" />


When you run this application it will display message as shown below:

Hello world! (by BeforeAdviceExample)
Hello friends:- This is Hello method of SimpleInterfaceImpl
Hello world! (by BeforeAdviceExample)
This is show method of SimpleInterfaceImpl class

Download this example code



Posted on: September 1, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null