Invoke Method Through Proxy : ProxyFactory « Spring « 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);
  }
}




           
       








Spring-InvokeMethodThroughProxy.zip( 4,745 k)

Related examples in the same category

1.ProxyFactory: Add Advisor
2.StaticMethodMatcher and ProxyFactory