這篇文章主要介紹“Java反射中java.beans包的用法”,在日常操作中,相信很多人在Java反射中java.beans包的用法問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Java反射中java.beans包的用法”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供威遠(yuǎn)網(wǎng)站建設(shè)、威遠(yuǎn)做網(wǎng)站、威遠(yuǎn)網(wǎng)站設(shè)計(jì)、威遠(yuǎn)網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、威遠(yuǎn)企業(yè)網(wǎng)站模板建站服務(wù),十載威遠(yuǎn)做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。看一個(gè)例子:
NodeDO.java:一個(gè)標(biāo)準(zhǔn)的javabean對(duì)象
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; } }
我們想實(shí)現(xiàn)類似這樣的功能:
public static void main(String[] args) throws Exception{ Mapparameters = new HashMap (){ { put("nodeName", "小胖測(cè)試"); 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這個(gè)map轉(zhuǎn)化成TestDO的對(duì)象,該如何來(lái)做呢?
(1)首先要定義一個(gè)用來(lái)轉(zhuǎn)化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)做轉(zhuǎn)化:
public class PropertyEditorSample { static{ PropertyEditorManager.registerEditor(NodeDO.class, DodeDOEditor.class); } public static void main(String[] args) throws Exception{ Mapparameters = new HashMap (){ { put("nodeName", "小胖測(cè)試"); 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 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; } }
其實(shí)關(guān)鍵點(diǎn)就兩句話
editor.setAsText(parameters.get(pd.getName()));//1 editor.getValue();//2 //因此,一般都會(huì)在setAsText中去調(diào)用setValue()來(lái)保存轉(zhuǎn)化以后的值,這樣通過(guò)getValue()就能獲取到了到此,關(guān)于“Java反射中java.beans包的用法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
網(wǎng)頁(yè)標(biāo)題:Java反射中java.beans包的用法-創(chuàng)新互聯(lián)
轉(zhuǎn)載來(lái)源:http://weahome.cn/article/dshedc.html