java bean 对象与map对象属性相互拷贝

/**
 * @author  zsl
 */
public class BeanUtil {

    /**
     * 将map中的值拷贝到bean中
     * @param source  属性来源map
     * @param target  目标Bean
     */
    public static  void copyProperties2Bean(Map<String,Object> source, Object target) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
            BeanInfo beanInfo = Introspector.getBeanInfo(target.getClass());
            PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor prop: properties) {
                String key = prop.getName();
                if(source.containsKey(key) && source.get(key) != null){
                    Object value = source.get(key);
                    Method setMethod = prop.getWriteMethod();
                    setMethod.invoke(target,value);
                }
            }


    }

    /**
     * 将bean中的属性存入到map中
     * @param source  来源bean
     * @param target
     */
    public static  void copyProperties2Map(Object source,Map<String,Object>  target) throws IntrospectionException, InvocationTargetException, IllegalAccessException {


            //1.获取bean信息
            BeanInfo beanInfo = Introspector.getBeanInfo(source.getClass());
            PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
            if(properties != null && properties.length>0){
                for (PropertyDescriptor prop :properties) {
                    //2.得到属性名
                    String name = prop.getName();
                    //3.过滤class属性
                    if(!"class".equals(name)){
                        //4.得到属性的get方法
                        Method getter = prop.getReadMethod();

                        //5.获取属性值
                        Object value = getter.invoke(source);
                        //6.放入map中
                        if(value != null ){
                            target.put(name,value);
                        }
                    }
                }
            }
    }


    /**
     * 根据字段名从对象反射取值
     * @param source
     * @param filed
     * @return
     * @throws IntrospectionException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static Object getValue(Object source,String filed) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
        //1.获取bean信息
        BeanInfo beanInfo = Introspector.getBeanInfo(source.getClass());
        PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();

        if(properties != null && properties.length>0){
            for (PropertyDescriptor prop :properties) {
                //2.得到属性名
                String name = prop.getName();
                //3.过滤class属性
                if(!"class".equals(name) && Objects.equals(filed,name)){
                    //4.得到属性的get方法
                    Method getter = prop.getReadMethod();

                    //5.获取属性值
                   Object  value = getter.invoke(source);
                    return value;

                }
            }
        }
        return  null;
    }

    /**
     * 实现bean之间的属性对拷
     * 之间使用spring 的BeanUtils工具进行实现
     * @param source
     * @param target
     * @param ignoreProperties 忽略的属性
     */
    public static void copyProperties(Object source ,Object target,String... ignoreProperties){
        BeanUtils.copyProperties(source,target,ignoreProperties);
    }




}

你可能感兴趣的:(java,bean,property)