mixpplus
11/6/2019 - 12:44 PM

proxy动态代理增强实现类

proxy动态代理增强实现类

package com.ingin.service.impl;

import com.ingin.service.IProduce;
import org.junit.Test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import static org.junit.Assert.*;

public class IProduceImplTest {
    private ProduceImpl produce = new ProduceImpl();

    @Test
    public void produce() {

        IProduce  iproduce = (IProduce)Proxy.newProxyInstance(produce.getClass().getClassLoader(), produce.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object retValue = null;
                double money = (double) args[0];
                retValue = method.invoke(produce,money*0.8);
                return retValue;
            }
        });
        iproduce.produce(1000.0);
    }
}