java proxy 代理

来源:互联网 发布:中路火男打js好打吗 编辑:程序博客网 时间:2024/06/06 04:53

spring AOP 使用动态代理技术在运行期织入增强的代码,动态代理分为基于jdk的和基于CGLib两种,jdk本身只提供接口的代理,而cglib支持类的代理.

1.jdk动态代理

jdk1.3以后,java提供了动态代理技术,允许开发者在运行期创建接口的代理实例.jdk动态代理主要涉及到java.lang.reflect包中的两个类:Proxy和InvocationHandler.

其中InvocationHandler是一个接口,可卡因通过实现该接口定义横切逻辑,并通过反射机制调用目标类的代码,动态将横切逻辑和业务逻辑编织在一起.而Proxy利用InvocationHandler动态的创建一个符合某一接口的实例,生成目标类的代理对象.


以下是示例代码:



①接口

public interface Count {/** * 查看账户方法 */public void queryCount();}

②实现类

public class CountImpl implements Count {@Overridepublic void queryCount() {System.out.println("query method...");}}

③代理类

public class CountProxy2 implements InvocationHandler {//要代理的目标类实例Object target = null;public CountProxy2(Object target) {this.target = target;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args)throws Throwable {//调用目标类方法前System.out.println("before.....");Object result = method.invoke(target, args);//调用目标类方法后System.out.println("after.....");//返回代理类return result;}

④测试代码

/**** Proxy.newProxyInstance方法会做如下几件事:* * 1,根据传入的第二个参数interfaces动态生成一个类,实现interfaces中的接口,* 该例中即BusinessProcessor接口的processBusiness方法* 。并且继承了Proxy类,重写了hashcode,toString,equals等三个方法。具体实现可参看* ProxyGenerator.generateProxyClass(...); 该例中生成了$Proxy0类* * 2,通过传入的第一个参数classloder将刚生成的类加载到jvm中。即将$Proxy0类load* * 3,利用第三个参数,调用$Proxy0的$Proxy0(InvocationHandler)构造函数* 创建$Proxy0的对象,并且用interfaces参数遍历其所有接口的方法,并生成Method对象初始化对象的几个Method成员变量**/public class CountTest {public static void main(String[] args) {CountImpl countImpl = new CountImpl();CountProxy2 handler = new CountProxy2(countImpl);Count count = (Count) Proxy.newProxyInstance(countImpl.getClass().getClassLoader(), countImpl.getClass().getInterfaces(), handler);count.queryCount();}}

⑤打印结果

before.....
query method...
after.....


0 0
原创粉丝点击