2013-02-12 105 views
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... 
} 

是任何安全的方式那樣做,不浪費記憶和時間?

回答

1

不,DataBinder中的目標對象是最終的,如果你想綁定另一個對象,你必須爲它創建一個新的DataBinder