如何在 spring xml 之外设置 SqlMapClient

发布于 2024-08-11 00:00:11 字数 1370 浏览 3 评论 0原文

我的 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 是设置的类,那么我为什么无法转换SqlMapClientFactoryBeanSqlMapClient

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌辣 2024-08-18 00:00:11

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

老子叫无熙 2024-08-18 00:00:11
SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean();
factory.setConfigLocation(YOUR_SQL_MAP_CONFIG_RESOURCE);
factory.afterPropertiesSet(); //omitting try/catch code
client = (SqlMapClient)factory.getObject();

SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean();
factory.setConfigLocation(YOUR_SQL_MAP_CONFIG_RESOURCE);
factory.afterPropertiesSet(); //omitting try/catch code
client = (SqlMapClient)factory.getObject();

voila

薄荷港 2024-08-18 00:00:11

我想添加对我有用的内容。必须使用一些遗留代码,在我可以转换到 MyBatis 之前,我想将旧的 applicationContext xml 转换为 spring @Configuration 类。

@Bean
public SqlMapClient sqlMap() throws Exception
{

    SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean();
    factory.setConfigLocation(new ClassPathResource("conf/ibatis.xml"));
    DataSource dataSource                   = this.dataSource;
    factory.setDataSource(dataSource);
    factory.afterPropertiesSet();
    return (SqlMapClient) factory.getObject();
}

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.

@Bean
public SqlMapClient sqlMap() throws Exception
{

    SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean();
    factory.setConfigLocation(new ClassPathResource("conf/ibatis.xml"));
    DataSource dataSource                   = this.dataSource;
    factory.setDataSource(dataSource);
    factory.afterPropertiesSet();
    return (SqlMapClient) factory.getObject();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文