Map 与 beanClass 互转
2017-01-04 17:29
441 查看
/** * 使用org.apache.commons.beanutils进行转换 */ class A { public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception { if (map == null) return null; Object obj = beanClass.newInstance(); org.apache.commons.beanutils.BeanUtils.populate(obj, map); return obj; } public static Map<?, ?> objectToMap(Object obj) { if(obj == null) return null; return new org.apache.commons.beanutils.BeanMap(obj); } } /** * 使用Introspector进行转换 */ class B { public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception { if (map == null) return null; Object obj = beanClass.newInstance(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { Method setter = property.getWriteMethod(); if (setter != null) { setter.invoke(obj, map.get(property.getName())); } } return obj; } public static Map<String, Object> objectToMap(Object obj) throws Exception { if(obj == null) return null; Map<String, Object> map = new HashMap<String, Object>(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (key.compareToIgnoreCase("class") == 0) { continue; } Method getter = property.getReadMethod(); Object value = getter!=null ? getter.invoke(obj) : null; map.put(key, value); } return map; } } /** * 使用reflect进行转换 */ class C { public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception { if (map == null) return null; Object obj = beanClass.newInstance(); Field[] fields = obj.getClass().getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){ continue; } field.setAccessible(true); field.set(obj, map.get(field.getName())); } return obj; } public static Map<String, Object> objectToMap(Object obj) throws Exception { if(obj == null){ return null; } Map<String, Object> map = new HashMap<String, Object>(); Field[] declaredFields = obj.getClass().getDeclaredFields(); for (Field field : declaredFields) { field.setAccessible(true); map.put(field.getName(), field.get(obj)); } return map; } }
相关文章推荐
- 1、CommonUtils.uuid()和CommonUtils.toBean(map, Person.class)
- org.springframework.beans.NotWritablePropertyException: Invalid property 'dDuMapDao' of bean class
- Ambiguous mapping found. Cannot map 'fancyController' bean method
- BeanClassLoaderAware
- 关于问题The fully qualified name of the bean's class, except if it serves only as a parent definition fo
- 关于问题The fully qualified name of the bean's class, except if it serves only as a parent definition fo
- jsf in action 笔记:bean的创建和初始化(三)(List,Map属性)--2008.04.12
- 异常:java.lang.ClassNotFoundException: org.apache.commons.collections.map.ListOrderedMap
- The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class files
- cannot find class for bean with name的一种解决记录
- springmvc上传文件报错org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]
- JAVABEAN递归转MAP实现
- map填充bean赋值,包括父类全部填充。
- SpringXML方式配置bean的集合注入:list,map,properties
- 利用注解和反射将map转化成bean
- classpath 和 classpath* 区别以及如何覆盖在配置文件中的Bean ID
- Could not resolve type alias 'categoryMap'. Cause: java.lang.ClassNotFoundException: Cannot find cl
- 一、Error creating bean with name 'hsFactory' defined in class path resource [xxx.xml]:
- Ambiguous mapping found. Cannot map 'xxxxController' bean method
- Spring ConfigurationClassPostProcessor Bean解析及自注册过程