BeanUtils工具的学习与介绍

什么是javabean呢?

简单来说,就是存粹的java对象,每个属性都提供对应的setXxx,getXxx方法。

下面是apache common beanutils对javabean的解释

  • As a necessary consequence of having a no-arguments constructor, configuration of your bean‘s behavior must be accomplished separately from its instantiation. This is typically done by defining a set of properties of
    your bean, which can be used to modify its behavior or the data that the bean represents. The normal convention for property names is that they start with a lower case letter, and be comprised only of characters that are legal in a Java identifier.
  • Typically, each bean property will have a public getter and setter method that are used to retrieve or define the property‘s value, respectively. The JavaBeans Specification defines a design
    pattern for these names, using get or set as the prefix for the property name with it‘s first character capitalized

这里的意思就是:javabean需要一个无参构造器,每个属性,都有一个public 的setXxx、getXxx方法。

下面看看一个具体的列子

public class Employee {

	private String address;
	private String firstName;
	private String lastName;
	private Employee employee;
	private List<String> privilegeIdList;
	private Map<String, String> theMap;

	public Map<String, String> getTheMap() {
		return theMap;
	}

	public void setTheMap(Map<String, String> theMap) {
		this.theMap = theMap;
	}

	public List<String> getPrivilegeIdList() {
		return privilegeIdList;
	}

	public void setPrivilegeIdList(List<String> privilegeIdList) {
		this.privilegeIdList = privilegeIdList;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public String getAddress() {
		return address;
	}

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

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		return "Employee [address=" + address + ", firstName=" + firstName
				+ ", lastName=" + lastName + ", employee=" + employee
				+ ", privilegeIdList=" + privilegeIdList + ", theMap=" + theMap
				+ "]";
	}
}

所以在开发中,如果我们得到一个Class对象,就可以通过java的反射来得到这个Class对象的相关属性。通常来书,一个Class对象的属性就是:还有哪些属性,那些方法,那些构造器等内容。具体的大家可以查阅相关资料。

由于使用java的反射机制不容易使用,所以我们有必要使用开源技术来访问这些内容。下面介绍BeanUtils工具,先大概浏览下BeanUtils api,如下面截图

通过截图,我们可以发现,这个类提供了多个静态方法来操作bean对象。这些方法主要有:

  • 设置一个对象的属性
  • 获取一个对象的属性
  • 复制一个对象属性给另外一个对象
  • 获取对象的描述信息

这个api接口极大简便了我们队java对象的操作能力。下面是具体的测试代码:

<span style="font-family:SimSun;font-size:18px;">/*
 * Licensed to Luohong Software.
 */
package embedding;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

/**
 * how to use the BeanUtils to set property
 * in the web, we can get the property, and then put the parameter to a map.
 * at finally,put the property map to the obj by using BeanUtils.popualte()
 *
 * */
public class Employee {

	private String address;
	private String firstName;
	private String lastName;
	private Employee employee;
	private List<String> privilegeIdList;
	private Map<String, String> theMap;

	public Map<String, String> getTheMap() {
		return theMap;
	}

	public void setTheMap(Map<String, String> theMap) {
		this.theMap = theMap;
	}

	public List<String> getPrivilegeIdList() {
		return privilegeIdList;
	}

	public void setPrivilegeIdList(List<String> privilegeIdList) {
		this.privilegeIdList = privilegeIdList;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public String getAddress() {
		return address;
	}

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

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		return "Employee [address=" + address + ", firstName=" + firstName
				+ ", lastName=" + lastName + ", employee=" + employee
				+ ", privilegeIdList=" + privilegeIdList + ", theMap=" + theMap
				+ "]";
	}

