문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top