//config.properties配置文件 proxyName=java.util.ArrayList #proxyName=mySpringFramework.ProxyFactoryBean proxyName.advice=mySpringFramework.CountTime proxyName.target=java.util.ArrayList //代理类 package mySpringFramework; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import proxyTest.Advice; public class ProxyFactoryBean { private Object target; private Advice ad; public Object getTarget() { return target; } public void setTarget(Object target) { this.target = target; } public Advice getAd() { return ad; } public void setAd(Advice ad) { this.ad = ad; } public Object getProxy(){ Object obj = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { ad.beginTime(method); Object me = method.invoke(target, args); ad.endTime(method); return me; } } ); return obj; } } //Bean工厂创建Bean对象或者代理类,经过类型判断是否为代理类 package mySpringFramework; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import proxyTest.Advice; public class BeanFactory { private Properties props = new Properties(); public BeanFactory(InputStream ips){ try { this.props.load(ips); ips.close(); } catch (IOException e) { e.printStackTrace(); } } public Object getBean(String name) throws Exception{ Object obj = null; obj = Class.forName(props.getProperty(name)).newInstance(); if(obj instanceof ProxyFactoryBean){ Object target = Class.forName( props.getProperty(name+".target")).newInstance(); Advice advice = (Advice)Class.forName( props.getProperty(name+".advice")).newInstance(); ProxyFactoryBean pfb = new ProxyFactoryBean(); pfb.setTarget(target); pfb.setAd(advice); obj = pfb.getProxy(); } return obj; } } //需要添加计时功能的功能类 package mySpringFramework; import java.lang.reflect.Method; import proxyTest.Advice; public class CountTime implements Advice{ private long beginTime; @Override public void endTime(Method me) { System.out.println(me.getName()+"执行了"+ (System.currentTimeMillis()-beginTime)); } @Override public void beginTime(Method me) { beginTime = System.currentTimeMillis(); } } //单元测试类 package mySpringFramework; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.Collection; public class Test { public static void main(String[] args) throws Exception { InputStream ips = Test.class.getResourceAsStream( "config.properties"); BeanFactory bf = new BeanFactory(ips); Collection list = (Collection)bf.getBean("proxyName"); list.add("abv"); list.add("zhangsa"); System.out.println(list.size()); } } 觉得好的给个脚印呗!!!!HOHO