zoukankan      html  css  js  c++  java
  • Spring模板方法模式及Callback

    转载 http://explore.iteye.com/blog/667992  

    Spring对JDBC的抽象和对Hibernate的集成,都采用了一种理念或者处理方式,那就是模板方法模式与相应的Callback接口相结合。

     

     采用模板方法模式是为了以一种统一而集中的方式来处理资源的获取和释放,以JdbcTempalte为例,如下:

     

    Java代码  收藏代码
    1. public abstract class JdbcTemplate {  
    2.         
    3.       public final Object execute(String sql){  
    4.             Connection con=null;  
    5.             Statement stmt=null;  
    6.              try  
    7.              {  
    8.                  con=getConnection();  
    9.                  stmt=con.createStatement();  
    10.                  Object retValue=executeWithStatement(stmt,sql);  
    11.                  return retValue;  
    12.              }  
    13.              catch(SQLException e){  
    14.                  ...  
    15.              }  
    16.              finally  
    17.              {  
    18.                closeStatement(stmt);  
    19.                releaseConnection(con);  
    20.              }  
    21.        }  
    22.   
    23.        protected abstract Object executeWithStatement(Statement   stmt, String sql);  
    24. }  

       这样处理之后,JDBC代码的使用得到了规范,连接释放等问题也得到了统一的管理。

      但是,JdbcTemplate是抽象类,不能够独立使用,我们每次进行数据访问的时候都要给出一个相应的子类实现,这样肯定不方便,所以就引入了回调。

    Java代码  收藏代码
    1. public interface StatementCallback{  
    2.       Object doWithStatement(Statement stmt);  
    3. }  

      

     

    Java代码  收藏代码
    1. public class JdbcTemplate {  
    2.         
    3.       public final Object execute(StatementCallback callback){  
    4.             Connection con=null;  
    5.             Statement stmt=null;  
    6.              try  
    7.              {  
    8.                  con=getConnection();  
    9.                  stmt=con.createStatement();  
    10.                  Object retValue=callback.doWithStatement(stmt);  
    11.                  return retValue;  
    12.              }  
    13.              catch(SQLException e){  
    14.                  ...  
    15.              }  
    16.              finally  
    17.              {  
    18.                closeStatement(stmt);  
    19.                releaseConnection(con);  
    20.              }  
    21.        }  
    22.   
    23.        ...//其它方法定义  
    24. }  

     

    使用如下:

     

    Java代码  收藏代码
    1. JdbcTemplate jdbcTemplate=...;  
    2. final String sql=...;  
    3. StatementCallback callback=new StatementCallback(){  
    4.       public Object=doWithStatement(Statement stmt){  
    5.              return ...;  
    6.       }  
    7. }  
    8.   
    9. jdbcTemplate.execute(callback);  
  • 相关阅读:
    对百度搜索引擎的评论
    团队开发个人总结05
    Bootsrap下拉菜单实现Hover下拉效果
    C#抽奖优惠券生成唯一码方法
    js.live方法无效, 报错:uncaught TypeError: $(...).live is not a function
    SQL 插入一条自定义主键值的数据
    一款很简单的直接发送邮件功能
    SQL生成指定范围内随机值
    SQL(replace)替换字段中指定的字符
    SQL表中删除一列
  • 原文地址:https://www.cnblogs.com/chenying99/p/2698657.html
Copyright © 2011-2022 走看看