我正在使用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
类型上定义属性?有没有更好的方法为具有自定义参数的对象定义处理程序类?