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

    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);  
  • 相关阅读:
    (转)PHP获取今天、昨天、明天的日期
    (转)META http-equiv="refresh" 实现网页自动跳转
    (转)PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数
    (转)Apache2 httpd.conf 配置详解 (二)
    Process finished with exit code 129
    Importing image to python :cannot import name 'imread'
    CUDA运行时错误 --- CUDA_ERROR_LAUNCH_FAILED: unspecified launch failure
    Recommendation system
    php手动实现ip2long和long2ip
    git将某个分支的代码完全覆盖另一个分支
  • 原文地址:https://www.cnblogs.com/chenying99/p/2674350.html
Copyright © 2011-2022 走看看