如何在 spring xml 之外设置 SqlMapClient
我的 xml 配置中有以下内容。我想将它们转换为我的代码,因为我正在容器外部进行一些单元/集成测试。
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>
我用来从上面获取内容的代码 xmls:
this.setSqlMapClient((SqlMapClient)ApplicationInitializer.getApplicationContext().getBean("MyMapClient"));
我的代码(用于单元测试目的):
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
当使用 xml 时,SqlMapClient
是设置的类,那么我为什么无法转换SqlMapClientFactoryBean
到 SqlMapClient
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SqlMapClientFactoryBean 是一个FactoryBean。它本身并不实现 SqlMapClient 接口,而是创建 SqlMapClient 的实例,这些实例在调用 getObject() 方法时返回。 Spring 容器了解 FactoryBeans,并让它们从调用者的角度来看就像普通的 bean 一样。我不确定在容器外部使用 FactoryBean 是否有效 - 如果您在容器生命周期之外调用 getObject() ,您可能会收到“未初始化”异常...
为什么不为您的容器创建一个单独的、精简的 Spring 配置测试用例,实例化它,并从那里获取bean?或者,您可以以非 Spring 方式创建 SqlMapClient 并将其设置在您的 DAO 上
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
瞧
voila
我想添加对我有用的内容。必须使用一些遗留代码,在我可以转换到 MyBatis 之前,我想将旧的 applicationContext xml 转换为 spring @Configuration 类。
I want to add what worked for me. Had to work with some legacy code and in till I can transition to MyBatis I want to convert the old applicationContext xml to spring @Configuration classes.