动态代理与普通的代理相比较,最大的好处是接口中声明的所有方法都被转移到一个集中的方法中处理(invoke),这样,在接口方法数量比较多的时候,我们可以进行灵活处理,而不需要像静态代理那样每一个方法进行中转。
动态代理类只能代理接口,代理类都需要实现InvocationHandler类,实现invoke方法。该invoke方法就是调用被代理接口的所有方法时需要调用的,该invoke方法返回的值是被代理接口的一个实现类动态代理典型例子就是spring的AOP
一般应用场景:
1:日志记录
2:事务处理
3:检测程序中某个方法的瓶颈
下面看下具体的代码实现:
package com.gd.proxy;
/**
*
* @author sandy
*
*/
public class Car {
private String brand;
public Car(String brand) {
this.brand = brand;
}
//获取汽车的描述
public String getDescribe() {
return brand + "车";
}
}
package com.gd.proxy;
/**
*
* @author sandy
*
*/
public class QQCar extends Car {
public QQCar(String brand) {
super(brand);
}
}
package com.gd.proxy;
/**
*
* @author sandy
* 宝马车
*/
public class BMWCar extends Car {
public BMWCar(String brand) {
super(brand);
}
}
package com.gd.proxy;
/**
*
* @author sandy
*
*/
public interface CarProxy {
//代理卖车
void sellCar(Car car);
}
package com.gd.proxy;
/**
*
* @author sandy
*
*/
public class CarProxyImpl implements CarProxy {
public void sellCar(Car car) {
System.out.println("卖出一辆" + car.getDescribe());
}
}
package com.gd.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
*
* @author sandy
* 代理商类
*/
public class AgentHandler implements InvocationHandler {
Object target;
public AgentHandler(Object target) {
super();
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("方法执行前开始记录日志.....................");
Object o = method.invoke(target, args);
System.out.println("方法执行后停止记录日志.....................");
return o;
}
}
测试类:
/**
*
*/
package com.gd.proxy;
import java.lang.reflect.Proxy;
import junit.framework.TestCase;
/**
* @author sandy
* 代理测试客户端
*/
public class DynamicProxyTest extends TestCase {
public void testsellCar(){
CarProxy proxy=(CarProxy)Proxy.newProxyInstance(CarProxy.class.getClassLoader(),new Class[]{CarProxy.class}, new AgentHandler(new CarProxyImpl()));
proxy.sellCar(new QQCar("qq"));
proxy.sellCar(new BMWCar("宝马"));
}
}