Spring_16_AOP 基础

来源:互联网 发布:淘宝首页焦点图在哪里 编辑:程序博客网 时间:2024/05/16 06:07
  • 总结:
    aop的本质也就是在代理类中,有一个被代理类的成员变量,先在代理类中初始化被代理类对象,然后调用获取被代理对象的方法(getLoggingProxy),
    通过反射机制,创建了被代理类对象,并且有了invoke方法,被代理类对象的方法执行(result =
    method.invoke(target, args);),
    都可以转换为代理中的方法执行,并且可以在方法执行的前后做一些其他操作(比如result = method.invoke(target,
    args);前后的日志输出,或者返回结果的处理等)

  • 被代理类接口

package com.hgh.spring.aop;public interface ArithmeticCalculator {    int add(int i,int j);    int sub(int i,int j);}
  • 被代理类
package com.hgh.spring.aop;public class ArithmeticCalculatorImpl implements ArithmeticCalculator{    @Override    public int add(int i, int j) {        // TODO Auto-generated method stub        return i+j;    }    @Override    public int sub(int i, int j) {        // TODO Auto-generated method stub        return i-j;    }}
  • 代理类
package com.hgh.spring.aop;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.ArrayList;import java.util.Arrays;public class ArithmeticCalculatorLoggingProxy {    //要代理的对象    private ArithmeticCalculator target;    public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target){        this.target = target;    }    //返回代理对象    public ArithmeticCalculator getLoggingProxy(){        ArithmeticCalculator proxy = null;        ClassLoader loader = target.getClass().getClassLoader();        Class [] interfaces = new Class[]{ArithmeticCalculator.class};        InvocationHandler handler = new InvocationHandler() {            /**             * proxy: 代理对象。 一般不使用该对象             * method: 正在被调用的方法             * args: 调用方法传入的参数             */            @Override            public Object invoke(Object proxy, Method method, Object[] args)                    throws Throwable {                // TODO Auto-generated method stub                String methodName =method.getName();                System.out.println("the method " +methodName +"begin with"+Arrays.asList(args));                //调用目标方法的返回值,如果在这里设定什么值,那么就会给调用方法返回什么值                Object result = null;                try {                    result = method.invoke(target, args);                } catch (NullPointerException e) {                    e.printStackTrace();                }                //打印日志                System.out.println("[after] The method ends with " + result);                return 3;            }        };        /**         * loader: 代理对象使用的类加载器。          * interfaces: 指定代理对象的类型. 即代理代理对象中可以有哪些方法.          * handler: 当具体调用代理对象的方法时, 应该如何进行响应, 实际上就是调用 InvocationHandler 的 invoke 方法         */        proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, handler);        return proxy;    }}
  • 测试方法
package com.hgh.spring.aop;public class Main {    public static void main(String[] args) {        ArithmeticCalculator arithmeticCalculator = new ArithmeticCalculatorImpl();        arithmeticCalculator = new ArithmeticCalculatorLoggingProxy(arithmeticCalculator).getLoggingProxy();        int result = arithmeticCalculator.add(3, 4);        System.out.println("add:"+result);        result = arithmeticCalculator.sub(5, 2);        System.out.println("sub:"+result);    }}
0 0