Implements MethodInterceptor : MethodInterceptor « Spring « Java Tutorial






File: Main.java

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;

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

    NameMatchMethodPointcut pc = new NameMatchMethodPointcut();
    pc.addMethodName("foo");
    pc.addMethodName("bar");
    Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleAdvice());

    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);
    MyClass proxy = (MyClass)pf.getProxy();

    proxy.foo();
    proxy.foo(1);
    proxy.bar();
    proxy.yup();
  }
}
class SimpleAdvice implements MethodInterceptor {

  public Object invoke(MethodInvocation invocation) throws Throwable {
      System.out.println("start: " + invocation.getMethod().getName());
      Object retVal = invocation.proceed();
      System.out.println("end");
      return retVal;
  }
}

class MyClass {

  public void foo() {
      System.out.println("foo");
  }

  public void foo(int x) {
      System.out.println("foo " + x);
  }

  public void bar() {
      System.out.println("bar");
  }

  public void yup() {
      System.out.println("yup");
  }
}
  Download:  Spring-implementsMethodInterceptor.zip( 4,745 k)








28.55.MethodInterceptor
28.55.1.Create A Benchmark with MethodInterceptor
28.55.2.Check Invoked Method In MethodInterceptor
28.55.3.Implements MethodInterceptor To Create Profiling Advice
28.55.4.Implements MethodInterceptor