Spring: replace SqlMapClientTemplate for all clients?

*爱你&永不变心* 提交于 2019-12-24 07:05:50

问题


I have a number of DAO classes that extend SqlMapClientDaoSupport, and call getSqlMapClientTemplate() to run iBatis queries.

For a particular test, I wish to replace the object returned when each DAO calls getSqlMapClientTemplate(), with my own custom class.

How can I do this?

I know that there is a setSqlMapClientTemplate( org.springframework.orm.ibatis.SqlMapClientTemplate ); however this presents two problems.

1) I wish the replacement to be "global" to my Spring configuration; I don't want to have to call set on each DAO.

2) That setter takes a SqlMapClientTemplate rather than the interface SqlMapClientTemplate implements (SqlMapClientOperations), so it looks as if I need to subclass SqlMapClientTemplate rather than just making my own implementation of the 'SqlMapClientOperation's interface.

How, for a particular Spring configuration, can I globally replace the SqlMapClientTemplate returned from all calls to getSqlMapClientTemplate()?

Thanks.


回答1:


Either use some sort of AOP or have all of the bean definitions in your context extend an abstract definition:

<bean id="baseDao" abstract="true">
    <property name="sqlMapClientTemplate" ref="yourNewClientTemplate"/>
</bean>

<bean id="specificDao" class="com.companyname.class" parent="baseDao" >
...
</bean>



回答2:


This item here goes over a similar question. I rewrote my DAOs to take SqlMapClientOperations as a parameter, which makes for easier and more straightforward testing, but you can use Mockito like in the link provided.



来源:https://stackoverflow.com/questions/1106218/spring-replace-sqlmapclienttemplate-for-all-clients

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!