本站首页    管理页面    写新日志    退出


«September 2025»
123456
78910111213
14151617181920
21222324252627
282930


公告
本博客在此声明所有文章均为转摘,只做资料收集使用。并无其他商业用途。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:210
评论数量:205
留言数量:-19
访问次数:920195
建立时间:2007年5月10日




[J2SE相关]InvocationHandler 操作例子(切面概念)
文章收藏,  网上资源,  软件技术,  电脑与网络

李小白 发表于 2007/6/6 20:18:19

public interface IHello {public void hello(String name); }--------------------------------------------------------------------------------public class HelloSpeaker implements IHello { public void hello(String name) {System.out.println("Hello, " + name);}} --------------------------------------------------------------------------------import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.logging.Level;import java.util.logging.Logger;public class LogHandler implements InvocationHandler {private Object delegate;private Logger logger = Logger.getLogger(this.getClass().getName());public Object bind(Object delegate) {this.delegate = delegate;return Proxy.newProxyInstance(delegate.getClass().getClassLoader(), delegate.getClass().getInterfaces(), this);}public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Object result = null;try {log("method starts..." + method);result = method.invoke(delegate, args);logger.log(Level.INFO, "method ends..." + method);} catch (Exception e) {log(e.toString());}return result;}private void log(String message) {logger.log(Level.INFO, message);}} --------------------------------------------------------------------------------public class ProxyDemo {public static void main(String[] args) {LogHandler logHandler = new LogHandler();IHello helloProxy = (IHello) logHandler.bind(new HelloSpeaker());helloProxy.hello("Justin");}』好了,拷贝上面的自己运行看看就知道了,原来切面就是这么回事。


阅读全文(2606) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.033 second(s), page refreshed 144765779 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号