【问题标题】:<jdbc:embedded-database> in context.xml does not work. Want to to start an sql script for unit testingcontext.xml 中的 <jdbc:embedded-database> 不起作用。想要启动一个用于单元测试的 sql 脚本
【发布时间】:2012-06-24 01:18:10
【问题描述】:

这是我的上下文文件。:

<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"> <!--Line 11-->

<tx:annotation-driven transaction-manager="transactionManager"/>



<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <!---->
    <property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver"/>
    <property name="url" value="jdbc:hsqldb:mem:mydb"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="hibernate.cfg.xml.incDTD"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.connection.shutdown">true</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
 </bean>

<jdbc:embedded-database id="embedded" type="HSQL"/>
<jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="classpath:ctl_data-scrubd.sql"/>
</jdbc:initialize-database>

我收到此错误

Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML document from class path resource [PersistenceHelper-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 64; <Line 11, Column 64>: XML-24500: (Error) Can not build schema 'http://www.springframework.org/schema/jdbc' located at 'http://www.springframework.org/schema/jdbc/spring-jdbc.xsd'

我想启动一个 sql 脚本进行测试。我需要从这个 xml 文件中启动它。我读过使用它不起作用。有谁知道我如何解决这个问题,或者从内部启动 .sql 文件的另一种方法。如果您想查看我的更多代码,请告诉我。

编辑::

我在另一个网站上发布了同样的问题,我得到了一些建议。现在的错误是:

at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean   with name 'org.springframework.jdbc.datasource.init.DataSourceInitializer#0': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Failed to execute database script; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource class path resource []: bin
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean

这也是我的配置文件。我所做的只是添加一个版本并移动一些声明:

<?xml version="1.0" encoding="UTF-8"?>
<!-This is the spring configuration file for test cases.-->
<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:jdbc="http://www.springframework.org/schema/jdbc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">

<tx:annotation-driven transaction-manager="transactionManager"/>

其他的都一样。

【问题讨论】:

    标签: java spring hibernate junit


    【解决方案1】:

    只是为了完成答案,这就是适合我的上下文(Spring 3.2.1):

    <beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:jdbc="http://www.springframework.org/schema/jdbc"
                xsi:schemaLocation="http://www.springframework.org/schema/beans
                                                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                                                        http://www.springframework.org/schema/jdbc
                                                        http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
        <jdbc:embedded-database id="dataSource" type="HSQL">
            <jdbc:script location="classpath:schema.sql" />
            <jdbc:script location="classpath:data.sql" />
        </jdbc:embedded-database>
    </beans>
    
    【解决方案2】:

    通过将定义 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 移到顶部并在定义中添加版本号“spring-jdbc-3.0.xsd”来解决此问题。

    【讨论】:

      【解决方案3】:

      你能确认你的类路径中有 spring-jdbc jar

      【讨论】:

      • 是的,我可以。我正在使用 intelliJ,我在我的 lib 文件夹中看到它。
      • 哦,好吧,所以你错过了 &lt;?xml version="1.0" encoding="UTF-8"?&gt; ,我以为你有它,你正在显示一个没有顶部的 sn-p。关于您的新问题-我认为这是因为您的 sql 脚本文件的内容有问题。我看到的另一个问题是您已将嵌入式数据源命名为“嵌入式”,但您的初始化程序正在针对名为 datasource 的数据源运行,这是否正确,理想情况下您希望单独针对嵌入式数据源运行脚本对吗?跨度>
      • 我非常怀疑是我的sql脚本,因为在我开始将spring框架引入我们的程序之前,它已经运行了很多次。你能解释一下你所说的第二部分是什么意思吗?我的 dataSource 保存了 Hibernate 数据库的适当值来创建它(“我想在这里使用 schema 一词,但我不完全确定它是否是这样的”)。 sql 脚本具有要插入到这些表中的值。理想情况下,我想创建表,然后对表运行脚本以填充它,如果这有意义的话。
      猜你喜欢
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      相关资源
      最近更新 更多