为什么SqlMapClientFactoryBean与SqlMapClient类型不同也可被注入
1个回答
展开全部
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。
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。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询