如何从 spring xml 设置 SqlMapClientTemplate
我有以下java课程。
package com.org.data.dbresource;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
public class DBConnectionManager {
private SqlMapClientTemplate sqlMapClientTemplate;
public void setSqlMapClientTemplate (SqlMapClientTemplate sq)
{
this.sqlMapClientTemplate = sq;
}
public SqlMapClientTemplate getSqlMapClientTemplate ()
{
return this.sqlMapClientTemplate;
}
}
我的 Spring xml 如下所示:
<bean id="IbatisDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/RSRC/app/oltp"/>
</bean>
<bean id="MySqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sql-map.xml"/>
<property name="dataSource" ref="IbatisDataSource"/>
</bean>
<bean id="myObject" class="com.org.data.dbresource.DBConnectionManager">
<property name="sqlMapClientTemplate" ref="MySqlMapClient"/>
</bean>
我得到的错误是:
无法转换属性值 类型 [com.ibatis.sqlmap.engine.impl.SqlMapClientImpl] 至所需类型 [org.springframework.orm.ibatis.SqlMapClientTemplate] 对于属性“sqlMapClientTemplate”;
如果我传递的是 SqlMapClient
而不是 SqlMapClientTemplate
,那么一切都会正常工作,但随后我必须显式捕获 SQLExceptions
我应该更改什么?
I've got following java class.
package com.org.data.dbresource;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
public class DBConnectionManager {
private SqlMapClientTemplate sqlMapClientTemplate;
public void setSqlMapClientTemplate (SqlMapClientTemplate sq)
{
this.sqlMapClientTemplate = sq;
}
public SqlMapClientTemplate getSqlMapClientTemplate ()
{
return this.sqlMapClientTemplate;
}
}
My Spring xml looks like following:
<bean id="IbatisDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/RSRC/app/oltp"/>
</bean>
<bean id="MySqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sql-map.xml"/>
<property name="dataSource" ref="IbatisDataSource"/>
</bean>
<bean id="myObject" class="com.org.data.dbresource.DBConnectionManager">
<property name="sqlMapClientTemplate" ref="MySqlMapClient"/>
</bean>
Error I get is:
Failed to convert property value of
type
[com.ibatis.sqlmap.engine.impl.SqlMapClientImpl]
to required type
[org.springframework.orm.ibatis.SqlMapClientTemplate]
for property 'sqlMapClientTemplate';
Everything works fine if, instead of SqlMapClientTemplate
I pass SqlMapClient
but then I have to explicitly catch SQLExceptions
What should I change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误说明了一切 - 您正在尝试将
SqlMapClient
类型的对象(由SqlMapClientFactoryBean
创建)注入到SqlMapClientTemplate
类型的属性中。您需要自己手动实例化
SqlMapClientTemplate
,无论是在DBConnectionManager
内,例如,然后
记住,
SqlMapClientTemplate
只不过是一个帮助器类。 Spring和iBatis都没有强制要求使用它,如果你想使用它,你需要自己实例化它。The error says it all - you're trying to inject an object of type
SqlMapClient
(as created bySqlMapClientFactoryBean
) into a property of typeSqlMapClientTemplate
.You need to manually instantiate the
SqlMapClientTemplate
yourself, either insideDBConnectionManager
, e.g.and then
Remember,
SqlMapClientTemplate
isw nothing more than a helper class. Neither Spring nor iBatis mandates its use, and if you want to use it, you need to instantiate it yourself.