查字典范文网 >> java的动态代理分析通用

java的动态代理分析通用

小编:zdfb

在日常的学习、工作、生活中,肯定对各类范文都很熟悉吧。那么我们该如何写一篇较为完美的范文呢?下面是小编帮大家整理的优质范文,仅供参考,大家一起来看看吧。

java的动态代理分析篇一

引导语:在使用动态代理类时,我们必须实现invocationhandler接口,以下是百分网小编分享给大家的java动态代理,欢迎阅读了解!

java代码

package test1;

// 被代理类 需实现的 接口

public interface proxied {

void dosomething();

void dosomethingelse(string str);

}

java代码

package test1;

public class concreteproxied implements proxied {

@override

public void dosomething() {

try {

(100);

} catch (interruptedexception e) {

n("error : interruptedexception");

}

n(ss().getsimplename()

+ " >> dosomething .");

}

@override

public void dosomethingelse(string str) {

try {

(150);

} catch (interruptedexception e) {

n("error : interruptedexception");

}

n(ss().getsimplename()

+ " >> dosomethingelse , argument = " + str + ".");

}

}

java代码

package test1;

import tionhandler;

import ;

public class timeinginvocationhandler implements invocationhandler{

//被代理的对象

private object proxied;

public timeinginvocationhandler(object proxied){

d = proxied;

}

// 参数 proxy 表示代理类的对象

// 参数 method 表示被代理类 和 代理类 都实现的接口 的方法对象

// 参数 args 表示方法 method 的参数数组

@override

public object invoke(object proxy, method method, object[] args)

throws throwable {

n(laringclass().getname());

long currenttimemillis = ttimemillis();

object ret = (proxied, args);

n(ss().getsimplename()+" >> wastes time : "

+(ttimemillis() - currenttimemillis)+"ms");

return ret;

}

}

java代码

package test1;

import ;

public class testproxy {

public static void main(string[] args) {

proxied proxied = new concreteproxied();

thing();

thingelse("only a string");

// 生成一个代理实例,这个代理实现了 proxied 接口

// 对这个代理(proxy)的`方法的调用 会 重定向到 timeinginvocationhandler 的 invoke 方法

proxied proxy = (proxied) xyinstance(

.getclassloader(), // 类加载器

new class[] { }, // 代理要实现的接口

new timeinginvocationhandler(proxied) // 调用处理器

);

thing();

thingelse("only a string");

}

}

运行test类,输出如下:

concreteproxied >> dosomething .

concreteproxied >> dosomethingelse , argument = only a string.

d

concreteproxied >> dosomething .

timeinginvocationhandler >> wastes time : 110ms

d

concreteproxied >> dosomethingelse , argument = only a string.

timeinginvocationhandler >> wastes time : 156ms

s("content_relate");

【java的动态代理分析】相关文章:

1.java使用动态代理来实现aop2.几分钟理解java中的动态代理3.java利用反射实现动态代理实现代码4.关于java动态实现的方法5.java动态方法调度实例6.java的动态考试系统的设计7.java中如何实现显示动态的时间8.java中静态绑定和动态绑定的区别9.jni是怎样java调用c动态库

热点推荐

上一篇:最新初一下学期暑假生活指导答案通用

下一篇:增资协议书范本合同精选6篇