2017-01-06 14 views
0

Я разрабатываю Spring Batch MongoDB в XML примера. Я успешно создал проект, но когда я его запускаю, я вижу, что ошибка ниже, я не знаю, что здесь происходит. Ошибка для справкиНевозможно преобразовать значение типа «org.springframework.batch.item.xml.StaxEventItemWriter» для требуемого типа «org.springframework.batch.item.ItemReader»

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.<init>(ClassPathXmlApplicationContext.java:139) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(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

<beans xmlns="http://www.springframework.org/schema/beans" 
    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.xsd"> 

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

    <!-- stored job-meta in memory --> 
    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"> 
     <property name="transactionManager" ref="transactionManager" /> 
    </bean> 


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

database.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
    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.xsd 
     http://www.springframework.org/schema/data/mongo 
     http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> 

     <!-- connect to mongodb --> 
    <mongo:mongo host="127.0.0.1" port="27017" /> 
    <mongo:db-factory dbname="yourdb" /> 

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
     <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> 
    </bean> 

</beans> 

рабочих мест report.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 

    <batch:job id="reportJob"> 
     <batch:step id="step1"> 
      <batch:tasklet> 
       <batch:chunk reader="xmlItemWriter" writer="mongodbItemReader" commit-interval="1"> 
       </batch:chunk> 
      </batch:tasklet> 
     </batch:step> 
    </batch:job> 

    <bean id="mongodbItemReader" class="org.springframework.batch.item.data.MongoItemReader"> 
     <property name="template" ref="mongoTemplate" /> 
     <property name="collection" value="report" /> 
    </bean> 

    <bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter"> 
     <property name="resource" value="classpath:xml/report.xml" /> 
     <property name="marshaller" ref="reportMarshaller" /> 
     <property name="rootTagName" value="record" /> 
    </bean> 

    <!-- ==== Solution-1 ==== --> 
    <bean id="reportMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
     <property name="classesToBeBound"> 
      <value>com.mkyong.Report</value> 
     </property> 
    </bean> 

    <!-- ==== Solution-2 ==== --> 
    <!-- <bean id="reportMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"> 
     <property name="aliases"> 
      <util:map id="aliases"> 
       <entry key="record" value="com.mkyong.Report" /> 
      </util:map> 
     </property> 
     <property name="converters"> 
      <array> 
       <ref bean="reportConverter" /> 
      </array> 
     </property> 
    </bean> --> 

    <bean id="reportConverter" class="com.mkyong.ReportConverter" /> 
</beans> 

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

Collection Report:

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

ответ

0

Необходимо использовать следующее. Предложил писателю для читателя и читателя для писателя.

<batch:job id="reportJob"> 
     <batch:step id="step1"> 
      <batch:tasklet> 
       <!-- <batch:chunk reader="xmlItemWriter" writer="mongodbItemReader" commit-interval="1"> --> 
       <batch:chunk reader="mongodbItemReader" writer="xmlItemWriter" commit-interval="1"> 
       </batch:chunk> 
      </batch:tasklet> 
     </batch:step> 
    </batch:job>