Skip to content

Instantly share code, notes, and snippets.

@bdeterling
Created January 13, 2009 00:13
Show Gist options
  • Save bdeterling/46252 to your computer and use it in GitHub Desktop.
Save bdeterling/46252 to your computer and use it in GitHub Desktop.
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory;
public class AopTester {
public interface MyInterface {
public void doStuff();
}
public static class ThingToWrap implements MyInterface {
public void doStuff() {
System.out.println("Doing stuff");
}
}
public static class MyInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation method) throws Throwable {
System.out.println("Before");
try {
return method.proceed();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
System.out.println("After");
}
}
}
public static void main(String[] args) {
ProxyFactory proxyFactory = new ProxyFactory(new ThingToWrap());
proxyFactory.addAdvice(new MyInterceptor());
MyInterface proxy = (MyInterface) proxyFactory.getProxy();
proxy.doStuff();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment