InvocationHandler的一个例子
来源:互联网 发布:开淘宝店铺步骤 编辑:程序博客网 时间:2024/04/27 19:54
InvocationHandler的一个例子
http://blog.csdn.net/linminqin/article/details/6313348
《深入浅出Hibernate》中有个JDBC运用InvocationHandler的例子
例子环境:
使用动态代理,在开发人员使用close()方法的时候,不是使用java的Connection的close方法,而是使用自定义的数据库连接池类的close()方法
Public class ConnectionHandler implements InvocationHandler { Connection dbConn; ConnectionPool connPool; //自定义线程池借口 public ConnectionHandler(ConnectionPool connPool) { this.connPool = connPool; } /** * 将动态代理绑定到指定Connection * @param conn * @return 绑定后的Connection */ public Connection bind(Connection conn) { this.dbConn= conn; Connection proxyConn = (Connection)Proxy.NewProxyInstance(conn.getClass().getClassLoader(), conn.getClass().getInterFaces(), this); return proxyConn ; } /* * 方法调用拦截器 * 判断当前调用的方法是否"close"方法 * 如果是,则调用pool.releaseConnection方法作为标准close方法的替代 */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object obj = null; //如果调用的事close()方法,则用pool的releaseConnection方法将其替换掉 if("close".equals(method.getName())) { pool.releaseConnection(dbConn); } else { obj = method.invoke(dbConn, args); } return obj; } }
配合Dynamic Proxy模式,DBConnectionPool.getConnection方法也做了一点小小的修改public synchronized Connection getConnection() throws DBException { if(pool == null) { pool = new Vector(); } Connection conn; if(pool.isEmpty()) { conn = createConnection(); } else { int lastIndex = pool.size() - 1; conn = pool.get(lastIndex); pool.remove(pool.get(lastIndex)); } ConnectionHandler connHandler = new ConnectionHandler(this); return connHandler.bind(conn); }
- InvocationHandler的一个例子
- InvocationHandler的一个例子
- 关于InvocationHandler的学习心得
- InvocationHandler 的异常处理
- 关于InvocationHandler的学习心得
- InvocationHandler
- InvocationHandler
- InvocationHandler
- InvocationHandler
- InvocationHandler 操作例子(切面概念)
- java动态代理例子(Proxy,InvocationHandler)
- invocationHandler动态代理的使用
- 初探java的Proxy+InvocationHandler
- 一个触发器的例子
- LineDDA的一个例子
- 一个函数的例子
- 一个触发器的例子
- DirectX的一个例子
- MIPS 在linux中的内存映射
- android 裁剪图片
- 我的第十八个C++上机报告(编程序,输出1/3-3/5+5/7-7/9…+19/21的结果)
- mountvol命令修改磁盘分区/光驱的盘符
- 第十周上机项目一:利用循环求和
- InvocationHandler的一个例子
- 花生哥的伤感爱情日志:伤害我,你会心痛吗?
- Extjs4.0学习指南
- 谈数据库索引和Sqlite中索引的使用
- 斗地主游戏源码
- 3I工作室烧烤活动
- sql练习
- Python中时间戳与时间字符串互相转化
- 求1000以内所有偶数的和