Java/Spring/ProxyFactory

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

Invoke Method Through Proxy

       
File: Main.java
import java.lang.reflect.Method;
import org.springframework.aop.Advisor;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.ControlFlowPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
public class Main {
  public static void main(String[] args) {
    TestBean target = new TestBean();
    Pointcut pc = new ControlFlowPointcut(Main.class, "test");
    Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleBeforeAdvice());
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);
    TestBean proxy = (TestBean) pf.getProxy();
    proxy.foo();
    test(proxy);
  }
  static void test(TestBean bean) {
    bean.foo();
  }
}
class TestBean {
  public void foo() {
    System.out.println("foo");
  }
}
class SimpleBeforeAdvice implements MethodBeforeAdvice {
  public void before(Method method, Object[] args, Object target) throws Throwable {
    System.out.println("Before method " + method);
  }
}





ProxyFactory: Add Advisor

       
File: Main.java
import java.lang.reflect.Method;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.Advisor;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
public class Main {
  public static void main(String[] args) {
    BeanOne one = new BeanOne();
    BeanTwo two = new BeanTwo();
    BeanOne proxyOne;
    BeanTwo proxyTwo;
    Pointcut pc = new SimpleStaticPointcut();
    Advice advice = new SimpleAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(pc, advice);
    ProxyFactory pf = new ProxyFactory();
    pf.addAdvisor(advisor);
    pf.setTarget(one);
    proxyOne = (BeanOne) pf.getProxy();
    pf = new ProxyFactory();
    pf.addAdvisor(advisor);
    pf.setTarget(two);
    proxyTwo = (BeanTwo) pf.getProxy();
    proxyOne.foo();
    proxyTwo.foo();
    proxyOne.bar();
    proxyTwo.bar();
  }
}
class BeanTwo {
  public void foo() {
    System.out.println("BeanTwo.foo");
  }
  public void bar() {
    System.out.println("BeanTwo.bar");
  }
}
class BeanOne {
  public void foo() {
    System.out.println("BeanOne.foo");
  }
  public void bar() {
    System.out.println("BeanOne.bar");
  }
}
class SimpleAdvice implements MethodInterceptor {
  public Object invoke(MethodInvocation invocation) throws Throwable {
    System.out.println("SimpleAdvice.Invoking " + invocation.getMethod().getName());
    Object retVal = invocation.proceed();
    System.out.println("SimpleAdvice.Done");
    return retVal;
  }
}
class SimpleStaticPointcut extends StaticMethodMatcherPointcut {
  public boolean matches(Method method, Class cls) {
    return ("foo".equals(method.getName()));
  }
  public ClassFilter getClassFilter() {
    return new ClassFilter() {
      public boolean matches(Class cls) {
        return (cls == BeanOne.class);
      }
    };
  }
}





StaticMethodMatcher and ProxyFactory

File: Main.java
import java.lang.reflect.Method;
import org.springframework.aop.Advisor;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.ruposablePointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcher;
public class Main {
  public static void main(String[] args) {
    SampleBean target = new SampleBean();
    ComposablePointcut pc = new ComposablePointcut(ClassFilter.TRUE, new GetterMethodMatcher());
    SampleBean proxy = getProxy(pc, target);
    testInvoke(proxy);
  }
  private static SampleBean getProxy(ComposablePointcut pc, SampleBean target) {
    Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleBeforeAdvice());
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);
    return (SampleBean) pf.getProxy();
  }
  private static void testInvoke(SampleBean proxy) {
    proxy.getAge();
    proxy.getName();
    proxy.setName("QQQ");
  }
}
class GetterMethodMatcher extends StaticMethodMatcher {
  public boolean matches(Method method, Class cls) {
    return (method.getName().startsWith("get"));
  }
}
class SampleBean {
  public String getName() {
    return "AAA";
  }
  public void setName(String name) {
  }
  public int getAge() {
    return 100;
  }
}
class SimpleBeforeAdvice implements MethodBeforeAdvice {
  public void before(Method method, Object[] args, Object target) throws Throwable {
    System.out.println("Before method " + method);
  }
}