Java BeanIO-设置@Field的属性以获取ConfigurableTypeHandler的新实例
我正在使用BeanIO编写一个固定格式的文件,其中应该包含一些自定义字段。这些字段应使用相同的基本处理程序类,但参数略有不同 我已经创建了一个自定义的Java BeanIO-设置@Field的属性以获取ConfigurableTypeHandler的新实例,java,bean-io,typehandler,Java,Bean Io,Typehandler,我正在使用BeanIO编写一个固定格式的文件,其中应该包含一些自定义字段。这些字段应使用相同的基本处理程序类,但参数略有不同 我已经创建了一个自定义的@记录,用于存储数据。例如: @Record(minOccurs = 0, maxOccurs = -1) @Fields({ @Field(name = "recordType", ordinal = 1, length = 2, rid = true, literal = "00")}) public class Cu
@记录
,用于存储数据。例如:
@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;
}
现在我想根据要使用它的字段自定义handlerclass。所以我创建了这个类:
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; }
}
公共类AmountHandler实现ConfigurableTypeHandler{
私有整数分数,长度;
公共AmountHandler(整数长度,整数分数){
这个长度=长度;
这个。分数=分数;
}
@凌驾
公共TypeHandler newInstance(属性)抛出IllegalArgumentException{
length=Integer.parseInt(properties.getProperty(“length”);
分数=整数.parseInt(properties.getProperty(“分数”));
返回新的CustomHandler(长度,分数);;
}
//解析和格式化尚未实现。
@凌驾
公共对象分析(字符串文本)抛出TypeConversionException{return null;}
@凌驾
公共字符串格式(对象值){return null;}
@凌驾
公共类getType(){return BigDecimal.Class;}
}
然而,我似乎找不到任何方法来设置每个字段的属性。
我该怎么做?有没有办法定义@字段
类型的属性?有没有更好的方法为具有自定义参数的对象定义处理程序类