在容器外实例化 spring bean(用于测试)

发布于 2024-08-11 14:45:45 字数 2615 浏览 3 评论 0原文

我的 applicaionContext.xml 中有以下内容

<bean id="IbatisDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@123.210.85.56:1522:ORCL"/>
    <property name="username" value="mydb"/>
    <property name="password" value="mydbpwd"/>
</bean>


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

,然后在我的代码中:

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SqlMapClient sqlclient = (SqlMapClient) ctx.getBean("myMapClient");

这样做会出现以下错误:

使用名称创建 bean 时出错 类中定义的“myMapClient” 路径资源 [applicationContext.xml]:调用 init方法失败;嵌套的 例外是 java.lang.NoClassDefFoundError: com/iplanet/ias/admin/common/ASException

我不明白为什么它要寻找该类?我正在尝试在容器之外做所有事情。所以它甚至不应该寻找那个类......但尽管如此,为了让它工作,我尝试寻找名为 ASException 的类,这样我就可以将它放在类路径上,但我在哪里可以找到 ASException 类。

有什么指点吗?

堆栈跟踪和我的编译测试/运行测试库的图像 替代文本替代文本替代文本

编辑 解决方案: 尽管我认为所有东西都在容器之外...但有一样东西不在容器之外。
请注意属性 configLocation:

<property name="configLocation" value="classpath:sql-map-config-oracle.xml"/>

sql-map-config-oracle.xml 的实际内容是

<sqlMapConfig>
   <settings enhancementEnabled="true" useStatementNamespaces="true" />
    <transactionManager type="JDBC">
        <dataSource type="JNDI">
            <property name="DataSource" value="my/jndi/mydb" />
        </dataSource>
    </transactionManager>
<sqlMap resource="somemapping.xml"/>
</sqlMapConfig>

JNDI 内容,不需要在那里!

sql-map-config-oracle.xml 应该是:

<sqlMapConfig>
   <settings enhancementEnabled="true" useStatementNamespaces="true" />
        <sqlMap resource="somemapping.xml"/>
</sqlMapConfig>

I have following in my applicaionContext.xml

<bean id="IbatisDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@123.210.85.56:1522:ORCL"/>
    <property name="username" value="mydb"/>
    <property name="password" value="mydbpwd"/>
</bean>


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

then in my code I have:

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SqlMapClient sqlclient = (SqlMapClient) ctx.getBean("myMapClient");

doing this gives me the following error:

Error creating bean with name
'myMapClient' defined in class
path resource
[applicationContext.xml]: Invocation
of init method failed; nested
exception is
java.lang.NoClassDefFoundError:
com/iplanet/ias/admin/common/ASException

I don't understand why is it looking for that class? I am trying to do everything outside the container. So it should not even be looking for that class...but nonetheless just to make it work I tried looking for class called ASException so I could put it on the classpath but no where can I find ASException class.

Any pointers?

Images of stack trace and my compile test / run test libs
alt text

alt text

alt text

Edit
Solution:
Even though I thought everything was outside the container...there was ONE thing that was not outside the container.
Notice the property configLocation:

<property name="configLocation" value="classpath:sql-map-config-oracle.xml"/>

actual content of sql-map-config-oracle.xml is

<sqlMapConfig>
   <settings enhancementEnabled="true" useStatementNamespaces="true" />
    <transactionManager type="JDBC">
        <dataSource type="JNDI">
            <property name="DataSource" value="my/jndi/mydb" />
        </dataSource>
    </transactionManager>
<sqlMap resource="somemapping.xml"/>
</sqlMapConfig>

JNDI stuff does not need to be there!

sql-map-config-oracle.xml should simply be:

<sqlMapConfig>
   <settings enhancementEnabled="true" useStatementNamespaces="true" />
        <sqlMap resource="somemapping.xml"/>
</sqlMapConfig>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

空袭的梦i 2024-08-18 14:45:45

你肯定有一个运行时依赖问题,正如@Cletus所说,org.springframework.orm.ibatis.SqlMapClientFactoryBean是用com.iplanet.ias.admin.common.ASException编译的,但现在你的类路径中没有它——Spring找不到它。您应该查看 SqlMapClientFactoryBean 的源代码以查看 ASException 被调用的地方 - Spring 应该有一个包含所有依赖项的 dist,您也可以在执行时查看那里你的调查。

You definitely have a runtime dependency issue as @Cletus said org.springframework.orm.ibatis.SqlMapClientFactoryBean was compiled with com.iplanet.ias.admin.common.ASException but now you don't have it in your classpath -- Spring can't find it. You should Look at the source for SqlMapClientFactoryBean to see where ASException is called -- Spring should have a dist with all it's dependencies in it, you can also look in there when doing your investigation.

香草可樂 2024-08-18 14:45:45

这个类是在编译过程中找到的,但不是在运行过程中找到的:

com/iplanet/ias/admin/common/ASException

因此,当您运行程序时,它似乎找不到属于您正在使用的Sun应用程序或门户服务器的这个类。简而言之:这是一个类路径错误。

This class was found during compilation but not during running:

com/iplanet/ias/admin/common/ASException

So when you're running the program, it can't seem to find this class, which belongs to the Sun app or portal server that you're using. In short: it's a classpath error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文