在容器外实例化 spring bean(用于测试)
我的 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你肯定有一个运行时依赖问题,正如@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 withcom.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 forSqlMapClientFactoryBean
to see whereASException
is called -- Spring should have a dist with all it's dependencies in it, you can also look in there when doing your investigation.这个类是在编译过程中找到的,但不是在运行过程中找到的:
因此,当您运行程序时,它似乎找不到属于您正在使用的Sun应用程序或门户服务器的这个类。简而言之:这是一个类路径错误。
This class was found during compilation but not during running:
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.