JavaBean和Map的相互转换
一、JavaBean
1.什么是JavaBean?
JavaBean其实就是一种遵循特定写法的类,必须遵循一定的规范:
- 类必须由public修饰,并且保证有公共的无参数的构造器
- 包含操作属性的的public方法,getter setter方法
- 字段是私有的
一个JavaBean是由3部分组成,分别是属性、方法、和事件。注意这里的属性是property,表示的是状态,并不是字段(Field)我们在刚开始学面向对象的时候其实有的时候也是用到了这个规范,我们写的getter和setter方法,比如public void setName(String name){ this.name = name;}属性其实就是name,在比如public int getAge(){return 19;},属性其实就是age。一般的,我们写getter个setter方法用的是IDE自己生成的,属性和字段同名。一定要区分属性和字段,它们并不是一个东西,也存在属性名和字段名不同。
2.内省机制
内省机制的作用就是用来查看和操作JavaBean的属性。
- 获取JavaBean的属性名
- 通过getter方法给属性获取值,通过setter给属性设置值
常用的API
Introspector类中
1 2 3 4
| static BeanInfo getBeanInfo(Class<?> beanClass) 在 Java Bean 上进行内省,了解其所有属性、公开的方法和事件。 static BeanInfo getBeanInfo(Class<?> beanClass, Class<?> stopClass) 在给定的“断”点之下,在 Java Bean 上进行内省,了解其所有属性和公开的方法。
|
可以看到在Introspector类中有两个静态方法可以获得获取JavaBean的信息,一般的第一个会得到父类Object.class中的getClass方法,第二种会屏蔽掉Object类中的方法。返回一个BeanInfo,在BeanInfo接口中有一个方法可以获得bean的描述器对象
1 2
| MethodDescriptor[] getMethodDescriptors() 获得 beans MethodDescriptor。 PropertyDescriptor[] getPropertyDescriptors() 获得 beans PropertyDescriptor。
|
分别返回的是各自的描述器对象
1 2
| Method getReadMethod() 获得应该用于读取属性值的方法。 就是getter方法 Method getWriteMethod() 获得应该用于写入属性值的方法。 就是setter方法
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| public class User { private String name; private int age; private boolean man; public String getName() { return name; } ...省略剩余getter setter方法 @Override public String toString() { return "User [name=" + name + ", age=" + age + ", man=" + man + "]"; } public static void main(String[] args) throws Exception { BeanInfo info = Introspector.getBeanInfo(User.class,Object.class); PropertyDescriptor[] pds = info.getPropertyDescriptors(); User u = User.class.newInstance(); for (PropertyDescriptor pd : pds) { System.out.println(pd.getName()+","+pd.getPropertyType()()); Method get = pd.getReadMethod(); Method set = pd.getWriteMethod(); System.out.println("getter方法:"+get); System.out.println("setter方法:"+set); if("name".equals(pd.getName())){ Method setter = pd.getWriteMethod(); setter.invoke(u, "xx"); Method getter = pd.getReadMethod(); Object o = getter.invoke(u); System.out.println(o); } } System.out.println(u); } }
|
二、JavaBean和Map的相互转化
我们可以发现javabean和map之间有很多相似之处.在map中是一对键值对key-value,而在JavaBean中是属性名-属性值。现在对这两种进行相互转换
1.JavaBean转化为Map
思路:把bean转化为map,不过就是把bean中的属性名给map的key,把属性值给map中的value。在程序上表达,提供一个方法,传入的是一个JavaBean对象,返回的是一个Map。
上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public static Map<String,Object> bean2map(Object bean) throws Exception{ Map<String,Object> map = new HashMap<>(); BeanInfo b = Introspector.getBeanInfo(bean.getClass(),Object.class); PropertyDescriptor[] pds = b.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { String propertyName = pd.getName(); Method m = pd.getReadMethod(); Object properValue = m.invoke(bean);
map.put(propertyName, properValue); } return map; }
|
2.把Map转化为JavaBean
思路:map转化为javabean,不就是把map的value设置给属性为属性值,也就在找到bean中和Map中key相同的属性名,把这个key对应的value设置给这个属性。在程序上表达就是,提供一个方法,传入map对象和一个bean的Class类型,返回一个JavaBean对象。注意,这里传入的参数多了一个,因为肯定得用反射创建bean对象把,如果只传入一个map对象,怎么创建对象?怎么知道吧map转化为什么类型的JavaBean。好了,直接撸代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public static <T> T map2bean(Map<String,Object> map,Class<T> clz) throws Exception{ T obj = clz.newInstance();
BeanInfo b = Introspector.getBeanInfo(clz,Object.class); PropertyDescriptor[] pds = b.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { Method setter = pd.getWriteMethod(); setter.invoke(obj, map.get(pd.getName())); } return obj; }
|