当前位置: 首页 > 面试题库 >

Spring Batch-MongoDB到XML-原因:java.lang.IllegalStateException:需要将输入转换为的类型

戚泰
2023-03-14
问题内容

我正在开发Spring Batch - MongoDB to XML示例。在此示例中,当我运行main方法时,我看到以下错误是cominng。请指导以下错误。我试图在网络上找到解决方案,但在网络上却找不到任何有用的方法。

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step1': Cannot resolve reference to bean 'mongodbItemReader' while setting bean property 'itemReader'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongodbItemReader' defined in class path resource [job-report.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: A type to convert the input into is required.
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1492)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1237)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:552)
    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.factory.BeanCreationException: Error creating bean with name 'mongodbItemReader' defined in class path resource [job-report.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: A type to convert the input into is required.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1589)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554)
    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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
    ... 15 more
Caused by: java.lang.IllegalStateException: A type to convert the input into is required.
    at org.springframework.util.Assert.state(Assert.java:392)
    at org.springframework.batch.item.data.MongoItemReader.afterPropertiesSet(MongoItemReader.java:200)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1648)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1585)
    ... 22 more

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>

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>

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="mongodbItemReader" writer="xmlItemWriter" 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>

@XmlRootElement(name = "record")
public class Report {

    private int id;
    private Date date;
    private long impression;
    private int clicks;
    private BigDecimal earning;

    @XmlAttribute(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @XmlElement(name = "date")
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @XmlElement(name = "impression")
    public long getImpression() {
        return impression;
    }

    public void setImpression(long impression) {
        this.impression = impression;
    }

    @XmlElement(name = "clicks")
    public int getClicks() {
        return clicks;
    }

    public void setClicks(int clicks) {
        this.clicks = clicks;
    }

    @XmlElement(name = "earning")
    public BigDecimal getEarning() {
        return earning;
    }

    public void setEarning(BigDecimal earning) {
        this.earning = earning;
    }
}

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

你能帮上忙吗?


问题答案:

谢谢@Sergio,它为您提供了很大的帮助,但可以进行以下配置

<bean id="mongodbItemReader" class="org.springframework.batch.item.data.MongoItemReader">
        <property name="template" ref="mongoTemplate" />
        <property name="collection" value="report" />
        <property name="targetType" value="com.mkyong.model.Report" />
        <property name="query" value="{'_id':{$gt:0} }" />
        <property name="sort">
            <util:map>
                 <entry key="id" value="#{T(org.springframework.data.domain.Sort.Direction).ASC}" /> 
            </util:map>
        </property>
    </bean>

我看到的唯一问题是,什么都没写到XML文件中。我将对此问题提出一个单独的问题。

注意: 此错误的主要原因是您需要<property name="targetType" value="com.mkyong.model.Report" />正确设置此属性。

您还可以参考:http : //www.mkyong.com/spring-batch/how-to-convert-
date-in-beanwrapperfieldsetmapper/
作为另一种解决方案。



 类似资料:
  • 问题内容: 我正在开发示例。在此示例中,当我运行main方法时,我看到以下错误是cominng。请指导以下错误。我试图在网络上找到解决方案,但在网络上却找不到任何有用的方法。 database.xml context.xml job-report.xml 库 App.java 你能帮上忙吗? 问题答案: 谢谢@Sergio,它为您提供了很大的帮助,但可以进行以下配置。 我看到的唯一问题是,什么都没

  • 我需要使用JOLT转换将下面的输入转换为精确的输出。输入: 输出: 我尝试使用以下规格,但没有驱动到我需要的精确输出。规格: 任何帮助都将不胜感激。谢谢

  • 问题内容: 我需要将Opencover Coverage结果导入Jenkins的能力,以便随着时间的推移提取Coverage趋势数据。似乎最好的方法是找到一种方法,将opencover xml格式转换为ncover格式,然后使用NCover插件导入结果。是否存在将xpress将opencover转换为ncover的现有xslt,或将进行转换的开源工具?我已经搜索过,却一无所获。 谢谢 问题答案:

  • 我已经在我的计算机上实现了一个数据结构,现在我正试图将它移植到我的android应用程序中。我打开一个原始的资源并获取一个,但我需要获取一个: 上面的代码引发以下异常,因为InputStream无法强制转换为FileInputStream,但这正是我需要的: 我所有的代码都是使用这个和一个FileInputStream构建的,所以我想继续使用它。从然后将其转换为? 在一些相关的帖子中,我无法为我的

  • 我有以下问题。我不是XSLT方面最伟大的专家,我想成功的是将一个XML从赛贝斯转换为以下内容: Sybase的输出: 我希望它的输出标题保持原样,条目部分不同 这是到目前为止我的XSLT(对不起,不太可能) 也许,有人可以帮我实现我想要的。非常感谢你。

  • 问题内容: 我正在尝试使用FOP和Java生成PDF文档。 我以字符串而不是文件的形式接收XML。 如何将该XML字符串转换为XML输入流,以便可以调用xslfoTransformer.transform(source,res); 其中source是我的XML字符串作为输入流。 请提供您的建议。 问题答案: 您可能希望将其转换为,而不是。使用StringReader执行此操作。StreamSour