美文网首页
jdk动态代理

jdk动态代理

作者: 息息小眠虫 | 来源:发表于2019-08-11 23:55 被阅读0次
package com.gobaio.dynamicproxy.proxyCglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;

/**
 * @author fucong
 * @date 2019/8/11 21:00
 * @description jdk的动态代理
 */
public class Client {
    public static void main(String[] args) {
        Producer producer = new Producer();

        Producer producer1 =  (Producer)Enhancer.create(producer.getClass(),
                (MethodInterceptor) (proxy, method, objects, methodProxy) -> {
            Object returnValue = null;
            Float arg = (Float) objects[0];
            if ("salesProducts".equals(method.getName())) {
                returnValue = method.invoke(producer, arg * 0.8f);
            }
            return returnValue;
        });

        producer1.salesProducts(15000f);
    }
}

相关文章

网友评论

      本文标题:jdk动态代理

      本文链接:https://www.haomeiwen.com/subject/fbbejctx.html