方法性能监测
  监测方法运行性能效率,此处简单以方法运行时间作为评价标准。传统的做法是在每个方法执行前后各自记录时间,然后计算运行时间。采用动态代理机制,可以将以上操作统一到InvocationHandler中,并创建代理类监测所有的方法。
  InvocationHandler实现类
  import java.lang.reflect.InvocationHandler;
  import java.lang.reflect.Method;
  public class PerformanceInterceptor implements InvocationHandler {
  private Object proxied;
  public PerformanceInterceptor(Object proxied) {
  this.proxied = proxied;
  }
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  long startTime = System.currentTimeMillis();
  Object obj = method.invoke(proxied, args);
  long endTime = System.currentTimeMillis();
  System.out.println("Method " + method.getName() + " execution time: " + (endTime - startTime) * 1.0 / 1000 + "s");
  return obj;
  }
  }
  测试类
  import java.lang.reflect.InvocationHandler;
  import java.lang.reflect.Proxy;
  import org.junit.Test;
  public class PerformanceInterceptorTest {
  @Test
  public void testInvoke() {
  UserDao userDao = new UserDaoImpl();
  Class<?> cls = userDao.getClass();
  InvocationHandler handler = new PerformanceInterceptor(userDao);
  UserDao proxy = (UserDao) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), handler);
  proxy.addUser(new User("tom"));
  proxy.deleteUser("tom");
  proxy.updateUser("tom");
  }
  }
  测试结果
  add user named Tom...
  Method addUser execution time: 1.0s
  delete user named tom...
  Method deleteUser execution time: 1.5s
  update user named tom...
  Method updateUser execution time: 2.0s
  日志管理
  有时候开发者需要在方法开始执行和执行结束时打印一些信息,使用动态代理可以对所有方法或者某些方法进行简单日志管理,并写入到指定文件。
  InvocationHandler实现类
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class LogInterceptor implements InvocationHandler {
private Object proxied;
public static final String path = "run.log";
public LogInterceptor(Object proxied) {
this.proxied = proxied;
}
public String beforeMethod(Method method) {
return getFormatedTime() + " Method:" + method.getName() + " start running ";
}
public String afterMethod(Method method) {
return getFormatedTime() + " Method:" + method.getName() + " end running ";
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
write(path, beforeMethod(method));
Object object = method.invoke(proxied, args);
write(path, afterMethod(method));
return object;
}
public String getFormatedTime() {
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return formater.format(System.currentTimeMillis());
}
public void write(String path, String content) {
FileWriter writer = null;
try {
writer = new FileWriter(new File(path), true);
writer.write(content);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != writer) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
  测试类
  import java.lang.reflect.InvocationHandler;
  import java.lang.reflect.Proxy;
  import org.junit.Test;
  public class LogInterceptorTest {
  @Test
  public void testInvoke() {
  UserDao userDao = new UserDaoImpl();
  Class<?> cls = userDao.getClass();
  InvocationHandler handler = new LogInterceptor(userDao);
  UserDao proxy = (UserDao) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), handler);
  proxy.addUser(new User("tom"));
  proxy.deleteUser("tom");
  proxy.updateUser("tom");
  }
  }
  测试结果
  在工程根目录下生成run.log文件,内容如下:
  2015-10-07 05:41:02 Method:addUser start running
  2015-10-07 05:41:03 Method:addUser end running
  2015-10-07 05:41:03 Method:deleteUser start running
  2015-10-07 05:41:05 Method:deleteUser end running
  2015-10-07 05:41:05 Method:updateUser start running
  2015-10-07 05:41:07 Method:updateUser end running
  总结
  尽管Java动态代理机制设计的已经非常出色,美中不足之处的是,Java动态代理只能代理实现接口的类。若想要对没有实现任何接口的类进行代理,这种情况确实罕见,需要使用cdlib。Java动态代理机制是实现AOP编程的重要技术之一,动态代理并非开发者日常使用的工具,但是在某些特殊的场景,使用动态代理可以非常好地解决某些类型的问题。