核心类:
java.lang.reflect.Proxy
java.lang.reflect.InvocationHandler
JDK动态代理要点:
- 被代理对象要实现接口
- 必须实现
java.lang.reflect.InvocationHandler
类并重写invoke
方法 - 在
invoke
方法里可以对要代理的对象进行增强,invoke
方法的第2个参数method
就是被代理对象要增强的方法,调用前需要与被代理对象绑定,也就是要往method.invoke()
的第一个参数传递被代理对象
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class DynamicProxyPattern {
static interface IService{
void doSomePost();
}
static class Serviceimp implements IService{
public void doSomePost(){
System.out.println("提交表单更新数据");
}
}
static class EnhanceProxy{
private IService service = new Serviceimp();
public void preEnhanceMethod(){
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 在此处可以添加被代理对象的增强行为
System.out.println("前置增强效果:");
System.out.println("检查用户是否有权限更新数据");
return method.invoke(service, args);
}
};
IService proxy = (IService) Proxy.newProxyInstance(service.getClass().getClassLoader(),
service.getClass().getInterfaces(), handler);
proxy.doSomePost();
}
public void postEnhanceMethod(){
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("后置增强效果:");
Object res = method.invoke(service, args);
System.out.println("返回更新操作的结果,成功还是失败?");
return res;
}
};
IService proxy = (IService) Proxy.newProxyInstance(service.getClass().getClassLoader(),
service.getClass().getInterfaces(), handler);
proxy.doSomePost();
}
}
public static void main(String[] args) {
EnhanceProxy proxy = new EnhanceProxy();
proxy.preEnhanceMethod();
System.out.println();
proxy.postEnhanceMethod();
}
}
运行结果如下:
前置增强效果:
检查用户是否有权限更新数据
提交表单更新数据
后置增强效果:
提交表单更新数据
返回更新操作的结果,成功还是失败?