gpt4 book ai didi

java - 无法将类型 'org.springframework.batch.item.xml.StaxEventItemWriter' 的值转换为所需类型 'org.springframework.batch.item.ItemReader'

转载 作者:太空宇宙 更新时间:2023-11-04 11:54:14 26 4
gpt4 key购买 nike

我正在开发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.<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>

数据库.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" }
>

最佳答案

需要使用下面的方法完成。已将作家交给读者,将读者交给作家。

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

关于java - 无法将类型 'org.springframework.batch.item.xml.StaxEventItemWriter' 的值转换为所需类型 'org.springframework.batch.item.ItemReader',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41500945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com