您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java反射中java.beans包的用法”,在日常操作中,相信很多人在Java反射中java.beans包的用法問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Java反射中java.beans包的用法”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
看一個(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{ Map<String, String> parameters = new HashMap<String, String>(){ { 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ì)象,該如何來做呢?
(1)首先要定義一個(gè)用來轉(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{ Map<String, String> parameters = new HashMap<String, String>(){ { 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<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; } }
其實(shí)關(guān)鍵點(diǎn)就兩句話
<pre name="code" class="java">editor.setAsText(parameters.get(pd.getName()));//1 editor.getValue();//2 //因此,一般都會(huì)在setAsText中去調(diào)用setValue()來保存轉(zhuǎn)化以后的值,這樣通過getValue()就能獲取到了
到此,關(guān)于“Java反射中java.beans包的用法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。