Home Tutorial Spring Spring3 Aop Spring AOP MethodBeforeAdvice

 
 

Share on Google+Share on Google+
Spring AOP MethodBeforeAdvice
Posted on: September 1, 2010 at 12:00 AM
Advertisement
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.

SimpleInterface.java

public interface SimpleInterface {
	public void hello();

	public void show();
}

SimpleInterfaceImpl.java

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");
	}

}

BeforeAdviceExample.java

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()
				+ ")");
	}
}

MainApplication.java

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

public class MainApplication {
	public static void main(String[] args) {
		ApplicationContext appContext = new FileSystemXmlApplicationContext(
				"classpath:./spring-config/springconfig.xml");
		SimpleInterface simpleInterface = (SimpleInterface) appContext
				.getBean("proxyBean");
		simpleInterface.hello();
		simpleInterface.show();
	}
}

springconfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
    "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

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

  <!-- Bean Classes -->

  <bean id="beanTarget" class="SimpleInterfaceImpl" />
  <!-- Advisor pointcut definition for before advice -->
  <bean id="theTracingBeforeAdvisor"
    class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
      <ref local="theTracingBeforeAdvice" />
    </property>
    <property name="pattern">
      <value>.*</value>
    </property>
  </bean>

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

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

</beans>

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

Advertisement

Related Tags for Spring AOP MethodBeforeAdvice:


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.

Posted on: September 1, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com