`

Java基础六:apache的Beanutils简单使用对JavaBean操作的封装

阅读更多
/**
 * apache的Beanutils简单使用
 * 
 * @author 张明学
 * 
 */
public class BeanUtilsStudy {

	public static void main(String[] args) throws Exception {
		PersonBean pbean = new PersonBean("java", 12);
		pbean.setP("P属性");
		// 定义几个属性名称
		String propertyP = "p";
		String propertyName = "name";
		String propertyAge = "age";
		Object returnValue = null;
		// **************getProperty与setProperty方法:获取与设置JavaBean的属性**************
		// 获取p属性值
		returnValue = BeanUtils.getProperty(pbean, propertyP);
		System.out.println(returnValue);
		// 获取name属性值
		returnValue = BeanUtils.getProperty(pbean, propertyName);
		System.out.println(returnValue);
		// 设置p属性值
		BeanUtils.setProperty(pbean, propertyP, "C++");
		System.out.println(pbean.getP());
		// 设置age属性值
		BeanUtils.setProperty(pbean, propertyAge, 24);
		System.out.println(pbean.getAge());
		// 可以自动帮我们进行类型转换
		BeanUtils.setProperty(pbean, propertyAge, "25");
		System.out.println(pbean.getAge());
		// birthday类型的java.util.Date类,下面这样写在它在定义的是必须要实例化birthday
		// java.util.Date有一个setTime()方法可以看作JavaBean
		BeanUtils.setProperty(pbean, "birthday.time", new Date().getTime());
		System.out.println(pbean.getBirthday());

		// **************copyProperties方法:将一个JavaBean的属性copy到另一个JavaBean中**************
		// PersonBean2是PersonBean的子类
		PersonBean2 pb2 = new PersonBean2();
		// 将pbean的属性值copy到pb2属性
		BeanUtils.copyProperties(pb2, pbean);
		System.out.println(pb2.getName());

		// **************describe方法:将javaBean转换成Map对象************** \\
		Map beanMap = BeanUtils.describe(pbean);
		for (Object key : beanMap.keySet()) {
			System.out.println(key + "=" + beanMap.get(key));
		}

		// **************populate方法:将Map中的值设置到javaBean对象************** \\
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", ".NET");
		map.put("age", 32);
		map.put("p", "PPP");
		BeanUtils.populate(pbean, map);
		System.out.println(pbean.getName() + "\t" + pbean.getAge() + "\t"
				+ pbean.getP());

		// 还有一个PropertyUtils
		returnValue = PropertyUtils.getProperty(pbean, propertyName);
		System.out.println(returnValue);
		PropertyUtils.setProperty(pbean, propertyAge, 21);
		System.out.println(pbean.getAge());
		// PropertyUtils.setProperty(pbean, propertyAge, "21");不能自动进行类型转换

	}
}

 其中用的JavaBean如下:

public class PersonBean {
	private String name;
	private int age;
	private Date birthday = new Date();
	private String x;
	public PersonBean() {
	}
	public PersonBean(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public String getP() {
		return x;
	}
	public void setP(String p) {
		this.x = p;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

 

public class PersonBean2 extends PersonBean {
	private String address;

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

}

 

分享到:
评论
2 楼 slaser 2010-05-31  
这个东西也算java基础?
1 楼 mp_juan 2010-05-31  
建议楼主自己看java.lang.reflect包的API,然后使用reflect自己实现..

相关推荐

    commons-beanutils:Apache Commons Beanutils

    Apache Commons BeanUtils Apache Commons BeanUtils为反射和自省提供了一个易于使用但灵活的包装器。文献资料可以在上找到更多信息。 可以浏览 。 与Apache Commons BeanUtils的用法有关的问题应张贴到。在哪里可以...

    javaenum源码-BeanUtils:BeanUtils库是一个Javabean复制工具,功能强大,性能高

    java枚举源码BeanUtils 这个 BeanUtils 库是一个 Java bean 复制实用程序,具有强大的功能和高性能。 Maven 使用 &lt;groupId&gt;com.github.yangtu222&lt;/groupId&gt; &lt;artifactId&gt;BeanUtils &lt;version&gt;1.0.11 特征:...

    BeanMapper:比BeanUtils.copyProperties更快

    由于BeanUtils(Spring或Apache Commons)的copyProperties实现是利用反射实现的,它在大量调用时具有比较严重的性能问题。 BeanMapper通过javassist类库实现在运行时直接生成字节码来转换类,在首次生成后,后续...

    org.apache.commons.beanutils.BeanUtils实例

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

    apache beanutils

    apache的beanutils包 自己用的不错 分享给大家。。。

    commons-beanutils-1.9.4-API文档-中文版.zip

    标签:beanutils、commons、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心...

    commons-beanutils-1.9.4-API文档-中英对照版.zip

    标签:beanutils、commons、jar包、java、中英对照文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请...

    net.sf.json jar包(全部依赖)

    commons-beanutils:Apache Commons BeanUtils 库,用于操作 JavaBeans。 commons-collections:Apache Commons Collections 库,提供了一些额外的 Java 集合类。 commons-logging:Apache Commons Logging 库,用于...

    commons-beanutils-1.8.3

    配置服务端所需要的jar文件,包括commons-logging commons-beanutils commons-lang ezmorph json-lib-2.4-jdk15 commons-collections-3.2.1。

    commons-beanutils-1.7.0-API文档-中英对照版.zip

    标签:beanutils、commons、jar包、java、API文档、中英对照版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    commons-beanutils-1.9.1-API文档-中文版.zip

    标签:commons、beanutils、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心...

    org.apache.commons.beanutils.jar

    org.apache.commons.beanutils.jar

    commons-beanutils-1.9.1解析javaBean方式

    Beanutils用了魔术般的反射技术,实现了很多夸张有用的功能,都是C/C++时代不敢想的。无论谁的项目,始终一天都会用得上它。我算是后知后觉了,第一回看到它的时候居然错过。 1.属性的动态getter,setter 2....

    apache BeanUtils

    apache BeanUtils

    commons-beanutils-1.7.0-API文档-中文版.zip

    标签:beanutils、commons、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心...

    BeanUtils工具

    BeanUtils工具是一种方便我们对JavaBean进行操作的工具,是Apache组织下的产品。beanUtils 可以便于对javaBean的属性、对象进行赋值,beanUtils可以将一个MAP集合的数据拷贝到一个javabean对象中。

    beanutils-1.9.3-bin

    BeanUtils是由Apache公司开发的针对操作JavaBean的工具包。 对于JavaBean,简单的来说,就是要有一个空参的构造器和对属性的getXXX方法和setXXX方法。 在由JDK提供的默认的API中,有java.beans包,里面有诸如...

    commons-beanutils-1.9.3-API文档-中英对照版.zip

    赠送jar包:commons-beanutils-1.9.3.jar; 赠送原API文档:commons-beanutils-1.9.3-javadoc.jar; 赠送源代码:commons-beanutils-1.9.3-sources.jar; 包含翻译后的API文档:commons-beanutils-1.9.3-javadoc-...

    Commons beanutils API.chm

    Commons beanutils API.chm apache Commons beanutils API

Global site tag (gtag.js) - Google Analytics