本帖最后由 朱明仁 于 2015-3-15 00:34 编辑  
 
Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),new InvocationHandler(){。。。。。这里面InvocationHandler是作为内部类传入的,所在的代码黏贴在下面: 
 
/* 
 * proxy4代理的可以是其他有接口的对象 
 */ 
import java.lang.reflect.InvocationHandler; 
import java.lang.reflect.Method; 
import java.lang.reflect.Proxy; 
import java.util.ArrayList; 
import java.util.List; 
 
 
public class test { 
 
        /** 
         * @param args 
         */ 
        public static void main(String[] args) { 
                // TODO Auto-generated method stub 
                ArrayList target=new ArrayList(); 
                Advice_1 advice=new MyAdvice_1(); 
                List proxy=(List)getProxy(target,advice); 
                proxy.add(232); 
                //System.out.println(proxy.isEmpty()); 
        } 
        public static Object getProxy(final ArrayList target,final Advice_1 advice){ 
                Object proxy=Proxy.newProxyInstance(target.getClass().getClassLoader(), 
                                target.getClass().getInterfaces(), 
                                new InvocationHandler(){ 
                                         
                                        @Override 
                                        public Object invoke(Object proxy, Method method, 
                                                        Object[] args) throws Throwable { 
                                                // TODO Auto-generated method stub 
                                                advice.beforeMethod(); 
                                                long start=System.currentTimeMillis(); 
                                                Object retVal=method.invoke(target, args); 
                                                long end=System.currentTimeMillis(); 
                                                //System.out.println(end-start+"  milliseconds"); 
                                                return retVal; 
                                        } 
 
                }); 
                return proxy; 
        } 
 
} 
class MyAdvice_1 implements Advice_1{ 
 
        public void beforeMethod(){ 
                System.out.println("方法前输出。。。。。"); 
        } 
 
} 
interface Advice_1{ 
        public abstract void beforeMethod(); 
} 
 |