gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:09:44 25 4
gpt4 key购买 nike

我的 applicationContext.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");

这样做会出现以下错误:

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

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

有什么指点吗?

堆栈跟踪图像和我的编译测试/运行测试库 alt text alt text alt text

编辑解决方案:尽管我认为所有东西都在容器之外...但有一样东西不在容器之外。
注意属性 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>

最佳答案

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

关于java - 在容器外实例化 spring bean(用于测试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1716636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com