繁体   English   中英

使用FixedLengthTokenizer的FlatFileItemReader无法正常工作

[英]FlatFileItemReader using FixedLengthTokenizer not working

我在SpringBatch中做一个示例,使用带有FixedLengthTokenizer的FlatFileItemReader从文本文件读取并保存到xml文件。 我收到一个错误:Bean属性“ fixedLengthTokenizer”不可写或具有无效的setter方法。 setter的参数类型是否与getter的返回类型匹配? (我将放孔控制台日志)。 感谢您的任何帮助 。 配置文件:spring-batch-context.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" 
xsi:schemaLocation=" http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/batch 
http://www.springframework.org/schema/batch/spring-batch-3.0.xsd">
    <bean id="dataSource1"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
           <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
           <property name="url" value="********"></property>
           <property name="password" value="*****"></property>
           <property name="username" value="******"></property>
         </bean>  
         <bean id="dataSource2"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
           <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
           <property name="url" value="*******"></property>
           <property name="password" value="*****"></property>
           <property name="username" value="*****"></property>
         </bean>

     <bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"  scope="step">

        <property name="resource" value="txt/flatfile" />
        <property name="lineMapper">

            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <property name="fieldSetMapper">
                     <bean class="artifact1.StudentFieldSetMapper" />
                   </property>
                <property name="fixedLengthTokenizer">
                   <bean class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
                      <property name="names" value="fisrtName,id, phone,lastName ,streetName,age,city, date_of_birth,quantity,sin ,code"></property>
                      <property name="columns" value="1-4,6-9,10-13,15-18,19-22,23-26,27-31,33-40,45-47,48-53,55-55"></property>
                   </bean>
                </property> 
           </bean>
        </property>
    </bean>


 <bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">

        <property name="resource" value="file:xml/etudiant.xml" />

        <property name="rootTagName" value="Student" />

        <property name="marshaller">

            <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                <property name="classesToBeBound">
                    <list>
                        <value>artifact1.ExamResult</value>
                    </list>
                </property>
            </bean>

        </property>

    </bean>

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

    <batch:job id="examResultJob">
        <batch:step id="step105" >
            <batch:tasklet transaction-manager="transactionManager">
                <batch:chunk reader="flatFileItemReader" writer="xmlItemWriter"   commit-interval="2" />
            </batch:tasklet>
        </batch:step>

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

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

 </beans>

main.java:

package artifact1;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main1 
{
  public static void main (String[] args)
  {
    ApplicationContext context=new  ClassPathXmlApplicationContext("spring/batch/spring-batch-context.xml");
    JobLauncher jobLuncher= (JobLauncher) context.getBean("jobLauncher");
    Job job=(Job) context.getBean("examResultJob");

    try
    {

      JobExecution jobExecution=jobLuncher.run(job, new JobParameters());
      System.out.println("job completed  :"+jobExecution.getStatus());
    }
    catch (JobExecutionException e)
    {
      System.out.println("job failed :");
      e.printStackTrace();
    }

  }

}

Student.java POJO:

