In this tutorial you will learn how use method before advice of spring aop and how to configure it in config.xml file.
In this tutorial you will learn how use method before advice of spring aop and how to configure it in config.xml file.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"?>
|
Hello world! (by BeforeAdviceExample) Hello friends:- This is Hello method of SimpleInterfaceImpl Hello world! (by BeforeAdviceExample) This is show method of SimpleInterfaceImpl class |