动态代理

概述 : Spring 框架有一个技术, 叫做 AOP 技术. (面向切面编程)

效果 : 拦截被调用对象的所有方法.
动态代理1

动态代理2

动态代理3

实现类完成
接口定义 :

public interface SuperStar {

    void sing(int money);
    void liveShow(int money);
    void sleep();
}

实现类定义 :

public class LiuYan implements SuperStar {

    @Override
    public void sing(int money) {
        System.out.println("柳岩为了唱了一首 <真的真的很爱你!> 歌曲. 挣了 = " + money);
    }

    @Override
    public void liveShow(int money) {
        System.out.println("柳岩参加了 <屌丝男士!> 节目. 挣了 = " + money);
    }

    @Override
    public void sleep() {
        System.out.println("柳岩真的很累了, 休息休息...");
    }
}

测试类定义 :

@Test
public void test1() {

    SuperStar star = new LiuYan();

    star.sing(-10);
    star.liveShow(-20);
    star.sleep();
}

@Test
public void test2() {

SuperStar star = new LiuYan();

/************ 动态代理对象 start **************/
// 1. 使用 Proxy 类调用 newProxyInstance 方法
// 1.1 类加载器 (与被代理对象保持一致)
ClassLoader loader = star.getClass().getClassLoader();
// 1.2 interfaces 接口行为规范 (与被代理对象保持一致)
Class<?>[] interfaces = star.getClass().getInterfaces();
// 1.3 InvocationHandler 调用处理器 (实现类)
// 注意点 : 返回的 Object 类型对象必须要转成接口实现.
SuperStar proxy = (SuperStar) Proxy.newProxyInstance(loader, interfaces, new MyInvocationHandler(star));
/************ 动态代理对象 end **************/
// System.out.println(proxy.getClass());
proxy.sing(10000);
proxy.liveShow(200000);
proxy.sleep();

}

MyInvocationHandler 实现类代码编写 :
简化版本 :

public class MyInvocationHandler implements InvocationHandler {
    // 属性
    private SuperStar star;

    // 构造方法
    public MyInvocationHandler(SuperStar star) {
        this.star = star;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable  {
        // 直接放行 
        return method.invoke(star, args);   
    }
}

判断版本 :

public class MyInvocationHandler implements InvocationHandler {
    // 属性
    private SuperStar star;

    // 构造方法
    public MyInvocationHandler(SuperStar star) {
        this.star = star;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable  {

        // 1. 判断方法
        if (method.getName().equals("sing")) {
            // 2. 取出参数
            int money = (int) args[0];
            // 3. 判断参数
            if (money < 10000) {
                System.out.println("滚, 你这个穷屌丝, 赶紧回家撸代码去...");
                return null;
            }
            // 4. 参数合法
            // 抽提成
            System.out.println("代理抽取了 " + (money * 0.3) + " 回扣.");
            // 5. 调用真实的方法
            return method.invoke(star, (int)(money * 0.7));
        } else if ("liveShow".equals(method.getName())) {
            // 2. 取出参数
            int money = (int) args[0];
            // 3. 判断参数
            if (money < 100000) {
                System.out.println("滚, How old are you. 继续回家撸代码去...");
                return null;
            }
            // 4. 参数合法
            // 抽提成
            System.out.println("代理抽取了 " + (money * 0.3) + " 回扣.");
            // 5. 调用真实的方法
            return method.invoke(star, (int)(money * 0.7));
        }

        return method.invoke(star, args);   // 其它方法直接放行   (close  池)
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值