我正在使用 DataBinder 循环创建几个对象。我想知道是否可以重用 DataBinder 对象而不是每次都创建它。
可能吗?
我现在有:
while (condition) {
obj = new MyObj();
DataBinder db = new DataBinder(obj,"my obj");
db.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(dateFormatParam), false));
// set up binder with some editors etc.
objectvalues = getValues();
MutablePropertyValues mpv = new MutablePropertyValues();
for (int i=0;i<fieldNames.length;i++){
mpv.add(StringUtils.trim(fieldNames[i]), objectvalues[i]);
}
db.bind(mpv);
// do something with obj...
}
我想要(这只是想象......):
obj = new MyObj();
DataBinder db = new DataBinder(obj,"my obj");
db.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(dateFormatParam), false));
// set up binder with some editors etc.
MutablePropertyValues mpv = new MutablePropertyValues();
for (int i=0;i<fieldNames.length;i++){
mpv.add(StringUtils.trim(fieldNames[i]), "empty value");
}
while (condition) {
objectvalues = getValues();
for (int i=0;i<fieldNames.length;i++){
mpv.setPropertyValueAt(objectvalues[i], i);
}
db.bind(mpv);
objCopy = obj.clone();
// do something with objCopy...
}
有什么安全的方法可以做到这一点,不浪费内存和时间吗?