0
我使用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...
}
是任何安全的方式那样做,不浪费记忆和时间?