Spring没有初始化bean(dbunit);(Spring not initializing bean(dbunit); what have I missed?)

编程入门 行业动态 更新时间:2024-10-28 21:15:35
Spring没有初始化bean(dbunit);(Spring not initializing bean(dbunit); what have I missed?) java

我在使用Spring集成设置Stripes MVC的教程中遵循一些指导原则,我正在尝试集成DBUnit以在启动时初始化我的数据库,因此我不必浪费时间每次手动插入数据。 不幸的是,我无法将该教程与付费电子书相关联。

在我的web.xml中,我引用了Spring

<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>

在我的applicationContext.xml中,我设置了以下bean

<bean id="dbUnitBootstrapper" class="com.jameselsey.salestracker.testing.DBUnitBootstrapper" init-method="execute"> <property name="enabled" value="true"/> <property name="operations"> <list> <bean class="org.dbunit.ant.Operation"> <property name="type" value="CLEAN_INSERT"/> <property name="src" value="classpath:testdata.xml"/> </bean> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/> <property name="url" value="jdbc:derby://localhost:1527/salestracker"/> <property name="username" value="admin"/> <property name="password" value="admin"/> </bean>

注意事项:

数据源与我的persistence.xml具有相同的细节,如果我手动插入数据,它会显示在我的应用程序中,因此连接细节应该没问题 我在我的DBUnitBootstrapper类中设置了断点,但这些断点从未被捕获,导致我相信这个bean没有被初始化。 testdata.xml文件存在于正确的位置,我有最简单的域对象,带有ID和一些String属性 在testdata.xml ,如果我将ID从1更改为abc我在控制台输出中得到一个numberFormatException ,所以听起来应用程序正在读取数据文件并尝试插入

我错过了一些明显的东西吗 我还可以做些什么? 我在JavaRanch上问了这个问题,但到目前为止我还没有得到很多帮助。 这是个人学习项目,所以取得一些进展会很棒:)

I'm following some guidelines in a tutorial for setting up Stripes MVC with Spring integration, and I'm trying to integrate DBUnit to initialise my DB on startup so I don't have to waste time manually inserting data each time. Unfortunately I can't link the tutorial as its from a paid for eBook.

In my web.xml I have referenced Spring

<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>

In my applicationContext.xml I have setup the following beans

<bean id="dbUnitBootstrapper" class="com.jameselsey.salestracker.testing.DBUnitBootstrapper" init-method="execute"> <property name="enabled" value="true"/> <property name="operations"> <list> <bean class="org.dbunit.ant.Operation"> <property name="type" value="CLEAN_INSERT"/> <property name="src" value="classpath:testdata.xml"/> </bean> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/> <property name="url" value="jdbc:derby://localhost:1527/salestracker"/> <property name="username" value="admin"/> <property name="password" value="admin"/> </bean>

Things to note:

The datasource has the same details as my persistence.xml, if I insert data manually it displays in my application, so the connection details should be fine I have set breakpoints in my DBUnitBootstrapper class, but these are never caught, leading me to believe this bean doesn't get initialised. the testdata.xml file exists in the correct place, I have the most simplest of domain objects with an ID and a few String attributes In the testdata.xml, if I change the ID from 1 to abc I get a numberFormatException in the console output, so it sounds like the application is reading the data file and trying to insert

Have I missed something obvious? What else can I do? I have asked this question on JavaRanch but I haven't been able to get much assistance so far. This is for a personal learning project so it would be great to make some progress :)

最满意答案

您似乎忘了在dbUnitBootstrapper设置dataSource属性

It seems to be you forgot to set a dataSource property in dbUnitBootstrapper

更多推荐

本文发布于:2023-04-27 11:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1326110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   bean   Spring   missed   initializing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!