JDK动态代理中包含一个类和一个接口:
InvocationHandler接口:
public interface InvocationHandler {
public Object invoke(Object proxy,Method method,Object[] args) throws Throwable;
}
参数说明:
Object proxy:指被代理的对象。
Method method:要调用的方法
Object[] args:方法调用时所需要的参数
接口IFromService
package com.test.service; public interface IFromService { public void removeService(long serviveid) throws Exception; public void modifyService(long serviveid)throws Exception; }
接口实现类 FromServieImpl
package com.test.service; public class FromServieImpl implements IFromService{ @Override
public void removeService(long serviveid) throws Exception {
// TODO Auto-generated method stub
System.out.println("模拟删除服务ID:"+serviveid);
Thread.currentThread().sleep(20);
} @Override
public void modifyService(long serviveid) throws Exception {
// TODO Auto-generated method stub
System.out.println("模拟修改服务ID:"+serviveid);
Thread.currentThread().sleep(40);
} /**
* 无接口实现类方法
* @param obj
* @throws InterruptedException
*/
public void addService(Object obj) throws InterruptedException{
System.out.println("模拟新增服务:"+obj);
Thread.currentThread().sleep(40);
} }
jdk代理类 FromServiceProxy
package com.test.proxy; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Date; import com.test.utils.DateTimeUtil; /**
* jdk 动态代理
* @author Administrator
*
*/
public class FromServiceProxy implements InvocationHandler{
private Object target; /**
* 绑定委托类并返回代理类
* @param target
* @return
*/
public Object bind(Object target){
this.target=target;
return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
} /**
* 调用
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
Object result=null;
Date stdate=new Date();
System.out.println("监测 开始时间:"+DateTimeUtil.dateToStrOfDefaulfFormat(stdate));
result=method.invoke(target, args);
Date eddate=new Date();
System.out.println("监测 结束时间:"+DateTimeUtil.dateToStrOfDefaulfFormat(eddate));
System.out.println(method.getName()+" 执行耗时:"+(eddate.getTime()-stdate.getTime())+" ms"); return result;
} }
测试 TestProxy
package com.test.proxy; import com.test.service.FromServieImpl;
import com.test.service.IFromService; public class TestProxy { public static void main(String[] args) throws Exception {
System.out.println("==============jdk proxy=================");
FromServiceProxy proxy=new FromServiceProxy();
IFromService service=(IFromService) proxy.bind(new FromServieImpl());
service.removeService(123);
service.modifyService(123);
}
}
输出如下:
==============jdk proxy=================
监测 开始时间:2016-03-24 13:25:48
模拟删除服务ID:123
监测 结束时间:2016-03-24 13:25:48
removeService 执行耗时:54 ms
监测 开始时间:2016-03-24 13:25:48
模拟修改服务ID:123
监测 结束时间:2016-03-24 13:25:48
modifyService 执行耗时:41 ms
Cglib动态代理
JDK的动态代理机制只能代理实现了接口的类,而不能实现接口的类就不能实现JDK的动态代理,cglib是针对类来实现代理的,他的原理是对指定的目标类生成一个子类,并覆盖其中方法实现增强,但因为采用的是继承,所以不能对final修饰的类进行代理。
cglib代理 FromServiceCglibProxy
package com.test.proxy; import java.lang.reflect.Method;
import java.util.Date; import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy; import com.test.utils.DateTimeUtil; /**
* cglib 动态代理
* @author Administrator
*
*/
public class FromServiceCglibProxy implements MethodInterceptor{ private Object target; /**
* 创建代理对象
* @param target
* @return
*/
public Object getInstance(Object target){
this.target=target;
Enhancer enhancer=new Enhancer();
enhancer.setSuperclass(this.target.getClass());
enhancer.setCallback(this);
return enhancer.create();
} @Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
Date stdate=new Date();
System.out.println("监测 开始时间:"+DateTimeUtil.dateToStrOfDefaulfFormat(stdate));
proxy.invokeSuper(obj, args);
Date eddate=new Date();
System.out.println("监测 结束时间:"+DateTimeUtil.dateToStrOfDefaulfFormat(eddate));
System.out.println(method.getName()+" 执行耗时:"+(eddate.getTime()-stdate.getTime())+" ms");
return null;
} }
测试类 TestProxy
package com.test.proxy; import com.test.service.FromServieImpl;
import com.test.service.IFromService; public class TestProxy { public static void main(String[] args) throws Exception { System.out.println("==============cglib proxy=================");
FromServiceCglibProxy cglib=new FromServiceCglibProxy();
FromServieImpl serviceImpl=(FromServieImpl) cglib.getInstance(new FromServieImpl());
serviceImpl.addService("entity");
}
}
输出如下:
==============cglib proxy=================
监测 开始时间:2016-03-24 13:25:48
模拟新增服务:entity
监测 结束时间:2016-03-24 13:25:48
addService 执行耗时:65 ms