|  | @@ -2,15 +2,49 @@ package com.lovecoding.aop;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  import org.springframework.context.support.ClassPathXmlApplicationContext;
 |  |  import org.springframework.context.support.ClassPathXmlApplicationContext;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +import java.lang.reflect.InvocationHandler;
 | 
											
												
													
														|  | 
 |  | +import java.lang.reflect.Method;
 | 
											
												
													
														|  | 
 |  | +import java.lang.reflect.Proxy;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |  /**
 |  |  /**
 | 
											
												
													
														|  |   * 使用 XML 配置 AOP 切片
 |  |   * 使用 XML 配置 AOP 切片
 | 
											
												
													
														|  |   *  效果和配置类无异样
 |  |   *  效果和配置类无异样
 | 
											
												
													
														|  |   */
 |  |   */
 | 
											
												
													
														|  |  public class XmlAop {
 |  |  public class XmlAop {
 | 
											
												
													
														|  |      public static void main(String[] args) {
 |  |      public static void main(String[] args) {
 | 
											
												
													
														|  | -        ClassPathXmlApplicationContext con =
 |  | 
 | 
											
												
													
														|  | -                new ClassPathXmlApplicationContext("anno.xml");
 |  | 
 | 
											
												
													
														|  | -        Counter countterImpl = con.getBean("countterImpl", Counter.class);
 |  | 
 | 
											
												
													
														|  | -        countterImpl.add(6,6);
 |  | 
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        /**
 | 
											
												
													
														|  | 
 |  | +         * Java JDK 的动态代理实际上就是一个
 | 
											
												
													
														|  | 
 |  | +         * 泛型的 反射封装
 | 
											
												
													
														|  | 
 |  | +         */
 | 
											
												
													
														|  | 
 |  | +        ClassLoader classLoader = XmlAop.class.getClassLoader();
 | 
											
												
													
														|  | 
 |  | +        Class[] cl = { Counter.class };
 | 
											
												
													
														|  | 
 |  | +        class MyInvocationHandler<T> implements InvocationHandler{
 | 
											
												
													
														|  | 
 |  | +            T t;
 | 
											
												
													
														|  | 
 |  | +            public MyInvocationHandler(T t) {
 | 
											
												
													
														|  | 
 |  | +                this.t = t;
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +            @Override
 | 
											
												
													
														|  | 
 |  | +            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 | 
											
												
													
														|  | 
 |  | +                System.out.println( "前置方法" );
 | 
											
												
													
														|  | 
 |  | +                Object invoke = method.invoke( t , args);
 | 
											
												
													
														|  | 
 |  | +                System.out.println( "后置方法" );
 | 
											
												
													
														|  | 
 |  | +                return invoke;
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +        Counter o =  (Counter) Proxy.newProxyInstance(
 | 
											
												
													
														|  | 
 |  | +                classLoader,
 | 
											
												
													
														|  | 
 |  | +                cl,
 | 
											
												
													
														|  | 
 |  | +                new MyInvocationHandler( new CountterImpl() )
 | 
											
												
													
														|  | 
 |  | +        );
 | 
											
												
													
														|  | 
 |  | +        o.add( 1,1 );
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +//        ClassPathXmlApplicationContext con =
 | 
											
												
													
														|  | 
 |  | +//                new ClassPathXmlApplicationContext("anno.xml");
 | 
											
												
													
														|  | 
 |  | +//        Counter countterImpl = con.getBean("countterImpl", Counter.class);
 | 
											
												
													
														|  | 
 |  | +//        countterImpl.add(6,6);
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  }
 |  |  }
 |