无法将'org.springframework.batch.item.xml.StaxEventItemWriter'类型的值转换为必需的类型'org.springframework.batch.item.ItemReader' [英] Cannot convert value of type 'org.springframework.batch.item.xml.StaxEventItemWriter' to required type 'org.springframework.batch.item.ItemReader'

查看:139
本文介绍了无法将'org.springframework.batch.item.xml.StaxEventItemWriter'类型的值转换为必需的类型'org.springframework.batch.item.ItemReader'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing Spring Batch MongoDB to XML example. I've successfully created the project, but when I am running it I see the below error is coming, I don't know what is going wrong here. Error for reference

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>

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

收集报告:

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


推荐答案

需要使用以下内容。曾为作家的读者和读者提供作家。

Need to use the following done. Had give writer for reader and reader for writer.

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

这篇关于无法将'org.springframework.batch.item.xml.StaxEventItemWriter'类型的值转换为必需的类型'org.springframework.batch.item.ItemReader'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