IllegalStateException:无法将type的值转换为必需的类型

时间:2014-03-13 19:42:36

标签: java spring spring-batch

这是我收到的错误。

       Caused by: java.lang.IllegalStateException: Cannot convert value of type [code.ProductFieldSetMapper] to required type [org.springframework.batch.item.file.mapping.FieldSetMapper] for property 'FieldSetMapper': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:264)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:450)
... 23 more

这是我的上下文文件(FileReaderConfig.xml)

        <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:batch="http://www.springframework.org/schema/batch"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/batch
    http://www.springframework.org/schema/batch/spring-batch.xsd">

<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
    <property name="resource" value="file:./output.txt" />
    <property name="linesToSkip" value="1" />
    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="lineTokenizer">
                <bean
                    class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="names" value="PRODUCT_ID,NAME,DESCRIPTION,PRICE" />
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean class="code.ProductFieldSetMapper" />
            </property>
        </bean>
    </property>
</bean>
<job id="importProducts" xmlns="http://www.springframework.org/schema/batch">
    <step id="readWriteProducts">
        <tasklet>
            <chunk reader="reader" writer="writer" commit-interval="100" />
        </tasklet>
    </step>
</job>

这里是界面(FieldSetMapper.java)            包裹代码;

 import org.springframework.batch.item.file.transform.FieldSet;
 import org.springframework.validation.BindException;

 public interface FieldSetMapper<T> {
T mapFieldSet(FieldSet fieldSet) throws BindException;
 }

这里是ProductFieldSetMapper.java

 package code;

import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

public class ProductFieldSetMapper implements FieldSetMapper<Product> {

public Product mapFieldSet(FieldSet fieldSet) throws BindException {
    // TODO Auto-generated method stub
    Product product = new Product();
    product.setId(fieldSet.readString("PRODUCT_ID"));
    product.setName(fieldSet.readString("NAME"));
    product.setDescription(fieldSet.readString("DESCRIPTION"));
    product.setPrice(fieldSet.readBigDecimal("PRICE"));
    return product;
}

}

这就是我正在运行的课程(Runner.java)     包裹代码;

import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.validation.BindException;

public class Runner {

public static void main(String[] args) throws BeansException, BindException {
    // TODO Auto-generated method stub
    Product product;
     ApplicationContext context = 
             new ClassPathXmlApplicationContext("FileReaderConfig.xml");

    ProductFieldSetMapper obj = (ProductFieldSetMapper) context.getBean("FieldSetMapper");

     product = (Product) obj.mapFieldSet((FieldSet)context.getBean("lineTokenizer"));

System.out.println(product.getDescription() + ""+product.getId()+""+product.getName());


}
}

我不知道我的代码在哪里(或为什么会这样)试图将ProductFieldSetMapper转换为FieldSetMapper(这只是一个界面,我知道它不会起作用)。

BTW,Product.java是一个包含变量及其各自的setter和getter的POJO。

3 个答案:

答案 0 :(得分:1)

错误是我使用自己的界面而不是Spring提供的界面的结果。我删除了我的接口类,并在导入后让ProductFieldSetMapper实现了org.springframework.batch.item.file.mapping.FieldSetMapper。这解决了这个问题。

答案 1 :(得分:0)

ProductFieldSetMapper obj = 
(ProductFieldSetMapper) context.getBean("FieldSetMapper");

应该是

ProductFieldSetMapper obj = 
(ProductFieldSetMapper) context.getBean("fieldSetMapper");

查看你的bean声明。

        <property name="fieldSetMapper">
            <bean class="code.ProductFieldSetMapper" />
        </property>

答案 2 :(得分:0)

这是带有一些修正的代码:

Runner.java (使用DelimitedLineTokenizer类将逗号分隔的字符串标记为FieldSet,该字符串进一步用于通过{{1}将其与对象(产品)映射}))

ProductFieldSetMapper

配置xml文件:(无需声明下面定义的任何bean或作业,因为您未在ApplicationContext context = new ClassPathXmlApplicationContext( "FileReaderConfig.xml"); ProductFieldSetMapper obj = (ProductFieldSetMapper) context.getBean("fieldSetMapper"); DelimitedLineTokenizer tokenizer = (DelimitedLineTokenizer) context .getBean("lineTokenizer"); FieldSet fieldSet = tokenizer.tokenize("1,Pepsi,Cold drinks,30"); Product product = (Product) obj.mapFieldSet(fieldSet); System.out.println(product.getDescription() + "-" + product.getId() + "-" + product.getName()); 类中的任何位置使用它)

Main

ProductFieldSetMapper.java:(无需定义自定义<bean id="lineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"> <property name="names" value="PRODUCT_ID,NAME,DESCRIPTION,PRICE" /> </bean> <bean id="fieldSetMapper" class="com.spring.batch.domain.ProductFieldSetMapper" />

FieldSetMapper

有关详细示例,请使用弹簧批处理作业阅读HERE并提供额外功能。