Apache Commons BeanUtils包学习 2 -beanutils PropertyUtils bean
原文:http://space.itpub.net/7191730/viewspace-446960#xspace-tracks
1.簡介
在一般的寫bean組件的時候,都必須要寫setter和getter方法,當(dāng)然如果我們事先已經(jīng)知道bean的相關(guān)屬性和方法,寫bean是比較簡單的,但是組件太多的時候,重復(fù)編寫常常是枯燥乏味令人厭煩的。但當(dāng)有些時候我們需要調(diào)用動態(tài)對象 的屬性的時候,我們應(yīng)該怎么來設(shè)定和獲取對象的屬性呢?BeanUtils就可以幫助我們來解決這個問題。它需要Collections包和logging包的支持。
2.已經(jīng)使用BeanUtils的事例
*構(gòu)建和java 對象模型交互的腳本語言,比如Bean Scripting Framework
*構(gòu)建web 層顯示和相似用途的模板語言處理器,如jsp或者Velocity
*構(gòu)建適用于jsp和xsp環(huán)境的自定義標(biāo)簽庫,如Jakarta Taglibs,Struts,Cocoon
*處理基于xml的配置資源文件,如Ant創(chuàng)建腳本,應(yīng)用程序部署文件和Tomcat的xml配置文件
3.API簡介
BeanUtils 的 Java API 主要的 package 總共四個
org.apache.commons.beanutils 這個是BeanUtils最基本的包,提供了處理getter和setter方法屬性的類
org.apache.commons.beanutils.converters :Converter接口的標(biāo)準(zhǔn)實現(xiàn),在啟動的時候和ConvertUtils一同注冊
org.apache.commons.beanutils.locale :組件的本地化處理包
org.apache.commons.beanutils.locale.converters :LocaleConverter接口的標(biāo)準(zhǔn)實現(xiàn),啟動的時候和LocaleConvertUtils 一同注冊
4.org.apache.commons.beanutils介紹
這四個包本專題只介紹這一個,其他的使用的時候請自己參考它的文檔,文檔內(nèi)容寫得很好.
?1.PropertyUtils (Set時不能自動進(jìn)行類型轉(zhuǎn)換?)
?它支持三種類型的屬性值-Simple(簡單屬性),Indexed(數(shù)組屬性),Mapped(Map屬性)
?我們先創(chuàng)建一個簡單的bean
???? public class Employee {
??????? private Map address;
??????? Private Employee[] subordinate;
??????? private String firstName;
??????? private String lastName;
??????? public Address getAddress(String type);
??????? public void setAddress(String type, Address address);
??????? public Employee getSubordinate(int index);
??????? public void setSubordinate(int index, Employee subordinate);
??????? public String getFirstName();
??????? public void setFirstName(String firstName);
??????? public String getLastName();
??????? public void setLastName(String lastName);
??? }
?1) Simple屬性訪問方法
?PropertyUtils.getSimpleProperty(Object bean, String name)
?PropertyUtils.setSimpleProperty(Object bean, String name, Object value)
?實現(xiàn)代碼:
???? Employee employee = ...;
??? String firstName = (String) PropertyUtils.getSimpleProperty(employee, "firstName");
??? String lastName = (String) PropertyUtils.getSimpleProperty(employee, "lastName");
??? ... manipulate the values ...
??? PropertyUtils.setSimpleProperty(employee, "firstName", firstName);
??? PropertyUtils.setSimpleProperty(employee, "lastName", lastName);
?2)Indexed屬性訪問方法
?PropertyUtils.getIndexedProperty(Object bean, String name)
?PropertyUtils.getIndexedProperty(Object bean, String name, int index)
?PropertyUtils.setIndexedProperty(Object bean, String name, Object value)
?PropertyUtils.setIndexedProperty(Object bean, String name, int index, Object value)
?實現(xiàn)代碼:
?Employee employee = ...;
??? int index = ...;
??? String name = "subordinate[" + index + "]";
??? Employee subordinate = (Employee) PropertyUtils.getIndexedProperty(employee, name);//根據(jù)value取得屬性
??? Employee employee = ...;
??? int index = ...;
??? Employee subordinate = (Employee) PropertyUtils.getIndexedProperty(employee, "subordinate", index);//根據(jù)索引?值取的屬性值
?3)Mapped屬性訪問方法
?PropertyUtils.getMappedProperty(Object bean, String name)
?PropertyUtils.getMappedProperty(Object bean, String name, String key)
?PropertyUtils.setMappedProperty(Object bean, String name, Object value)
?PropertyUtils.setMappedProperty(Object bean, String name, String key, Object value)
?實現(xiàn)代碼:
?Employee employee = ...;
??? Address address = ...;
??? PropertyUtils.setMappedProperty(employee, "address(home)", address);//根據(jù)數(shù)組內(nèi)的值來去的相應(yīng)的屬性值
??? Employee employee = ...;
??? Address address = ...;
??? PropertyUtils.setMappedProperty(employee, "address", "home", address);
4)Nested屬性訪問方法 //Nested的意思是參數(shù)中包含組件
?PropertyUtils.getNestedProperty(Object bean, String name)
?PropertyUtils.setNestedProperty(Object bean, String name, Object value)
?實現(xiàn)代碼:
?String city = (String) PropertyUtils.getNestedProperty(employee,??"address(home).city");
?2.BeanUtils.DynaBean and BeanUtils.DynaClass 接口介紹
?DynaBean必須有一個bean來實現(xiàn)這個接口,DynaClass必有一個bean的屬性集來實現(xiàn)
?1)BasicDynaBean and BasicDynaClass - 基本的 Dynamic 類型
?基本的API:
?BasicDynaClass(java.lang.String name, java.lang.Class dynaBeanClass, DynaProperty[] properties)
?BasicDynaBean(DynaClass dynaClass)
?我們先定義一下基本的代碼:
?//定義動態(tài)屬性集
?DynaProperty[] props = new DynaProperty[]{
??? new DynaProperty("address", java.util.Map.class),
??? new DynaProperty("subordinate", mypackage.Employee[].class),
??? new DynaProperty("firstName", String.class),
??? new DynaProperty("lastName",? String.class)
????? };
?//創(chuàng)建動態(tài)類
??? BasicDynaClass dynaClass = new BasicDynaClass("employee", null, props);
?DynaBean employee = dynaClass.newInstance();
//設(shè)定動態(tài)屬性值
??? employee.set("address", new HashMap());
??? employee.set("subordinate", new mypackage.Employee[0]);
??? employee.set("firstName", "Fred");
??? employee.set("lastName", "Flintstone");
?2)ResultSetDynaClass (Wraps ResultSet in DynaBeans) - 使用 ResultSet 的 Dynamic JavaBean
?API:
?ResultSetDynaClass(java.sql.ResultSet resultSet)
?ResultSetDynaClass(java.sql.ResultSet resultSet, boolean lowerCase)
?Connection conn = ...;
??? Statement stmt = conn.createStatement();
??? ResultSet rs = stmt.executeQuery
??? ("select account_id, name from customers");
??? Iterator rows = (new ResultSetDynaClass(rs)).iterator();
??? while (rows.hasNext()) {
?//利用動態(tài)bean進(jìn)行輸出
??? DynaBean row = (DynaBean) rows.next();
??? System.out.println("Account number is " +
?????????????????????? row.get("account_id") +
?????????????????????? " and name is " + row.get("name"));
??? }
??? rs.close();
??? stmt.close();
?4)RowSetDynaClass (Disconnected ResultSet as DynaBeans) - 使用 RowSet 的 Dynamic JavaBean
?API:
?RowSetDynaClass(java.sql.ResultSet resultSet)
?RowSetDynaClass(java.sql.ResultSet resultSet, boolean lowerCase)
?
??? Connection conn = ...;? // 從緩沖池取得連接
??? Statement stmt = conn.createStatement();
??? ResultSet rs = stmt.executeQuery("SELECT ...");
??? RowSetDynaClass rsdc = new RowSetDynaClass(rs);
??? rs.close();
??? stmt.close();
??? ...;??????????????????? //關(guān)閉連接返回緩沖池
??? List rows = rsdc.getRows();
??? ...;?????????????????? // 處理得到的行
總結(jié)
以上是生活随笔為你收集整理的Apache Commons BeanUtils包学习 2 -beanutils PropertyUtils bean的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ctfmon是什么启动项_win10系统
- 下一篇: 小球碰撞(面向对象)