Spring未初始化bean(dbunit);我错过了什么?
我遵循教程中的一些准则来设置 Stripes MVC 与 Spring 集成,并且我尝试集成 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< /code>,如果我将 ID 从
1
更改为abc
,我会在控制台输出中收到numberFormatException
,所以听起来应用程序是读取数据文件并尝试插入
我是否错过了一些明显的东西?我还能做什么?我在 JavaRanch< 上提出了这个问题/a> 但到目前为止我还没有得到太多帮助。这是一个个人学习项目,因此取得一些进展会很棒:)
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 fewString
attributes - In the
testdata.xml
, if I change the ID from1
toabc
I get anumberFormatException
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您忘记在
dbUnitBootstrapper
中设置dataSource
属性It seems to be you forgot to set a
dataSource
property indbUnitBootstrapper