Mega Code Archive

 
Categories / Java Tutorial / Spring
 

Add Advice To ProxyFactory

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