java 動態代理的案例使用與解析

java 動態代理的案例使用與解析


解析在代碼註釋中,不多做解釋

import java.lang.reflect.Method;
//簡單的方法攔截接口
public interface Advice {
	void beforeMethod(Method method);
	void afterMethod(Method method);
}
import java.lang.reflect.Method;
//簡單的接口實現
public class MyAdvice implements Advice {
	long beginTime = 0;
	public void afterMethod(Method method) {
		// TODO Auto-generated method stub
		System.out.println("after method:" + method.getName());
		long endTime = System.currentTimeMillis();
		System.out.println(method.getName() + " running time of " + (endTime - beginTime));

	}

	public void beforeMethod(Method method) {
		// TODO Auto-generated method stub
		System.out.println("before method:" + method.getName());
		beginTime = System.currentTimeMillis();
	}

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


//獲取java 代理對象的工具類
public class ProxyUtil {
    public static Object getProxy(final Object target,final Advice advice) {
//        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");//生成的代理class對象在工程目錄下的com\sun\proxy
        Object proxy;
        proxy= Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler(){
                    public Object invoke(Object proxy, Method method, Object[] args)
                            throws Throwable {
                        advice.beforeMethod(method);
                        Object retVal = method.invoke(target, args);
                        advice.afterMethod(method);
                        return retVal;
                    }
                }
        );
        return proxy;
    }

}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

//使用Jdk 動態代理類必須實現接口,代理的方法包括接口中方法和Object的基礎方法,驗證想要被代理的方法必須代理
//類實現接口中定義的方法
public class ProxyEg1 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ProxyEg1 eg1 = new ProxyEg1();
//        ArrayList<Integer> eg1 = new ArrayList<>();
        Object proxy = ProxyUtil.getProxy(eg1, new MyAdvice());
        Method method = ProxyEg1.class.getMethod("method");
        System.out.println(method);
        System.out.println(proxy);
//        method.invoke(proxy);//報錯 object is not an instance of declaring class,表明proxy和ProxyEg1 沒有繼承關係
        System.out.println("eg1 end-------");
        Method[] methods = proxy.getClass().getMethods();//打印代理類的所有方法
        for (Method m : methods) {
            System.out.println(m.getName());//發現並沒有method方法
        }
        System.out.println("eg2 end-------");
        ArrayList<Integer> eg2 = new ArrayList<>();
        Collection p1 =(Collection) ProxyUtil.getProxy(eg2, new MyAdvice());
        System.out.println(p1.getClass());
        System.out.println(p1);
        p1.add(1);


		//實現代理的案例
		final ArrayList target = new ArrayList();			
		Collection proxy3 = (Collection)ProxyUtil.getProxy(target,new MyAdvice());
		proxy3.add("zxx");
		proxy3.add("lhm");
		proxy3.add("bxd");
		System.out.println(proxy3.size());
		System.out.println(proxy3.getClass().getName());

    }

    public void method(){
        System.out.println("method run");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章