Method Before Advice : MethodBeforeAdvice « Spring « Java Tutorial






File: Main.java

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;

public class Main {
  public static void main(String[] args) throws Exception {
    MessageWriter target = new MessageWriter();

    ProxyFactory pf = new ProxyFactory();

    pf.addAdvice(new SimpleBeforeAdvice());
    pf.setTarget(target);

    MessageWriter proxy = (MessageWriter) pf.getProxy();

    proxy.writeMessage();
  }
}

class MessageWriter {
  public void writeMessage() {
    System.out.println("A");
  }
}

class SimpleBeforeAdvice implements MethodBeforeAdvice {
  public void before(Method method, Object[] args, Object target) throws Throwable {
    System.out.println("Before method: " + method.getName());
  }

}
  Download:  Spring-MethodBeforeAdvice.zip( 4,744 k)








28.54.MethodBeforeAdvice
28.54.1.Method Before Advice
28.54.2.Implements MethodBeforeAdvice To Check Parameters