JobRepositoryFactoryBean错误春季批处理

时间:2018-12-14 11:34:34

标签: spring spring-batch

我开始学习春季批处理,但遇到一个问题,当我想 使用JobRepositoryFactoryBean将作业的状态保存在数据库中。 编译器显示:

原因:org.springframework.beans.factory.BeanCreationException:创建在类路径资源[springConfig.xml]中定义的名称为'jobRepository'的bean时出错:初始化方法的调用失败;嵌套异常为java。 lang.NoClassDefFoundError:org / springframework / jdbc / core / simple / ParameterizedRowMapper

但当我使用MapJobRepositoryFactoryBean

时不会出错

我正在使用Spring 5

springconfig.xml

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


    <context:component-scan base-package="springbatch" />
    <context:annotation-config />



    <bean id="personneReaderCSV" class="org.springframework.batch.item.file.FlatFileItemReader">
        <property name="resource" value="input/personnes.txt" />
        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <property name="lineTokenizer">
                    <bean
                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="delimiter" value="," />
                        <property name="names" value="id,nom,prenom,civilite" />
                    </bean>
                </property>
                <property name="fieldSetMapper">
                    <bean
                        class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                        <property name="targetType" value="springbatch.entities.Personne" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>

    <bean id="jobLauncher"
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />

    </bean>

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

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

    <bean name="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring_test" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>springbatch.entities.Personne</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>




    <job id="importPersonnes" xmlns="http://www.springframework.org/schema/batch">
        <step id="readWritePersonne">
            <tasklet>
                <chunk reader="personneReaderCSV" 
                processor="personProcessor"
                    writer="personWriter" 
                    commit-interval="2" />
            </tasklet>
        </step>
    </job>

    <bean id="daoPersonne" class="springbatch.dao.PersonneDaoImp">
        <property name="factory" ref="sessionFactory"></property>
    </bean>

    <bean id="personWriter" class="springbatch.batch.PersonneWriter">
            <property name="dao" ref="daoPersonne"></property>
    </bean>

    <bean id="personProcessor" class="springbatch.batch.PersonneProcess">
    </bean>

    <bean id="batchLauncher" class="springbatch.MyBean">
    </bean>


    <bean id="jobRepository"
        class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseType" value="Mysql" />

    </bean>


    <task:scheduled-tasks>
        <task:scheduled ref="batchLauncher" method="message"
            cron=" 59 * * * * * " />
    </task:scheduled-tasks>

    <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>


</beans>

但使用时不会出错:

<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    <property name="transactionManager" ref="transactionManager" /> 
</bean>

1 个答案:

答案 0 :(得分:0)

您正在将Spring Batch v2.2与Spring Framework 5一起使用。由于ParameterizedRowMapper在Spring Framework 4.2+中已被删除,因此无法正常工作(因此除外)。

我建议您使用Spring Batch v4.1(因为不再维护v2.x),并且您的问题已得到解决。

管理Spring依赖项的最好方法是让Spring Boot通过start.spring.io或使用Spring Boot BOM生成项目来为您完成。通过这两种方式,您将拥有正确的Spring项目依赖关系,这些依赖关系可以很好地协同工作。