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);  
  • 相关阅读:
    HDU 2236 无题Ⅱ
    Golden Tiger Claw(二分图)
    HDU 5969 最大的位或 (思维,贪心)
    HDU 3686 Traffic Real Time Query System (图论)
    SCOI 2016 萌萌哒
    Spring Boot支持控制台Banner定制
    构建第一个Spring Boot程序
    Spring Boot重要模块
    Java fastjson JSON和String互相转换
    BCompare 4 Windows激活方法【试用期30天重置】
  • 原文地址:https://www.cnblogs.com/chenying99/p/2674350.html
Copyright © 2011-2022 走看看