`

propertyMap = BeanUtils.describe(cust) ;propertyMap = PropertyUtils.describe(cus

    博客分类:
  • Bean
阅读更多
package beantest;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BasicDynaBean;
import org.apache.commons.beanutils.BasicDynaClass;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

public class BeanUtilsUsage {

	public static void main(String[] args) throws Exception {

		// demoNormalJavaBeans();

		// demoDynaBeans();

		Address addr1 = new Address("CA1234", "xxx", "Los Angeles", "USA");

		Address addr2 = new Address("100000", "xxx", "Beijing", "China");

		Address[] addrs = new Address[2];

		addrs[0] = addr1;

		addrs[1] = addr2;

		Customer cust = new Customer(123, "John Smith", addrs);

		Map propertyMap = new HashMap();

		// ok所有的都会转换成为String
		propertyMap = BeanUtils.describe(cust);
		logMap(propertyMap);

		// 错误,会按照原来的类型进行转换 Long不能转为String (Customer id is long type)
		/*
		 * propertyMap = PropertyUtils.describe(cust);
		 * 
		 * 
		 * 
		 * logMap2(propertyMap);
		 * 
		 *  // 错误,会按照原来的类型进行转换 Long不能转为String (Customer id is long type) //Map<String,String>改成<String
		 * ,Object>比较好 /*propertyMap = PropertyUtils.describe(cust);
		 * 
		 * logMap(propertyMap);
		 */

		propertyMap = PropertyUtils.describe(cust);

		logMap2(propertyMap);
	}

	static void logMap(Map<String, String> map) {

		/*
		 * always <Map.Entry> as Object stored in entrySet
		 */
		Set<Map.Entry<String, String>> entrySet = map.entrySet();

		/*
		 * Iterator keeps the object in this collection
		 */
		Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();

		while (iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			System.out.print(entry.getKey() + ": ");
			System.out.println(entry.getValue());
		}

	}

	static void logMap2(Map<String, Object> map) {

		/*
		 * always <Map.Entry> as Object stored in entrySet
		 */
		Set<Map.Entry<String, Object>> entrySet = map.entrySet();

		/*
		 * Iterator keeps the object in this collection
		 */
		Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator();

		while (iterator.hasNext()) {
			Map.Entry<String, Object> entry = iterator.next();
			System.out.print(entry.getKey() + ": ");
			System.out.println(entry.getValue());
		}

	}

	public static void demoNormalJavaBeans() throws Exception {

		System.out
				.println(StringUtils.center(" demoNormalJavaBeans ", 40, "="));

		// data setup

		Address addr1 = new Address("CA1234", "xxx", "Los Angeles", "USA");

		Address addr2 = new Address("100000", "xxx", "Beijing", "China");

		Address[] addrs = new Address[2];

		addrs[0] = addr1;

		addrs[1] = addr2;

		Customer cust = new Customer(123, "John Smith", addrs);

		// accessing the city of first address

		String cityPattern = "addresses[0].city";

		String name = (String) PropertyUtils.getSimpleProperty(cust, "name");

		String city = (String) PropertyUtils.getProperty(cust, cityPattern);

		Object[] rawOutput1 = new Object[] { "The city of customer ", name,

		"'s first address is ", city, "." };

		System.out.println(StringUtils.join(rawOutput1));

		// setting the zipcode of customer's second address

		String zipPattern = "addresses[1].zipCode";

		if (PropertyUtils.isWriteable(cust, zipPattern)) {

			System.out.println("Setting zipcode ...");

			PropertyUtils.setProperty(cust, zipPattern, "200000");

		}

		String zip = (String) PropertyUtils.getProperty(cust, zipPattern);

		Object[] rawOutput2 = new Object[] { "The zipcode of customer ", name,

		"'s second address is now ", zip, "." };

		System.out.println(StringUtils.join(rawOutput2));

		System.out.println();

	}

	public static void demoDynaBeans() throws Exception {

		System.out.println(StringUtils.center(" demoDynaBeans ", 40, "="));

		// creating a DynaBean

		DynaProperty[] dynaBeanProperties = new DynaProperty[] {

		new DynaProperty("name", String.class),

		new DynaProperty("inPrice", Double.class),

		new DynaProperty("outPrice", Double.class),

		};

		BasicDynaClass cargoClass = new BasicDynaClass("Cargo",
				BasicDynaBean.class, dynaBeanProperties);

		DynaBean cargo = cargoClass.newInstance();

		// accessing a DynaBean

		cargo.set("name", "Instant Noodles");

		cargo.set("inPrice", new Double(21.3));

		cargo.set("outPrice", new Double(23.8));

		System.out.println("name: " + cargo.get("name"));

		System.out.println("inPrice: " + cargo.get("inPrice"));

		System.out.println("outPrice: " + cargo.get("outPrice"));

		System.out.println();

	}

}

 

 

 

===================================================================

package beantest;

public class Customer {

	private long id;

	private String name;

	private Address[] addresses;

	public Customer() {

	}

	public Customer(long id, String name, Address[] addresses) {

		this.id = id;

		this.name = name;

		this.addresses = addresses;

	}

	public Address[] getAddresses() {  

		return addresses;

	}

	public void setAddresses(Address[] addresses) {

		this.addresses = addresses;

	}

	public long getId() {

		return id;

	}

	public void setId(long id) {

		this.id = id;

	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

 

==================================================================

 

package beantest;

/*
 * Java bean注意标准
 */
public class Address {

	private String zipCode;

	private String addr;

	private String city;

	private String country;

	public Address() {

	}

	public Address(String zipCode, String addr, String city, String country) {

		this.zipCode = zipCode;

		this.addr = addr;

		this.city = city;

		this.country = country;

	}

	public String getAddr() {

		return addr;

	}

	public void setAddr(String addr) {

		this.addr = addr;

	}

	public String getCity() {

		return city;

	}

	public void setCity(String city) {

		this.city = city;

	}

	public String getCountry() {

		return country;

	}

	public void setCountry(String country) {

		this.country = country;

	}

	public String getZipCode() {

		return zipCode;

	}

	public void setZipCode(String zipCode) {

		this.zipCode = zipCode;

	}
}

 

 

 

分享到:
评论

相关推荐

    BeanMapper:比BeanUtils.copyProperties更快

    一个BeanUtils.copyProperties的小型快速替代。 起因 由于BeanUtils(Spring或Apache Commons)的copyProperties实现是利用反射实现的,它在大量调用时具有比较严重的性能问题。 BeanMapper通过javassist类库实现在...

    org.apache.commons.beanutils.jar

    1. **属性操作**:BeanUtils提供了`getProperty()`和`setProperty()`方法,用于获取和设置JavaBean对象的属性值。这些方法通过反射机制动态地解析属性名,从而无需硬编码getter和setter方法的调用。 2. **类型转换*...

    commons-beanutils.jar.zip

    2. **动态属性访问**:通过BeanUtils.getProperty()和BeanUtils.setProperty(),我们可以动态地获取或设置Bean的任意属性,即使这些属性不存在于源代码中。 3. **类型转换**:BeanUtils.convertType()能够自动进行...

    commons-beanutils-1.8.0.jar beanutils.jar beanutils.jar工具包

    commons-beanutils-1.8.0.jar beanutils.jar beanutils.jar工具包

    Beanutils基本用法.doc

    - `BeanUtils.setProperty(myBean, "code", "someValue")`。该语句可以动态地为名为`code`的属性设置值。 - 对于嵌套对象的属性设置同样适用。 #### 三、BeanComparator动态排序 BeanComparator是Beanutils提供的...

    commons-beanutils.jar

    标题中的"commons-beanutils.jar"就是这个库的实现文件,用于简化JavaBean对象的属性操作。当我们在处理JSON数据并尝试将这些数据绑定到Java对象时,可能会遇到因缺少这个库而导致的问题。 首先,Apache Commons ...

    json用到的jar包(commons-beanutils.jar、commons-collections-3.1.jar等)

    json相关jar包(不使用springmvc开发时)。其中包含(commons-beanutils.jar、commons-collections-3.1.jar、commons-lang-2.6.jar、commons-logging.jar、ezmorph-1.0.6.jar、json-lib-2.2.3-jdk15.jar)

    commons.jar +commons-beanutils.jar

    标题中的"commons.jar +commons-beanutils.jar"指的是Apache Commons项目中的两个核心组件:commons.jar和commons-beanutils.jar。Apache Commons是Java编程语言中的一系列小型实用程序库,为开发人员提供了各种常见...

    commons-beanutils.jar+commons-logging.jar

    Apache提供的这个beanutils包极大方便了javabean的 操作。包含了最新的commons-beanutils-1.9.3.jar和api文档,以及其依赖的commons-logging-1.2.jar包

    commons-beanutils.jar、commons-logging.jar两个包

    beanUtils 方便访问javaBean 附带支持框架 logging jar包,Apache提供的这个beanutils包极大方便了javabean的 操作。包含了最新的commons-beanutils-1.9.3.jar,以及其依赖的commons-logging-1.2.jar包

    commons-beanutils.jar下载

    commons-beanutils.jar commons-beanutils.jar

    JSF开发包:commons-beanutils.jar+commons-collections.jar+commons-digester.jar+jsf-api.jar+jsf-impl.jar+jstl.jar+standard.jar

    JSF开发所必需包:花了很长时间才收集好,很费时,现已收集好,何不分享给大家,让大家节省...commons-beanutils.jar commons-collections.jar commons-digester.jar jsf-api.jar jsf-impl.jar jstl.jar standard.jar

    org.apache.commons.beanutils.BeanUtils实例

    此为BeanUtils的实例。其中apache的包有一个小的BUG已在其中说明。

    java Beanutils.copyProperties( )用法详解

    BeanUtils.copyProperties() 方法和 PropertyUtils.copyProperties() 方法的主要区别是,BeanUtils.copyProperties() 方法会进行类型转换,而 PropertyUtils.copyProperties() 方法不会。因此,BeanUtils....

    commons-beanutils.jar.zip(173 k)

    commons-beanutils.jar.zip(173 k)commons-beanutils.jar.zip(173 k)commons-beanutils.jar.zip(173 k) jdbc 专用

Global site tag (gtag.js) - Google Analytics