1

我正在使用 BeanIO 编写一个固定格式的文件,其中应该包含一些自定义字段。这些字段应该使用相同的基本处理程序类,但它们的参数略有不同。

我创建了一个@Record存储数据的自定义。例子:

@Record(minOccurs = 0, maxOccurs = -1)
@Fields({
    @Field(name = "recordType", ordinal = 1, length = 2, rid = true, literal = "00")})         
public class CustomField{
    @Field(ordinal = 2, length = 8, padding = '0', align = Align.RIGHT, handlerclass = AmountHandler.class)
    private BigDecimal firstAmount;

    @Field(ordinal = 3, length = 8, padding = '0', align = Align.RIGHT, handlerclass = AmountHandler.class)
    private BigDecimal secondAmount;
}

我现在想根据要使用它的字段自定义处理程序类。所以我创建了这个类:

public class AmountHandler implements ConfigurableTypeHandler{
    private int fraction, length;

    public AmountHandler (int length, int fraction){
        this.length = length;
        this.fraction = fraction;
    }

    @Override
    public TypeHandler newInstance(Properties properties) throws IllegalArgumentException {
        length = Integer.parseInt(properties.getProperty("length"));
        fraction = Integer.parseInt(properties.getProperty("fraction"));
        return new CustomHandler(length, fraction);;
    }
    //parse and format are not yet implemented. 
    @Override
    public Object parse(String text) throws TypeConversionException { return null;}

    @Override
    public String format(Object value) { return null; }

    @Override
    public Class<?> getType() { return BigDecimal.class; }
}

但是,我似乎找不到任何方法来设置每个字段的属性。我怎样才能做到这一点?有没有办法在@Field类型上定义属性?有没有更好的方法来为具有自定义参数的对象定义处理程序类?

4

0 回答 0