zoukankan      html  css  js  c++  java
  • PropertyDescriptor动态setter和getter设置对象属性

    PropertyDescriptor

    我们在开发的过程中,有时会需要动态地设置属性,也就是动态getter、 setter。
    使用传统的反射 Method、Field等类去处理,需要对方法名进行大量拼接,比较麻烦。
    可以使用 PropertyDescriptor.

    常用方法:

    • 构造方法:
    PropertyDescriptor(String propertyName, Class<?> beanClass): 第一个参数是属性名称,第二个参数是Class类。
    
    • 其他方法:
    Method getReadMethod(): 返回 Method
    
    propertyDescriptor.getReadMethod().invoke(Object obj, Object... args):
     相当于 get方法。第一个参数是对象,后面的参数是属性名称
    
    propertyDescriptor.getWriteMethod().invoke(Object obj): 相当于 set方法。
    

    示例如下:

    • 对象类 Person :
    public class Person {
    
        private int age;
    
        private String name;
    
        private String address;
      
        //getter、 setter方法
    }
    
    • PropertyDescriptor使用如下:
        public static void propertyDescriptorTest() {
            try {
                Person person = new Person();
                //setter
                PropertyDescriptor propertyDescriptor = new PropertyDescriptor("address", Person.class);
                propertyDescriptor.getWriteMethod().invoke(person, "深圳市");
                //getter
                String address = (String)propertyDescriptor.getReadMethod().invoke(person);
                System.out.println(address);
            }  catch (Exception e) {
                log.error("property Exception.",e);
            }
        }
    
    

    BeanWrapper 和 BeanWrapperImpl

    BeanWrapper和BeanWrapperImpl是 Spring的接口和类,可以通过BeanWrapper和BeanWrapperImpl获取 PropertyDescriptor。

    • 构造方法:
    BeanWrapperImpl(Object object): 参数为对象
    
    • 其他方法:
    PropertyDescriptor[] getPropertyDescriptors(): 用于获取对象的所有 PropertyDescriptor
    
    PropertyDescriptor getPropertyDescriptor(String var1) throws InvalidPropertyException: 获取指定属性名称的PropertyDescriptor
    

    BeanWrapperImpl示例如下:

    • 通过 BeanWrapper获取 PropertyDescriptor:
        public static void getPropertyByBeanWrapper() {
            try {
                Person person = new Person();
                //初始化
                BeanWrapper beanWrapper = new BeanWrapperImpl(person);
                //setter
                PropertyDescriptor propertyDescriptor = beanWrapper.getPropertyDescriptor("address");
                propertyDescriptor.getWriteMethod().invoke(person, "深圳市");
                //getter
                String address = (String)propertyDescriptor.getReadMethod().invoke(person);
                System.out.println(address);
            } catch (Exception e) {
                log.error("getPropertyNameDescriptor exception.", e);
            }
        }
    
    • 通过 BeanWrapper 设置所有的属性:
    public static void setAllProperty(Person person) {
    	BeanWrapper beanWrapper = new BeanWrapperImpl(person);
    	//获取所有的 propertyDescriptor
    	PropertyDescriptor[] propertyDescriptors = beanWrapper.getPropertyDescriptors();
    	for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
    		String name = propertyDescriptor.getName();
    		Class<?> propertyType = propertyDescriptor.getPropertyType();
    		boolean isNull = beanWrapper.getPropertyValue(name) == null;
    		//所有String类型的属性,如果为null,改为""
    		if (String.class == propertyType && isNull) {
    			beanWrapper.setPropertyValue(name, "");
    		}
    		//所有Integer类型的属性,如果为null,改为0
    		if (Integer.class == propertyType && isNull) {
    			beanWrapper.setPropertyValue(name, 0);
    		}
    	}
    }
    
    
  • 相关阅读:
    MySQL查询所有库中表名
    MySQL统计数据库表大小
    Spring Cloud 自定义ConfigServer 解决敏感信息存储问题
    JQuery Ajax执行过程AOP拦截
    虚拟机下的centos断电(非正常关机)后mysql启动不了
    Ubuntu 13.10 如何修改背景色--豆沙绿
    CI框架CodeIgniter伪静态各种服务器设置
    MongoDB中MapReduce不同的写法,不同的结果
    分享个人预算系统源码(含说明文档)
    Java lambda 分组后多列求和
  • 原文地址:https://www.cnblogs.com/expiator/p/15426140.html
Copyright © 2011-2022 走看看