views:

236

answers:

2

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  A: 

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>
matt b
I think the abstract definition approach is best. I'm not sure that AOP approach will work in this instance. It only works when the enhanced object is injected into another object and not for internal `this` method calls. Without extra work that is.
laz
A: 

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.

Spencer K