views:

334

answers:

2

I have the following in my xml configurations. I would like to convert these to my code because I am doing some unit/integration testing outside of the container.

xmls:

<bean id="MyMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation" value="classpath:sql-map-config-oracle.xml"/>
  <property name="dataSource" ref="IbatisDataSourceOracle"/>
 </bean>

<bean id="IbatisDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/my/mydb"/>
</bean>

code I used to fetch stuff from above xmls:

this.setSqlMapClient((SqlMapClient)ApplicationInitializer.getApplicationContext().getBean("MyMapClient"));

my code (for unit testing purposes):

SqlMapClientFactoryBean bean = new SqlMapClientFactoryBean();
UrlResource urlrc = new UrlResource("file:/data/config.xml");
bean.setConfigLocation(urlrc);
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@123.210.85.56:1522:ORCL");
dataSource.setUsername("dbo_mine");
dataSource.setPassword("dbo_mypwd");
bean.setDataSource(dataSource);

SqlMapClient sql = (SqlMapClient) bean; //code fails here

when the xml's are used then SqlMapClient is the class that sets up then how come I cant convert SqlMapClientFactoryBean to SqlMapClient

+1  A: 

SqlMapClientFactoryBean is a FactoryBean. It does not implement the SqlMapClient interface itself, but manufactures instances of SqlMapClient which are returned when it's getObject() method is called. The Spring container knows about FactoryBeans, and makes them look just like normal beans from the perspective of the caller. I'm not sure that using the FactoryBean outside a container will work - you may get a "not initialized" exception if you call getObject() outside of the container lifecycle ...

Why not create a separate, stripped down Spring config for your test cases, instantiate that, and obtain the beans from there? Alternatively, you could create the SqlMapClient the non-Spring way and set that on your DAO

alasdairg
what you mentioned in the second paragraph can you show me how to do that? or a link? I am find with making separate xmls' for testing purposes but what should go in there? in the end I have to be able to pass SqlMapClient to setSqlMapClient(..) because my DAO is using getSqlMapClientTemplate()..
Omnipresent
how would I create SqlMapClient the non-spring way? I thought thats what I was trying to do here by leaving out xml's. ..care to show some code?
Omnipresent
A: 
SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean();
factory.setConfigLocation(YOUR_SQL_MAP_CONFIG_RESOURCE);
factory.afterPropertiesSet(); //omitting try/catch code
client = (SqlMapClient)factory.getObject();

voila

darthtrevino