<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list><!--<value>META-INF/jdbc.properties</value> --></list>
</property>
</bean>
<bean id="datasource" class="数据源实现类">
<property name="url" value="${jdbc.url}" />
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="minPoolSize" value="2" />
<property name="maxPoolSize" value="200" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="META-INF/sqlMapConfig.xml" />
<property name="dataSource" ref="datasource" />
</bean>
<!-- 抽象的数据库访问实现 -->
<bean abstract="true" id="abstractDao" class="com.jnkj.dao.AbstractDao">
<property name="sqlMapClient" ref="sqlMapClient" />
<property name="dataSource" ref="datasource" />
</bean>
<!-- DAO定义 继承了抽象公用的dao之后,不用每个dao都配置一个property的属性 -->
<bean id="deptDAO" parent="abstractDao" class="com.jnkj.dao.DeptDAOImpl" />
</beans>
分析:
当service层调用deptDAO后,会直接实例化abstractDao,但是abstractDao本身没有配置的属性,
所以得从父类找寻属性,找到abstractDao,有extends SqlMapClientDaoSupport,所以找到SqlMapClientDaoSupport
public abstract class SqlMapClientDaoSupport extends DaoSupport {
private SqlMapClientTemplate sqlMapClientTemplate = new SqlMapClientTemplate();
private boolean externalTemplate = false;
public final void setDataSource(DataSource dataSource) {
if (!this.externalTemplate) {
this.sqlMapClientTemplate.setDataSource(dataSource);
}
}
public final DataSource getDataSource() {
return this.sqlMapClientTemplate.getDataSource();
}
public final void setSqlMapClient(SqlMapClient sqlMapClient) {
if (!this.externalTemplate) {
this.sqlMapClientTemplate.setSqlMapClient(sqlMapClient);
}
}
public final SqlMapClient getSqlMapClient() {
return this.sqlMapClientTemplate.getSqlMapClient();
}
public final void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
Assert.notNull(sqlMapClientTemplate, "SqlMapClientTemplate must not be null");
this.sqlMapClientTemplate = sqlMapClientTemplate;
this.externalTemplate = true;
}
public final SqlMapClientTemplate getSqlMapClientTemplate() {
return this.sqlMapClientTemplate;
}
}
虽然没有相应属性,但是有对应的set,get方法,所以是可以完成初始化的。
对于setDataSource方法,是sqlMapClientTemplate 对象调用的,
又 private SqlMapClientTemplate sqlMapClientTemplate = new SqlMapClientTemplate();
我们可以看一下SqlMapClientTemplate 这个类:
public class SqlMapClientTemplate extends JdbcAccessor implements SqlMapClientOperations {
private SqlMapClient sqlMapClient;
public SqlMapClientTemplate() {
}..........................
可以看出继承了JdbcAccessor ,故需要一个jdbc的数据源
对于sqlMapClient属性的初始化,在SqlMapClientFactoryBean里可以体现
SqlMapClientFactoryBean部分代码如下:
public class SqlMapClientFactoryBean implements FactoryBean<SqlMapClient>, InitializingBean {
private static final ThreadLocal<LobHandler> configTimeLobHandlerHolder = new ThreadLocal<LobHandler>();
public static LobHandler getConfigTimeLobHandler() {
return configTimeLobHandlerHolder.get();
}
private Resource[] configLocations;
private Resource[] mappingLocations;
private Properties sqlMapClientProperties;
private DataSource dataSource;
private boolean useTransactionAwareDataSource = true;
private Class transactionConfigClass = ExternalTransactionConfig.class;
private Properties transactionConfigProperties;
private LobHandler lobHandler;
private SqlMapClient sqlMapClient;
public SqlMapClientFactoryBean() {
this.transactionConfigProperties = new Properties();
this.transactionConfigProperties.setProperty("SetAutoCommitAllowed", "false");
}
一大堆的属性,但是我们只需要配置dataSource,和configLocations
sqlMapConfig.xml的文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="false"
lazyLoadingEnabled="false" errorTracingEnabled="true" maxRequests="20"
maxSessions="20" maxTransactions="10" useStatementNamespaces="false"/>
<sqlMap resource="META-INF/sqlmap/Dept.xml"/>
</sqlMapConfig>