开发者

how to set SqlMapClientTemplate from spring xml

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.SqlMapClientFactory开发者_开发百科Bean">
  <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?


The error says it all - you're trying to inject an object of type SqlMapClient (as created by SqlMapClientFactoryBean) into a property of type SqlMapClientTemplate.

You need to manually instantiate the SqlMapClientTemplate yourself, either inside DBConnectionManager, e.g.

private SqlMapClientTemplate sqlMapClientTemplate;

public void setSqlMapClient(SqlMapClient sqlMapClient)
{
    this.sqlMapClientTemplate = new SqlMapClientTemplate(sqlMapClient);
}   

and then

<bean id="myObject" class="com.org.data.dbresource.DBConnectionManager">
   <property name="sqlMapClient" ref="MySqlMapClient"/>
</bean>

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