package artifact1;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="etud")
public class Student
{
  String fisrtName;
  Integer id;
  Integer phone;
  String lastName;
  String streetName;
  Integer age;
  String city;
  String date_of_birth;
  Integer quantity;
  Integer sin;
  Short code;
  @XmlElement(name= "prenom")
  public String getFisrtName()
  {
    return fisrtName;
  }
  public void setFisrtName(String fisrtName)
  {
    this.fisrtName = fisrtName;
  }
  @XmlElement(name= "idd")
  public Integer getId()
  {
    return id;
  }
  public void setId(Integer id)
  {
    this.id = id;
  }
  @XmlElement(name= "tel")
  public Integer getPhone()
  {
    return phone;
  }
  public void setPhone(Integer phone)
  {
    this.phone = phone;
  }
  @XmlElement(name= "nom")
  public String getLastName()
  {
    return lastName;
  }
  public void setLastName(String lastName)
  {
    this.lastName = lastName;
  }
  @XmlElement(name= "trig")
  public String getStreetName()
  {
    return streetName;
  }
  public void setStreetName(String streetName)
  {
    this.streetName = streetName;
  }
  @XmlElement(name= "age")
  public Integer getAge()
  {
    return age;
  }
  public void setAge(Integer age)
  {
    this.age = age;
  }
  @XmlElement(name= "ville")
  public String getCity()
  {
    return city;
  }
  public void setCity(String city)
  {
    this.city = city;
  }
  @XmlElement(name= "datenaiss")
  public String getDate_of_birth()
  {
    return date_of_birth;
  }
  public void setDate_of_birth(String date_of_birth)
  {
    this.date_of_birth = date_of_birth;
  }
  @XmlElement(name= "quant")
  public Integer getQuantity()
  {
    return quantity;
  }
  public void setQuantity(Integer quantity)
  {
    this.quantity = quantity;
  }
  @XmlElement(name= "sin")
  public Integer getSin()
  {
    return sin;
  }
  public void setSin(Integer sin)
  {
    this.sin = sin;
  }
  @XmlElement(name= "cd")
  public Short getCode()
  {
    return code;
  }
  public void setCode(Short code)
  {
    this.code = code;
  }
  @Override
  public String toString()
  {
    return "Student [fisrtName=" + fisrtName + ", id=" + id + ", phone=" + phone + ", lastName="
        + lastName + ", streetName=" + streetName + ", age=" + age + ", city=" + city
        + ", date_of_birth=" + date_of_birth + ", quantity=" + quantity + ", sin=" + sin + ", code="
        + code + "]";
  }


}

StudentFieldSetMapper:

package artifact1;

import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

public class StudentFieldSetMapper implements  FieldSetMapper<Student>
{

  public Student mapFieldSet(FieldSet fs) throws BindException
  {
    System.out.println("inside StudentFieldSetMapper");
    Student et=new Student();
    et.setFisrtName(fs.readString(0));
    et.setId(fs.readInt(1));
    et.setPhone(fs.readInt(2));
    et.setLastName(fs.readString(3));
    et.setStreetName(fs.readString(4));
    et.setAge(fs.readInt(5));
    et.setCity(fs.readString(6));
    et.setDate_of_birth(fs.readString(7));
    et.setQuantity(fs.readInt(8));
    et.setSin(fs.readInt(9));
    et.setCode(fs.readShort(10));
    return et;
  }

}

日志控制台:

2018-10-26 13:56:42 ERROR org.springframework.batch.core.step.AbstractStep:229 - Encountered an error executing step ste
p105 in job examResultJob
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.flatFileItemReader'
 defined in class path resource [spring/batch/spring-batch-context.xml]: Cannot create inner bean 'org.springframework.b
atch.item.file.mapping.DefaultLineMapper#184f6be2' of type [org.springframework.batch.item.file.mapping.DefaultLineMappe
r] while setting bean property 'lineMapper'; nested exception is org.springframework.beans.factory.BeanCreationException
: Error creating bean with name 'org.springframework.batch.item.file.mapping.DefaultLineMapper#184f6be2' defined in clas
s path resource [spring/batch/spring-batch-context.xml]: Error setting property values; nested exception is org.springfr
amework.beans.NotWritablePropertyException: Invalid property 'fixedLengthTokenizer' of bean class [org.springframework.b
atch.item.file.mapping.DefaultLineMapper]: Bean property 'fixedLengthTokenizer' is not writable or has an invalid setter
 method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.j
ava:313)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueRes
olver.java:122)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCap
ableBeanFactory.java:1531)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBea
nFactory.java:1276)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBea
nFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanF
actory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:345)
    at org.springframework.batch.core.scope.StepScope.get(StepScope.java:113)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192)
    at com.sun.proxy.$Proxy3.open(Unknown Source)
    at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)
    at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:197)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
    at artifact1.Main1.main(Main1.java:27)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.b
atch.item.file.mapping.DefaultLineMapper#184f6be2' defined in class path resource [spring/batch/spring-batch-context.xml
]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid pr
operty 'fixedLengthTokenizer' of bean class [org.springframework.batch.item.file.mapping.DefaultLineMapper]: Bean proper
ty 'fixedLengthTokenizer' is not writable or has an invalid setter method. Does the parameter type of the setter match t
he return type of the getter?
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCap
ableBeanFactory.java:1568)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBea
nFactory.java:1276)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBea
nFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanF
actory.java:483)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.j
ava:299)
    ... 26 more
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'fixedLengthTokenizer' of bean class
 [org.springframework.batch.item.file.mapping.DefaultLineMapper]: Bean property 'fixedLengthTokenizer' is not writable o
