码迷,mamicode.com
首页 > 编程语言 > 详细

Spring学习-day09

时间:2018-03-11 21:41:04      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:Spring

动态代理:
技术分享图片

package com.atguigu.spring.aop.helloworld;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
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 h = new InvocationHandler() {
            /**
             * proxy: 正在返回的那个代理对象, 一般情况下, 在 invoke 方法中都不使用该对象
             * method: 正在被调用的方法
             * args: 调用方法时, 传入的参数
             */
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) 
                    throws Throwable {
                String methodName = method.getName();
                //日志
                System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
                //执行方法
                Object result = method.invoke(target, args);
                //日志
                System.out.println("The method " + methodName + " ends with " + result);
                return result;
            }
        };
        proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);

        return proxy;
    }
}
package com.atguigu.spring.aop.helloworld;

public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

    @Override
    public int add(int i, int j) {

        int result = i + j;
        return result;
    }

    @Override
    public int sub(int i, int j) {

        int result = i - j;
        return result;
    }

    @Override
    public int mul(int i, int j) {

        int result = i * j;
        return result;
    }

    @Override
    public int div(int i, int j) {

        int result = i / j;
        return result;
    }

}
package com.atguigu.spring.aop.helloworld;

public interface ArithmeticCalculator {
    //加减乘除
    int add(int i, int j);
    int sub(int i, int j);

    int mul(int i, int j);
    int div(int i, int j);
}
package com.atguigu.spring.aop.helloworld;

public class Main {
    public static void main(String[] args) {
//      ArithmeticCalculatorImpl arithmeticCalculatorImpl = new ArithmeticCalculatorImpl();

        ArithmeticCalculator target = new ArithmeticCalculatorImpl();
        ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy();

        int result = proxy.add(1, 2);
        System.out.println("-->" + result);

        result = proxy.div(4, 2);
        System.out.println("-->" + result);
    }
}

Spring学习-day09

标签:Spring

原文地址:http://blog.51cto.com/13416247/2085306

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!