Java Tutorial/Spring/MethodInterceptor

Материал из Java эксперт
Перейти к: навигация, поиск

Check Invoked Method In MethodInterceptor

File: Main.java



import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.Advisor;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.DynamicMethodMatcherPointcut;
public class Main {
  public static void main(String[] args) {
    MyClass target = new MyClass();
    Advisor advisor = new DefaultPointcutAdvisor(new SimpleDynamicPointcut(), new SimpleAdvice());
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);
    MyClass proxy = (MyClass) pf.getProxy();
    proxy.foo(1);
    proxy.bar();
  }
}
class MyClass {
  public void foo(int x) {
    System.out.println("MyClass.foo() : " + x);
  }
  public void bar() {
    System.out.println("MyClass.bar()");
  }
}
class SimpleAdvice implements MethodInterceptor {
  public Object invoke(MethodInvocation invocation) throws Throwable {
    System.out.println("SimpleAdivce.Invoking " + invocation.getMethod().getName());
    Object retVal = invocation.proceed();
    System.out.println("SimpleAdvice.Done");
    return retVal;
  }
}
class SimpleDynamicPointcut extends DynamicMethodMatcherPointcut {
  public boolean matches(Method method, Class cls) {
    return ("foo".equals(method.getName()));
  }
  public boolean matches(Method method, Class cls, Object[] args) {
    int x = (Integer) args[0];
    return (x != 100);
  }
  public ClassFilter getClassFilter() {
    return new ClassFilter() {
      public boolean matches(Class cls) {
        return (cls == MyClass.class);
      }
    };
  }
}





Create A Benchmark with MethodInterceptor

File: Main.java



import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.util.StopWatch;
public class Main {
  public static void main(String[] args) {
    Main target = new Main();
    ProxyFactory factory = new ProxyFactory();
    factory.setTarget(target);
    factory.addAdvice(new ProfilingInterceptor());
    Main bean = (Main) factory.getProxy();
    bean.doSomeWork(10);
  }
  public void doSomeWork(int noOfTimes) {
    for (int x = 0; x < noOfTimes; x++) {
    }
  }
}
class ProfilingInterceptor implements MethodInterceptor {
  public Object invoke(MethodInvocation invocation) throws Throwable {
    StopWatch sw = new StopWatch();
    sw.start(invocation.getMethod().getName());
    Object returnValue = invocation.proceed();
    sw.stop();
    System.out.println("Executed method: " + invocation.getMethod().getName());
    System.out.println("On object of type: " + invocation.getThis().getClass().getName());
    System.out.println("With arguments:");
    for (int x = 0; x < invocation.getArguments().length; x++) {
      System.out.print(invocation.getArguments()[x]);
    }
    return returnValue;
  }
  
    
}





Implements MethodInterceptor

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





Implements MethodInterceptor To Create Profiling Advice

File: context.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 id="afterBean" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target">
      <bean class="MtBean">
        <property name="firstName" value="AAA"/>
      </bean>
    </property>
    <property name="interceptorNames">
      <list>
        <idref bean="profilingAdvice"/>
      </list>
    </property>
    <property name="proxyTargetClass" value="true"/>
  </bean>
  <bean id="profilingAdvice" class="SimpleProfilingAroundAdvice"/>
</beans>