读Spring 2.5.6 源码学匿名类的使用

最近开始重新学习spring。在研究spring开发包自带的例子jpetstore时,遇到了匿名类的使用,感觉新鲜,于是择其一二处,以供日后反刍。
在 org.springframework.orm.ibatis 包中,有一个类叫SqlMapClientTemplate,它是用spring整合iBatis对数据库进行操作的一个工具类。浏览一下,其主要提供了对数据库的各种“增查改删”的操作。

[b]增[/b]:


public Object insert(String statementName) throws DataAccessException {
return insert(statementName, null);
}

public Object insert(final String statementName, final Object parameterObject) throws DataAccessException {

return execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
return executor.insert(statementName, parameterObject);
}});
}


[b]查[/b]:

public List queryForList(String statementName) throws DataAccessException {
return queryForList(statementName, null);
}

public List queryForList(final String statementName, final Object parameterObject)
throws DataAccessException {

return executeWithListResult(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
return executor.queryForList(statementName, parameterObject);
}
});
}


[b]改[/b]:

public int update(String statementName) throws DataAccessException {
return update(statementName, null);
}

public int update(final String statementName, final Object parameterObject)
throws DataAccessException {

Integer result = (Integer) execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
return new Integer(executor.update(statementName, parameterObject));
}
});
return result.intValue();
}


[b]删[/b]:

public int delete(String statementName) throws DataAccessException {
return delete(statementName, null);
}

public int delete(final String statementName, final Object parameterObject)
throws DataAccessException {

Integer result = (Integer) execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
return new Integer(executor.delete(statementName, parameterObject));
}
});
return result.intValue();
}




从以上诸方法可以看出,在spring中使用了匿名类,使这一经典框架代码更加简洁美观。如果仅仅是代码的简洁美观,不能用,那就这样煞费苦心的设计就成了花拳绣腿了。我们可以注意到,以上所有的代码中所有的匿名类都实现了一个接口,那就是同一包(org.springframework.orm.ibatis)下的SqlMapClientCallback。这个接口只有一个方法,

Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException;

上述匿名类实现了这一接口,重写了上述方法。 之后工具类SqlMapClientTemplate利用

public Object execute(SqlMapClientCallback action) throws DataAccessException

调用了匿名类实现的接口方法[b]doInSqlMapClient[/b]。代码如下:

public Object execute(SqlMapClientCallback action) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
Assert.notNull(this.sqlMapClient, "No SqlMapClient specified");

// We always needs to use a SqlMapSession, as we need to pass a Spring-managed
// Connection (potentially transactional) in. This shouldn't be necessary if
// we run against a TransactionAwareDataSourceProxy underneath, but unfortunately
// we still need it to make iBATIS batch execution work properly: If iBATIS
// doesn't recognize an existing transaction, it automatically executes the
// batch for every single statement...

SqlMapSession session = this.sqlMapClient.openSession();
if (logger.isDebugEnabled()) {
logger.debug("Opened SqlMapSession [" + session + "] for iBATIS operation");
}
Connection ibatisCon = null;

try {
Connection springCon = null;
DataSource dataSource = getDataSource();
boolean transactionAware = (dataSource instanceof TransactionAwareDataSourceProxy);

// Obtain JDBC Connection to operate on...
try {
ibatisCon = session.getCurrentConnection();
if (ibatisCon == null) {
springCon = (transactionAware ?
dataSource.getConnection() : DataSourceUtils.doGetConnection(dataSource));
session.setUserConnection(springCon);
if (logger.isDebugEnabled()) {
logger.debug("Obtained JDBC Connection [" + springCon + "] for iBATIS operation");
}
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Reusing JDBC Connection [" + ibatisCon + "] for iBATIS operation");
}
}
}
catch (SQLException ex) {
throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
}

// Execute given callback...
try {
return action.doInSqlMapClient(session);
}
catch (SQLException ex) {
throw getExceptionTranslator().translate("SqlMapClient operation", null, ex);
}
finally {
try {
if (springCon != null) {
if (transactionAware) {
springCon.close();
}
else {
DataSourceUtils.doReleaseConnection(springCon, dataSource);
}
}
}
catch (Throwable ex) {
logger.debug("Could not close JDBC Connection", ex);
}
}

// Processing finished - potentially session still to be closed.
}
finally {
// Only close SqlMapSession if we know we've actually opened it
// at the present level.
if (ibatisCon == null) {
session.close();
}
}
}

[b]注意此段代码中46行return action.doInSqlMapClient(session);[/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值