1. /**
    2. * 使用AOP联盟提供的API,用来定义拦截器使用
    3. * @param args
    4. * @throws Throwable
    5. */
    6. public static void main(String[] args) throws Throwable {
    7. // 我特意这么写用以显示指定拦截器的使用方法
    8. MethodInterceptor interceptor = new MethodInterceptor() {
    9. @Override
    10. public Object invoke(MethodInvocation methodInvocation) throws Throwable {
    11. System.out.println("before...");
    12. Object proceed = methodInvocation.proceed();
    13. System.out.println("after...");
    14. return proceed;
    15. }
    16. };
    17. Object result = interceptor.invoke(new MethodInvocation() {
    18. @Override
    19. public Method getMethod() {
    20. return null;
    21. }
    22. @Override
    23. public Object[] getArguments() {
    24. return new Object[0];
    25. }
    26. @Override
    27. public Object proceed() throws Throwable {
    28. System.out.println("invoke action");
    29. return "HELLO,AOP";
    30. }
    31. @Override
    32. public Object getThis() {
    33. return null;
    34. }
    35. @Override
    36. public AccessibleObject getStaticPart() {
    37. return null;
    38. }
    39. });
    40. System.out.println(result);
    41. }