Add Advice To ProxyFactory : ProxyFactory « Spring « Java Tutorial






File: Main.java

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory;

public class Main {
  public static void main(String[] args) throws Exception {
    MessageWriter target = new MessageWriter();

    ProxyFactory pf = new ProxyFactory();

    pf.addAdvice(new MessageDecorator());
    pf.setTarget(target);

    MessageWriter proxy = (MessageWriter) pf.getProxy();

    target.writeMessage();
    System.out.println("");
    proxy.writeMessage();
  }
}

class MessageWriter {
  public void writeMessage() {
      System.out.print("A");
  }
}
class MessageDecorator implements MethodInterceptor {
  public Object invoke(MethodInvocation invocation) throws Throwable {
      System.out.print("B ");
      Object retVal = invocation.proceed();
      System.out.println("C");
      return retVal;
  }
}
  Download:  Spring-AddAdviceToProxyFactory.zip( 4,744 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