	public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException {
		Employee employee = new Employee();

		Map<String, Object> empMap = new HashMap<String, Object>();
		empMap.put("address", "广东河源龙川");
		empMap.put("firstName", "骆");
		empMap.put("lastName", "Hong");
		empMap.put("employee", new Employee());  //测试对象setXxx、getXxx

		List<String> privilegeIdList = new ArrayList<String>();
		privilegeIdList.add("hello");
		privilegeIdList.add("hello");
		privilegeIdList.add("hello");
		privilegeIdList.add("hello");
		empMap.put("privilegeIdList", privilegeIdList);  //测试属性为List的setXxx、getXxx

		Map<String, String> theMap = new HashMap<String, String>();
		theMap.put("hello", "value");
		theMap.put("hello12", "value");
		theMap.put("hello2", "value");
		theMap.put("hello3", "value");

		empMap.put("theMap", theMap);  //测试属性为map的setXxx、getXxx

		//将map里面的内容,set进对象里面,前提是属性名与key相同
		BeanUtils.populate(employee, empMap);

		//复制
		Employee copyEmployee = (Employee) BeanUtils.cloneBean(employee);
		System.out.println(copyEmployee);//

		//测试复制方法使用的是浅度复制
		copyEmployee.getPrivilegeIdList().add("你好,世界");
		String value = BeanUtils.getNestedProperty(copyEmployee, "theMap(hello12)");
		System.out.println(value);

		//获取一个bean的描述,使用map来存储
		Map<String,String> map = BeanUtils.describe(employee);
		System.out.println(map);

		System.out.println(employee);
	}
}</span>

输出结果:

<span style="font-family:SimSun;font-size:18px;">Employee [address=广东河源龙川, firstName=骆, lastName=Hong, employee=Employee [address=null, firstName=null, lastName=null, employee=null, privilegeIdList=null, theMap=null], privilegeIdList=[hello, hello, hello, hello], theMap={hello=value, hello12=value, hello2=value, hello3=value}]
value
{lastName=Hong, address=广东河源龙川, class=class embedding.Employee, employee=Employee [address=null, firstName=null, lastName=null, employee=null, privilegeIdList=null, theMap=null], firstName=骆, privilegeIdList=hello, theMap={hello=value, hello12=value, hello2=value, hello3=value}}
Employee [address=广东河源龙川, firstName=骆, lastName=Hong, employee=Employee [address=null, firstName=null, lastName=null, employee=null, privilegeIdList=null, theMap=null], privilegeIdList=[hello, hello, hello, hello, 你好,世界], theMap={hello=value, hello12=value, hello2=value, hello3=value}]
</span>

通过BeanUtils源码,我们可以发现,BeanUtils使用了如下的框架,来实现这个复杂操作

ConverUtils主要负责将值进行转换,PropertyUtils主要负责取出bean的相关属性。其中在Converter中,使用了策略设计模式来解决类型转换。

总结:通过学习和研究,其实开源源码的难度,有时候并没有我们想象中的那么困难。在研究他人代码时,我们不能囫囵吐枣的乱看,需要系统的,从高处抽象的看待。并且,对于类过多的情况,我们可以采用画UML等方式来组织。

时间: 2024-08-04 02:00:43

BeanUtils工具的学习与介绍的相关文章

java学习--基础知识进阶第十三天--反射机制的概述和字节码对象的获取方式、反射操作构造方法、成员方法、成员属性、JavaBean的概述&amp;BeanUtils的使用、自定义BeanUtils工具类

今日内容介绍 u 反射机制的概述和字节码对象的获取方式 u 反射操作构造方法.成员方法.成员属性 u JavaBean的概述&BeanUtils的使用 u 自定义BeanUtils工具类 第1章 反射机制概述.字节码对象的获取方式.反射操作构造方法.成员方法.成员属性 1.1 反射机制的概述和字节码对象的获取方式 1.1.1 反射介绍 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法,对于任意一个对象,都能够调用它的任意一个方法        这种动态获取的以及动

工具fiddler学习

