mercredi 22 juin 2022

Why is Spring's BeanWrapper#getPropertyValue so slow?

This is my test method.I'm trying to invoke this method 100000 times, and it cost 12000ms.

private static void test() {
    MyBean bean = new MyBean();
    Field[] fields = MyBean.class.getDeclaredFields();
    BeanWrapper beanWrapper = new BeanWrapperImpl(bean);
    for (Field f : fields) {
        f.setAccessible(true);
        String fieldName = f.getName();
        Object fieldValue = beanWrapper.getPropertyValue(fieldName);
    }
}

And when I change the code.

private static void test() {
    MyBean bean = new MyBean();
    Field[] fields = MyBean.class.getDeclaredFields();
    BeanWrapper beanWrapper = new BeanWrapperImpl(bean);
    for (Field f : fields) {
        f.setAccessible(true);
        String fieldName = f.getName();
        Object fieldValue = f.get(bean);
        // Object fieldValue = beanWrapper.getPropertyValue(fieldName);
    }
}

And it only costs about 400ms. Why is Spring's BeanWrapper#getPropertyValue so slow?





Aucun commentaire:

Enregistrer un commentaire