構造函數:
public JdbcTemplate(DataSource dataSource) { //帶數據源的構造函數
setDataSource(dataSource); //設置構造函數
afterPropertiesSet(); //在屬性設置好後的處理
}
public void setDataSource(DataSource dataSource) { //設置數據源
this.dataSource = dataSource;
}
//afterPropertiesSet爲JdbcAccessor中的函數
public void afterPropertiesSet() { //在屬性設置好後的處理
if (getDataSource() == null) { //數據源爲空則拋出llegalArgumentException異常
throw new IllegalArgumentException("Property 'dataSource' is required");
}
if (!isLazyInit()) { //如果不是延遲加載的 取得轉換異常
getExceptionTranslator();
}
}
//取得連接
public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {
try {
return doGetConnection(dataSource);
}
catch (SQLException ex) {
throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
}
}
二、源代碼的分析
三、實例
DataSourceUtils分析
TransactionSynchronizationManager分析
ConnectionHolder分析