dynamicproxy.java

来自「动态网站管理发布系统」· Java 代码 · 共 38 行

JAVA
38
字号
package com.ntsky.proxy;

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

public class DynamicProxy implements InvocationHandler{
  private TestInterface testInterface;
  public DynamicProxy(){
	  testInterface = new Test2();
  }
  
  public TestInterface create(){
	  TestInterface testInterface = null;
	  testInterface = (TestInterface)Proxy.newProxyInstance(TestInterface.class.getClassLoader(),new Class[]{TestInterface.class},this);
	  return testInterface;
  }
  public Object invoke(Object obj,Method method,Object[] args){
	  Object o = null;
	  try {
		System.out.println("方法名="+method.getName());
	    if(method.getName().equals("sayHello")){
		  System.out.println("调用sayHello");
	    }else{
		  o = method.invoke(testInterface,args);
	    }
	  } catch (IllegalArgumentException e) {
		e.printStackTrace();
	  } catch (IllegalAccessException e) {
		e.printStackTrace();
	  } catch (InvocationTargetException e) {
		e.printStackTrace();
	  }	  
	  return o;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?