使用Spring Batch将csv文件的数据写入数据库时​​出错

时间:2014-10-14 08:41:31

标签: spring-batch

我正在使用Spring Batch将csv文件的数据写入mysql数据库。

但是在运行我的主java文件的initialize-database文件中出现关于弹簧批的database.xml组件的错误。下面给出了databse.xml文件代码和主类代码。这个例子我是从mkyong.com做的

运行主文件时会出现以下错误。

Exception in thread "main"  
    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Cannot locate BeanDefinitionDecorator for element [initialize-database]
        Offending resource: class path resource [spring/batch/config/database.xml]
            at org.springframework.beans.factory.parsing.FailFastProblemReporter.fatal(FailFastProblemReporter.java:59)
            at org.springframework.beans.factory.parsing.ReaderContext.fatal(ReaderContext.java:68)
            at org.springframework.beans.factory.parsing.ReaderContext.fatal(ReaderContext.java:55)
            at org.springframework.beans.factory.xml.NamespaceHandlerSupport.findDecoratorForNode(NamespaceHandlerSupport.java:119)
            at org.springframework.beans.factory.xml.NamespaceHandlerSupport.decorate(NamespaceHandlerSupport.java:97)
            at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateIfRequired(BeanDefinitionParserDelegate.java:1475)
            at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateBeanDefinitionIfRequired(BeanDefinitionParserDelegate.java:1462)
            at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateBeanDefinitionIfRequired(BeanDefinitionParserDelegate.java:1442)
            at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.processBeanDefinition(DefaultBeanDefinitionDocumentReader.java:314)
            at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:203)
            at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:182)
            at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:139)
            at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:108)
            at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
            at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
            at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
            at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
            at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
            at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
            at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
            at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
            at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
            at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
            at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
            at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
            at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
            at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
            at com.mkyong.App.main(App.java:20)


      This example i am writting from mkyong.com
      but it is giving the above error which is i am not understanding.

主java文件是下面给出的App.java

package com.mkyong;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {  
    public static void main(String args[])
    {
        String [] springConfig=
            {"spring/batch/config/context.xml",
             "spring/batch/config/database.xml",
             "spring/batch/jobs/job-report.xml"
            };

        ApplicationContext context=new ClassPathXmlApplicationContext(springConfig);

        JobLauncher jobLauncher=(JobLauncher)context.getBean("jobLauncher");
        Job job=(Job)context.getBean("job");

        try{
            JobExecution execution=jobLauncher.run(job, new JobParameters());
            System.out.println("Exit Status:" + execution.getStatus());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        System.out.println("Done");
    }

}

Database的文件是database.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">

        <bean id="dataSource" 
                class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
                <property name="username" value="root"></property>
                <property name="password" value="1234"></property>
        </bean>
       <bean id="transactionManager"                   class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">

                         <jdbc:initialize-database data-source="dataSource">
                        <jdbc:script location="org/springframework/batch/core/schema-drop- 
                               mysql.sql"/>
                        <jdbc:script location="org/springframework/batch/core/schema-mysql.sql" />
                    </jdbc:initialize-database>
       </bean>     
</beans>

1 个答案:

答案 0 :(得分:0)

我认为你有一个错字,试试这个:

<bean id="transactionManager"
        class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

    <!-- create job-meta tables automatically -->
    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" />
        <jdbc:script location="org/springframework/batch/core/schema-mysql.sql" />
    </jdbc:initialize-database>

jdbc:initialize不应该在transactionManager内。