------- android培训、java培训、期待与您交流! ----------
import java.lang.reflect.Method; //定义公共的接口,两个方法计算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) { long endTime = System.currentTimeMillis(); System.out.println(method.getName() + " running time of " + (endTime - beginTime)); } public void beforeMethod(Method method) { beginTime = System.currentTimeMillis(); } }
import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.Collection; public class ProxyTest { public static void main(String[] args) throws Exception{ // 返回用指定的类加载器定义的代理类的Class对象,它可以实现指定的Collection接口 Class clazzProxy1 = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class); System.out.println(clazzProxy1.getName());//$Proxy0 System.out.println("----------begin constructors list----------"); //获取构造方法 Constructor[] constructors = clazzProxy1.getConstructors(); //以带参数的格式打印构造方法 for(Constructor constructor : constructors){ String name = constructor.getName(); StringBuilder sBuilder = new StringBuilder(name); sBuilder.append('('); Class[] clazzParams = constructor.getParameterTypes(); for(Class clazzParam : clazzParams){ sBuilder.append(clazzParam.getName()).append(','); } if(clazzParams!=null && clazzParams.length != 0) sBuilder.deleteCharAt(sBuilder.length()-1); sBuilder.append(')'); System.out.println(sBuilder.toString()); //只有一个$Proxy0(java.lang.reflect.InvocationHandler) } System.out.println("----------begin methods list----------"); /*$Proxy0() $Proxy0(InvocationHandler,int)*/ //获取代理类的方法,并打印,结果显示是Collection类的方法 Method[] methods = clazzProxy1.getDeclaredMethods(); for(Method method : methods){ String name = method.getName(); StringBuilder sBuilder = new StringBuilder(name); sBuilder.append('('); Class[] clazzParams = method.getParameterTypes(); for(Class clazzParam : clazzParams){ sBuilder.append(clazzParam.getName()).append(','); } if(clazzParams!=null && clazzParams.length != 0) sBuilder.deleteCharAt(sBuilder.length()-1); sBuilder.append(')'); System.out.println(sBuilder.toString()); } System.out.println("----------begin create instance object----------"); //Object obj = clazzProxy1.newInstance(); 没有无参的构造方法,不能这样创建对象 //获取代理类构造方法。 Constructor constructor = clazzProxy1.getConstructor(InvocationHandler.class); //定义InvocationHandler局部内部类,实现invoke方法 class MyInvocationHander1 implements InvocationHandler{ public Object invoke(Object proxy, Method method, Object[] args)throws Throwable { return null; } } //通过构造方法创建对象,传入MyInvocationHander1对象,因为知道是collection类,所以直接强制类型转换 Collection proxy1 = (Collection)constructor.newInstance(new MyInvocationHander1()); System.out.println(proxy1);//输出null,因为proxy1的toString方法被转发 proxy1.clear(); // proxy1.size();//调用带有返回值的方法出现空指针异常 //匿名内部类的方式 Collection proxy2 = (Collection)constructor.newInstance(new InvocationHandler(){ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null; } }); final ArrayList target = new ArrayList(); //抽取获取代理类方法getProxy(obj,advice),生成代理类对象 Collection proxy3 = (Collection)getProxy(target,new MyAdvice()); proxy3.add("zxx"); proxy3.add("lhm"); proxy3.add("bxd"); System.out.println(proxy3.size()); System.out.println(proxy3.getClass().getName());//$Proxy1 } //生成代理类实例方法,接收代理目标对象,和advice两个参数 private static Object getProxy(final Object target,final Advice advice) { //直接使用Proxy类的newProxyInstance方法创建代理类对象 Object proxy3 = Proxy.newProxyInstance( target.getClass().getClassLoader(), //参数一,ClassLoader target.getClass().getInterfaces(), //参数二,实现的接口数组 new InvocationHandler(){ //参数三,InvacationHandler对象 public Object invoke(Object proxy, Method method, Object[] args)throws Throwable { //将advice定义为final类型,方法接收需要代理的方法名 //在invoke方法前后添加功能代码 advice.beforeMethod(method); //target为需要代理的接口 Object retVal = method.invoke(target, args); advice.afterMethod(method); return retVal; } } ); return proxy3; } }
import java.io.InputStream; import java.util.Collection; public class AopFrameworkTest { public static void main(String[] args) throws Exception { // 关联配置文件 InputStream ips = AopFrameworkTest.class.getResourceAsStream("config.properties"); // 根据配置信息生成Bean工厂对象并返回对象 Object bean = new BeanFactory(ips).getBean("xxx"); System.out.println(bean.getClass().getName());//$Proxy0 代理类 ((Collection)bean).clear(); } }
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import cn.itcast.day3.Advice; public class BeanFactory { //通过构造方法关联配置文件,初始化配置文件 Properties props = new Properties(); public BeanFactory(InputStream ips){ try { props.load(ips); } catch (IOException e) { e.printStackTrace(); } } //根据配置文件获取相应的对象 public Object getBean(String name){ String className = props.getProperty(name); Object bean = null; try { Class clazz = Class.forName(className); bean = clazz.newInstance();//获取对象的实例 } catch (Exception e) { e.printStackTrace(); } if(bean instanceof ProxyFactoryBean){//如果对象时代理工厂类,则返回代理代理工厂对象 Object proxy = null; ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean)bean; try { //代理需要advice和target,获取advice和target对象 Advice advice = (Advice)Class.forName(props.getProperty(name + ".advice")).newInstance(); Object target = Class.forName(props.getProperty(name + ".target")).newInstance(); //设置代理工厂对象的配置信息 proxyFactoryBean.setAdvice(advice); proxyFactoryBean.setTarget(target); //生成动态代理对象 proxy = proxyFactoryBean.getProxy(); } catch (Exception e) { e.printStackTrace(); } return proxy; } return bean; } }
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import cn.itcast.day3.Advice; public class ProxyFactoryBean { //代理工厂的两个属性 private Advice advice; private Object target; public Advice getAdvice() { return advice; } public void setAdvice(Advice advice) { this.advice = advice; } public Object getTarget() { return target; } public void setTarget(Object target) { this.target = target; } //生成代理对象方法 public Object getProxy() { Object proxy3 = 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 proxy3; } }
------- android培训、java培训、期待与您交流! ----------