无法将’org.springframework.batch.item.xml.StaxEventItemWriter’类型的值转换为必需的类型’org.springframework.batch.item.ItemReader’

我正在开发Spring Batch MongoDB to XML示例。 我已经成功创建了这个项目,但是当我运行它时,我看到下面的错误即将来临,我不知道这里出了什么问题。 参考错误

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step1': Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.batch.item.xml.StaxEventItemWriter' to required type 'org.springframework.batch.item.ItemReader' for property 'itemReader'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'org.springframework.batch.item.xml.StaxEventItemWriter' to required type 'org.springframework.batch.item.ItemReader' for property 'itemReader': no matching editors or conversion strategy found at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:563) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:740) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93) at com.mkyong.App.main(App.java:15) Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.batch.item.xml.StaxEventItemWriter' to required type 'org.springframework.batch.item.ItemReader' for property 'itemReader'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'org.springframework.batch.item.xml.StaxEventItemWriter' to required type 'org.springframework.batch.item.ItemReader' for property 'itemReader': no matching editors or conversion strategy found at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:591) at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603) at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1538) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1497) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1237) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:552) ... 11 more Caused by: java.lang.IllegalStateException: Cannot convert value of type 'org.springframework.batch.item.xml.StaxEventItemWriter' to required type 'org.springframework.batch.item.ItemReader' for property 'itemReader': no matching editors or conversion strategy found at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:306) at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576) ... 17 more 

Report.java

 public class Report { private int id; private Date date; private long impression; private int clicks; private BigDecimal earning; public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public long getImpression() { return impression; } public void setImpression(long impression) { this.impression = impression; } public int getClicks() { return clicks; } public void setClicks(int clicks) { this.clicks = clicks; } public BigDecimal getEarning() { return earning; } public void setEarning(BigDecimal earning) { this.earning = earning; } } 

的context.xml

           

database.xml

         

作业report.xml将

                      com.mkyong.Report    <!--             -->   

App.java

 public class App { public static void main(String[] args) { String[] springConfig = { "database.xml", "context.xml", "job-report.xml" }; ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("reportJob"); try { JobExecution execution = jobLauncher.run(job, new JobParameters()); System.out.println("Exit Status : " + execution.getStatus()); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); } } 

ReportConverter.java

 public class ReportConverter implements Converter { Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public boolean canConvert(Class type) { //we only need "Report" object return type.equals(Report.class); } @Override public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { Report report = (Report) source; writer.setValue(String.valueOf(report.getId())); Date date = report.getDate(); writer.setValue(formatter.format(date)); writer.setValue(String.valueOf(report.getClicks())); BigDecimal earning = report.getEarning(); writer.setValue(String.valueOf(earning)); writer.setValue(Long.toString(report.getImpression())); } @Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { Report obj = new Report(); //get attribute obj.setId(Integer.valueOf(reader.getAttribute("id"))); reader.moveDown(); //get date Date date = null; try { date = new SimpleDateFormat("MM/dd/yyyy").parse(reader.getValue()); } catch (ParseException e) { e.printStackTrace(); } obj.setDate(date); reader.moveUp(); reader.moveDown(); //get impression String impression = reader.getValue(); NumberFormat format = NumberFormat.getInstance(Locale.US); Number number = 0; try { number = format.parse(impression); } catch (ParseException e) { e.printStackTrace(); } obj.setImpression(number.longValue()); reader.moveUp(); reader.moveDown(); //get click obj.setClicks(Integer.valueOf(reader.getValue())); reader.moveUp(); reader.moveDown(); //get earning obj.setEarning(new BigDecimal(reader.getValue())); reader.moveUp(); return obj; } } 

收集报告:

 > db.report.find() { "_id" : 1, "_class" : "com.mkyong.Report", "date" : ISODate("2013-05-31T18:30:00Z"), "impression" : NumberLong(139237), "clicks" : 40, "earning" : "220.90" } { "_id" : 2, "_class" : "com.mkyong.Report", "date" : ISODate("2013-06-01T18:30:00Z"), "impression" : NumberLong(339100), "clicks" : 60, "earning" : "320.88" } { "_id" : 3, "_class" : "com.mkyong.Report", "date" : ISODate("2013-06-02T18:30:00Z"), "impression" : NumberLong(431436), "clicks" : 76, "earning" : "270.80" } { "_id" : 4, "_class" : "com.mkyong.Report", "date" : ISODate("2016-03-11T18:30:00Z"), "impression" : NumberLong(534987), "clicks" : 43, "earning" : "454.80" } > 

需要使用以下完成。 曾为作家提供读者和读者作家。