Implements MethodInterceptor to check the invocation method name : MethodInterceptor « Spring « Java






Implements MethodInterceptor to check the invocation method name

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




           
       








Spring-implementsMethodInterceptor.zip( 4,745 k)

Related examples in the same category

1.Implements MethodInterceptor To Create Profiling Advice
2.Create A Benchmark with MethodInterceptor
3.Check InvokedMethod In MethodInterceptor