Frage

I'm creating few objects in loop using DataBinder. I wonder if it is possible to reuse DataBinder object instead of create it each time.

Is it possible?

Now I have:

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...
}

I'd like to have (this is imagination only...):

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...
}

Is any safe way to do it like that, to not waste memory and time?

War es hilfreich?

Lösung

No, target object in DataBinder is final, if you want to bind on another object you are forced to create a new DataBinder for it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top