1:Fiddler 是以代理web服务器的形式工作的,它使用代理地址:127.0.0.1, 端口:8888. 当Fiddler会自动设置代理.能支持HTTP代理的任意程序的数据包都能被Fiddler嗅探到,Fiddler的运行机制其实就是本机上监听8888端口的HTTP代理. Fiddler2启动的时候默认IE的代理设为了127.0.0.1:8888,而其他浏览器是需要手动设置的,所以将Firefox的代理改为127.0.0.1:8888就可以监听数据了. Firefox 上通过如下步骤设置代理

BeanUtils工具

什么是BeanUtils工具 BeanUtils工具是一种方便我们对JavaBean进行操作的工具,是Apache组织下的产品. BeanUtils工具一般可以方便javaBean的哪些操作? 1)beanUtils 可以便于对javaBean的属性进行赋值. 2)beanUtils 可以便于对javaBean的对象进行赋值. 3)beanUtils可以将一个MAP集合的数据拷贝到一个javabean对象中. BeanUtils的使用 使用beanUtils按照以下步骤~ 前提:约定前提: 参数

【百度地图API】如何在地图上添加标注?——另有:坐标拾取工具+打车费用接口介绍

原文:[百度地图API]如何在地图上添加标注?--另有:坐标拾取工具+打车费用接口介绍 摘要: 在这篇文章中,你将学会,如何利用百度地图API进行标注.如何使用API新增的打车费用接口. ------------------------------------------------------------------------------------------------------- 哇,好久没有上来了.主要是因为最近工作繁忙,加上休息时间被各种排练.社团活动占满,导致木有更新此博客.

这是个非常强悍的beanutils工具

注:需要的jar包两个(缺一不可): commons-beanutils-1.8.3.jar:commons-logging-1.1.1.jar package cn.test1; import java.util.Date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Enumeration; import java.util.UUID; import javax.se

Apache commons lang工具类学习笔记(2)--StringUtils

StringUtils工具类具有对String具有简单而强大的处理能力,从检查空串到分割字符串,到生成格式化的字符串,使用都很方便简洁,能减少很多代码量; 详细的使用方法可以参考下面的例子或者官方的API(http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#isAlpha(java.lang.CharSequence)) packa

JDBC--使用beanutils工具类操作JavaBean

1.在JavaEE中,Java类的属性通过getter,setter来定义: 2.可使用BeanUtils工具包来操作Java类的属性: --Beanutils是由Apache公司开发,能够方便对Bean类进行简便的操作 --涉及到的包: (1)   BeanUtils相关包 commons-beanutils-1.8.3.jar commons-beanutils-1.8.3-javadoc.jar commons-beanutils-1.8.3-javadoc.jar commons-bea

ORCHARD学习教程-介绍

ORCHARD 是什么? Orchard 是由微软公司创建,基于 ASP.NET MVC 技术的免费开源内容管理系统: 可用于建设博客.新闻门户.企业门户.行业网站门户等各种网站 简单易用的后台界面 性能稳定,功能齐全 热拔插模块化架构提供超强可扩展性 BSD 协议授权,可用于商业闭源项目 ORCHARD 1.8.1 发布! 北京时间 2014 年 3 月 29 日 Orchard 团队发布了 Orchard 1.8 正式版.这次更新主要有如下几个特点:迁移到 .NET 4.5.升级到新的 AS

HTML DOM学习------简单介绍

DOM:w3c文档对象模型. 是中立于平台和语言的接口,它允许程序和脚本动态地访问和更新文档的内容.结构和样式. HTML DOM :定义了所有 HTML 元素的对象和属性,以及访问它们的方法. 换言之,HTML DOM 是关于如何获取.修改.添加或删除 HTML 元素的标准. DOM节点:HTML文档中所有内容都是节点: 整个文档是一个文档节点: 每个HTML元素是一个元素节点: HTML元素内的文本是一个文本节点: 每个HTML属性是一个属性节点: 注释是注释节点: HTML DOM将HTM