为什么SqlMapClientFactoryBean与SqlMapClient类型不同也可被注入_百度...
发布网友
发布时间:2024-01-09 19:55
我来回答
共1个回答
热心网友
时间:2024-10-27 22:33
spring3.0注入sqlMapClient的几种方式:
bean定义:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cms" />
<property name="username" value="root" />
<property name="password" value="19860619" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:/context/ibatis/sqlMapConfig.xml
</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
a.在context中装载sqlMapClient并建立sqlMapClientfactory,每次执行sql操作都从factory中获取sqlMapClient.factory获取bean方法:
WebApplicationContext wac = ContextLoader
.getCurrentWebApplicationContext();
SqlMapClient sqlMapClient = wac.getBean("sqlMapClient",
SqlMapClient.class);
b.在每个DAO中使用spring注解注入sqlMapClient:
@Autowired
@Qualifier("sqlMapClient")
private SqlMapClient sqlMapClient;
c.创建BaseDao继承SqlMapClientDaoSupport,所有DAO都继承BaseDao.BaseDao中注入sqlMapClient:
@Autowired
@Qualifier("sqlMapClient")
public void setSqlMapClientForAutowired(SqlMapClient sqlMapClient) {
super.setSqlMapClient(sqlMapClient);
}
该方法实际上是使用了sqlMapclientTemplate并向template注入sqlMapClient。