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...
}
是任何安全的方式那樣做,不浪費記憶和時間?