首页 > 编程语言 > 详细

JAVA 获取/设置bean的某个属性值

时间:2021-09-02 20:51:01      阅读:17      评论:0      收藏:0      [点我收藏+]
public class test{
    // 设置bean的某个属性值
    public static void setProperty(MaterialPriceLineVO materialPriceLineVO, String fieldName, String value) throws Exception {
        // 获取bean的某个属性的描述符
        PropertyDescriptor propDesc = new PropertyDescriptor(fieldName, MaterialPriceLineVO.class);
        // 获得用于写入属性值的方法
        Method methodSetUserName = propDesc.getWriteMethod();
        // 写入属性值
        methodSetUserName.invoke(materialPriceLineVO, value);
    }

    // 获取bean的某个属性值
    public static String getStringProperty(MaterialPriceLineVO materialPriceLineVO, String fieldName) throws Exception {
        // 获取Bean的某个属性的描述符
        PropertyDescriptor proDescriptor = new PropertyDescriptor(fieldName, MaterialPriceLineVO.class);
        // 获得用于读取属性值的方法
        Method methodGet = proDescriptor.getReadMethod();
        // 读取属性值
        Object objValue = methodGet.invoke(materialPriceLineVO);
        if (ObjectUtils.isEmpty(objValue) || "null".equals(objValue)){
            return null;
        }else {
            return objValue.toString();
        }
    }

    public static String getIntegerProperty(MaterialPriceLineVO materialPriceLineVO, String fieldName) throws Exception {
        // 获取Bean的某个属性的描述符
        PropertyDescriptor proDescriptor = new PropertyDescriptor(fieldName, MaterialPriceLineVO.class);
        // 获得用于读取属性值的方法
        Method methodGet = proDescriptor.getReadMethod();
        // 读取属性值
        Object objValue = methodGet.invoke(materialPriceLineVO);
        if (ObjectUtils.isEmpty(objValue) || "null".equals(objValue)){
            return null;
        }else {
            return objValue.toString();
        }
    }

    public static String getBigDecimalProperty(MaterialPriceLineVO materialPriceLineVO, String fieldName) throws Exception {
        // 获取Bean的某个属性的描述符
        PropertyDescriptor proDescriptor = new PropertyDescriptor(fieldName, MaterialPriceLineVO.class);
        // 获得用于读取属性值的方法
        Method methodGet = proDescriptor.getReadMethod();
        // 读取属性值
        Object objValue = methodGet.invoke(materialPriceLineVO);
        if (ObjectUtils.isEmpty(objValue) || "null".equals(objValue)){
            return null;
        }else {
            return objValue.toString();
        }
    }
}

 

JAVA 获取/设置bean的某个属性值

原文:https://www.cnblogs.com/xiexiaoxia/p/15219848.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!