r has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.BeanWrapperImpl.createNotWritablePropertyException(BeanWrapperImpl.java:243)
    at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.jav
a:435)
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:29
0)
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:27
8)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCap
ableBeanFactory.java:1564)
    ... 30 more
2018-10-26 13:56:42 ERROR org.springframework.batch.core.step.AbstractStep:274 - Exception while closing step execution 
resources in step step105 in job examResultJob
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.flatFileItemReader'
 defined in class path resource [spring/batch/spring-batch-context.xml]: Cannot create inner bean 'org.springframework.b
atch.item.file.mapping.DefaultLineMapper#184f6be2' of type [org.springframework.batch.item.file.mapping.DefaultLineMappe
r] while setting bean property 'lineMapper'; nested exception is org.springframework.beans.factory.BeanCreationException
: Error creating bean with name 'org.springframework.batch.item.file.mapping.DefaultLineMapper#184f6be2' defined in clas
s path resource [spring/batch/spring-batch-context.xml]: Error setting property values; nested exception is org.springfr
amework.beans.NotWritablePropertyException: Invalid property 'fixedLengthTokenizer' of bean class [org.springframework.b
atch.item.file.mapping.DefaultLineMapper]: Bean property 'fixedLengthTokenizer' is not writable or has an invalid setter
 method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.j
ava:313)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueRes
olver.java:122)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCap
ableBeanFactory.java:1531)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBea
nFactory.java:1276)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBea
nFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanF
actory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:345)
    at org.springframework.batch.core.scope.StepScope.get(StepScope.java:113)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192)
    at com.sun.proxy.$Proxy3.close(Unknown Source)
    at org.springframework.batch.item.support.CompositeItemStream.close(CompositeItemStream.java:85)
    at org.springframework.batch.core.step.tasklet.TaskletStep.close(TaskletStep.java:305)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:271)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
    at artifact1.Main1.main(Main1.java:27)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.b
atch.item.file.mapping.DefaultLineMapper#184f6be2' defined in class path resource [spring/batch/spring-batch-context.xml
]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid pr
operty 'fixedLengthTokenizer' of bean class [org.springframework.batch.item.file.mapping.DefaultLineMapper]: Bean proper
ty 'fixedLengthTokenizer' is not writable or has an invalid setter method. Does the parameter type of the setter match t
he return type of the getter?
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCap
ableBeanFactory.java:1568)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBea
nFactory.java:1276)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBea
nFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanF
actory.java:483)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.j
ava:299)
    ... 26 more
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'fixedLengthTokenizer' of bean class
 [org.springframework.batch.item.file.mapping.DefaultLineMapper]: Bean property 'fixedLengthTokenizer' is not writable o
r has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.BeanWrapperImpl.createNotWritablePropertyException(BeanWrapperImpl.java:243)
    at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.jav
a:435)
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:29
0)
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:27
8)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCap
ableBeanFactory.java:1564)
    ... 30 more
job completed  :FAILED

在配置spring bach文件中:spring-batch-context.xml:我改变了这个:

<property name="fixedLengthTokenizer">
                   <bean class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
                      <property name="names" value="fisrtName,id, phone,lastName ,streetName,age,city, date_of_birth,quantity,sin ,code"></property>
                      <property name="columns" value="1-4,6-9,10-13,15-18,19-22,23-26,27-31,33-40,45-47,48-53,55-55"></property>
                   </bean>
                </property>

至 :

<property name="lineTokenizer">
                   <bean class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
                      <property name="names" value="fisrtName,id, phone,lastName ,streetName,age,city, date_of_birth,quantity,sin ,code"></property>
                      <property name="columns" value="1-4,6-9,10-13,15-18,19-22,23-26,27-31,33-40,45-47,48-53,55-55"></property>
                   </bean>
                </property>

工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM