JDK动态代理
JDK 1.3以后,Java提供了动态代理的技术,允许开发者在运行期创建接口的代理实例。在Sun刚推出动态代理时,还很难想象它有多大的实际用途,现在我们终于发现动态代理是实现AOP的绝好底层技术。
JDK的动态代理主要涉及到java.lang.reflect包中的两个类:Proxy和InvocationHandler。其中InvocationHandler是一个接口,可以通过实现该接口定义横切逻辑,并通过反射机制调用目标类的代码,动态将横切逻辑和业务逻辑编织在一起。
而Proxy利用InvocationHandler动态创建一个符合某一接口的实例,生成目标类的代理对象。这样讲一定很抽象,我们马上着手使用Proxy和InvocationHandler这两个魔法戒
public interface ForumService {
public void removeTopic(int topicId);
public void removeForum(int forrumId);
}
public class ForumServiceImpl implements ForumService {
public void removeForum(int forumId) {
// ①-1开始对该方法进行性能监视
// PerformanceMonitor.begin("removeForum");
System.out.println("模拟删除Forum记录:"+forumId);
try {
Thread.currentThread().sleep(40);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// PerformanceMonitor.end();
}
public void removeTopic(int topicId) {
// PerformanceMonitor.begin("removeTopic");
System.out.println("模拟删除Topic记录:"+topicId);
try {
Thread.currentThread().sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// PerformanceMonitor.end();
}
}
public class PerformanceHandler implements InvocationHandler {
private Object target;
public PerformanceHandler(Object target) {
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
PerformanceMonitor.begin(target.getClass().getName()+"."+method.getName());
Object obj = method.invoke(target, args);
PerformanceMonitor.end();
return obj;
}
}
public class PerformanceMonitor {
// ①通过一个ThreadLocal保存调用线程相关的性能监视信息
private static ThreadLocal<MethodPerformance> performanceRecord
= new ThreadLocal<MethodPerformance>();
// ②启动对某一目标方法的性能监视
public static void begin(String method) {
System.out.println("begin monitor...");
MethodPerformance mp = new MethodPerformance(method);
performanceRecord.set(mp);
}
public static void end() {
System.out.println("end monitor...");
MethodPerformance mp = performanceRecord.get();
mp.printPerformance();
}
}
public class MethodPerformance {
private long begin;
private long end;
private String serviceMethod;
public MethodPerformance(String serviceMethod) {
this.serviceMethod = serviceMethod;
this.begin = System.currentTimeMillis();
}
public void printPerformance() {
end = System.currentTimeMillis();
long elapse = begin - end;
System.out.println(serviceMethod + " 花费 " + elapse + "毫秒");
}
}
import java.lang.reflect.Proxy;
public class TestProxyForumService {
public static void main(String[] args) {
ForumService target = new ForumServiceImpl();
PerformanceHandler handler = new PerformanceHandler(target);
ForumService proxy = (ForumService)Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
handler);
proxy.removeForum(10);
proxy.removeTopic(234);
}
}
输出结果:
begin monitor...
模拟删除Forum记录:10
end monitor...
test.aop.ForumServiceImpl.removeForum 花费 -40毫秒
begin monitor...
模拟删除Topic记录:234
end monitor...
test.aop.ForumServiceImpl.removeTopic 花费 -20毫秒
本文参考:http://www.iteye.com/topic/1123293