Java反射中java.beans包学习总结
之前一直以为PropertyEditor是SpringMVC提供的,今天才知道这个是jdk提供的,汗颜啊!
这个东西能干啥用呢?可以把字符串转成一个bean对象,spring mvc在把form表单映射到controller的入参对象就是利用的这个东西。
看一个例子:
NodeDO.java:一个标准的javabean对象
public class NodeDO { private String name; private String email; private Date date; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "NodeDO [name=" + name + ", email=" + email + ", date=" + DodeDOEditor.sdf.format(date) + "]"; } }
TestDO.java一样,里面引用了NodeDO:
public class TestDO { private String nodeName; private NodeDO nodeDO; public String getNodeName() { return nodeName; } public void setNodeName(String nodeName) { this.nodeName = nodeName; } public NodeDO getNodeDO() { return nodeDO; } public void setNodeDO(NodeDO nodeDO) { this.nodeDO = nodeDO; } }
我们想实现类似这样的功能:
public static void main(String[] args) throws Exception{ Map<String, String> parameters = new HashMap<String, String>(){ { put("nodeName", "小胖测试"); put("nodeDO", "xiaopang|xiaopang@163.com|2015-10-20 12:00:00"); } }; TestDO testDo = convert(parameters); System.out.println(testDo.getNodeName()); System.out.println(testDo.getNodeDO()); }
把parameters这个map转化成TestDO的对象,该如何来做呢?
(1)首先要定义一个用来转化Property的PropertyEditor:
public class DodeDOEditor extends PropertyEditorSupport{ public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public void setAsText(String text) throws IllegalArgumentException{ String[] tokens = text.split("\\|"); NodeDO nodeDo = new NodeDO(); nodeDo.setName(tokens[0]); nodeDo.setEmail(tokens[1]); try{ nodeDo.setDate(sdf.parse(tokens[2])); }catch(ParseException e){ throw new IllegalArgumentException(e); } setValue(nodeDo); } }
(2)做转化:
public class PropertyEditorSample { static{ PropertyEditorManager.registerEditor(NodeDO.class, DodeDOEditor.class); } public static void main(String[] args) throws Exception{ Map<String, String> parameters = new HashMap<String, String>(){ { put("nodeName", "小胖测试"); put("nodeDO", "xiaopang|xiaopang@163.com|2015-10-20 12:00:00"); } }; TestDO testDo = convert(parameters); System.out.println(testDo.getNodeName()); System.out.println(testDo.getNodeDO()); } private static TestDO convert(Map<String, String> parameters)throws Exception { TestDO testDO = new TestDO(); BeanInfo bi = Introspector.getBeanInfo(TestDO.class); PropertyDescriptor[] pds = bi.getPropertyDescriptors(); for(PropertyDescriptor pd : pds){ Class<?> propertyType = pd.getPropertyType(); Method writeMethod = pd.getWriteMethod(); if(propertyType == Class.class){ //ignore }else if(propertyType == String.class){ writeMethod.invoke(testDO, parameters.get(pd.getName())); }else{ PropertyEditor editor = PropertyEditorManager.findEditor(propertyType); if(editor != null){ editor.setAsText(parameters.get(pd.getName())); writeMethod.invoke(testDO, editor.getValue()); }else{ System.out.println("no editor for:"+pd.getName()); } } } return testDO; } }
其实关键点就两句话
<pre name="code" class="java">editor.setAsText(parameters.get(pd.getName()));//1 editor.getValue();//2 //因此,一般都会在setAsText中去调用setValue()来保存转化以后的值,这样通过getValue()就能获取到了
以上就是本次整理的全部内容,如果大家还有任何不明白的地方,可以在下方留言讨论,感谢大家对我们的支持。
赞 (0)