Invoke Method Through Proxy : ProxyFactory « Spring « Java Tutorial






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);
  }
}
  Download:  Spring-InvokeMethodThroughProxy.zip( 4,745 k)








28.57.ProxyFactory
28.57.1.Proxy Factory Add Advisor
28.57.2.Invoke Method Through Proxy
28.57.3.Add Advice To ProxyFactory