前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BeanUtils.populate 使用笔记

BeanUtils.populate 使用笔记

作者头像
全栈程序员站长
发布2022-09-14 11:41:40
2900
发布2022-09-14 11:41:40
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

最近在学习网站开发,在后端获取网站请求数据的时候用到了BeanUtils.populate()方法,具体用法是:

代码语言:javascript
复制
BeanUtils.populate(object obj, Map<String, String[]> map);

于是我就在想这个方法是怎么把map中的数据封装到obj对象里的。打开源码看,看别人写的代码是真难受,看了半天还是没看懂。上网搜了一下,发现多数都是在讲用法,关于方法的实现过程一部分人是说obj对象里要有名称与map中key对应的成员变量,另一部分说是要有与key对应的set方法,讲得都不太明白。

自己动手,丰衣足食。写几个类测试下,上代码。先构建4个Student对象:

StudentA.java

代码语言:javascript
复制
/* * 有r成员,方法各自对应 */
public class StudentA { 
   
	private String name;
	private String number;
	private String sex;
	private String r_name;
	private String r_number;
	private String r_sex;
	
	
	
	public String getName() { 
   
		return name;
	}
	public void setName(String name) { 
   
		this.name = name;
	}
	public String getNumber() { 
   
		return number;
	}
	public void setNumber(String number) { 
   
		this.number = number;
	}
	public String getSex() { 
   
		return sex;
	}
	public void setSex(String sex) { 
   
		this.sex = sex;
	}

	public String getR_name() { 
   
		return r_name;
	}
	public void setR_name(String r_name) { 
   
		this.r_name = r_name;
	}
	public String getR_number() { 
   
		return r_number;
	}
	public void setR_number(String r_number) { 
   
		this.r_number = r_number;
	}
	public String getR_sex() { 
   
		return r_sex;
	}
	public void setR_sex(String r_sex) { 
   
		this.r_sex = r_sex;
	}
	
	
	public StudentA() { 
   
		super();
	}
	
	public StudentA(String name, String number, String sex) { 
   
		super();
		this.name = name;
		this.number = number;
		this.sex = sex;
	}
	
}

StudentB.java

代码语言:javascript
复制
/* * 有r成员,但r方法对应到不带r成员上 */
public class StudentB { 
   
	private String name;
	private String number;
	private String sex;
	private String r_name;
	private String r_number;
	private String r_sex;
	
	
	
	public String getName() { 
   
		return name;
	}
	public void setName(String name) { 
   
		this.name = name;
	}
	public String getNumber() { 
   
		return number;
	}
	public void setNumber(String number) { 
   
		this.number = number;
	}
	public String getSex() { 
   
		return sex;
	}
	public void setSex(String sex) { 
   
		this.sex = sex;
	}
	
	public String getR_name() { 
   
		return name;
	}
	public void setR_name(String r_name) { 
   
		this.name = r_name;
	}
	public String getR_number() { 
   
		return number;
	}
	public void setR_number(String r_number) { 
   
		this.number = r_number;
	}
	public String getR_sex() { 
   
		return sex;
	}
	public void setR_sex(String r_sex) { 
   
		this.sex = r_sex;
	}
	
	public String sysNa() { 
   
		return r_name;
	}
	public String sysNu() { 
   
		return r_number;
	}
	public String sysSe() { 
   
		return r_sex;
	}
	
	
	public StudentB() { 
   
		super();
	}
	
	public StudentB(String name, String number, String sex) { 
   
		super();
		this.name = name;
		this.number = number;
		this.sex = sex;
	}

}

StudentC.java

代码语言:javascript
复制
/* * 无r成员,但有r方法且对应到不带r成员上 */
public class StudentC { 
   
	private String name;
	private String number;
	private String sex;
	/* * private String r_name; private String r_number; private String r_sex; */
	
	
	
	public String getName() { 
   
		return name;
	}
	public void setName(String name) { 
   
		this.name = name;
	}
	public String getNumber() { 
   
		return number;
	}
	public void setNumber(String number) { 
   
		this.number = number;
	}
	public String getSex() { 
   
		return sex;
	}
	public void setSex(String sex) { 
   
		this.sex = sex;
	}
	public String getR_name() { 
   
		return name;
	}
	public void setR_name(String r_name) { 
   
		this.name = r_name;
	}
	public String getR_number() { 
   
		return number;
	}
	public void setR_number(String r_number) { 
   
		this.number = r_number;
	}
	public String getR_sex() { 
   
		return sex;
	}
	public void setR_sex(String r_sex) { 
   
		this.sex = r_sex;
	}
	
	
	public StudentC() { 
   
		super();
	}
	
	public StudentC(String name, String number, String sex) { 
   
		super();
		this.name = name;
		this.number = number;
		this.sex = sex;
	}

}

StudentD.java

代码语言:javascript
复制
/* * 有r成员,但没有对应的set和get的r方法 */
public class StudentD { 
   
	private String name;
	private String number;
	private String sex;
	private String r_name;
	private String r_number;
	private String r_sex;
	
	
	public String getName() { 
   
		return name;
	}
	public void setName(String name) { 
   
		this.name = name;
	}
	public String getNumber() { 
   
		return number;
	}
	public void setNumber(String number) { 
   
		this.number = number;
	}
	public String getSex() { 
   
		return sex;
	}
	public void setSex(String sex) { 
   
		this.sex = sex;
	}
	
	public String sysNa() { 
   
		return r_name;
	}
	public String sysNu() { 
   
		return r_number;
	}
	public String sysSe() { 
   
		return r_sex;
	}
	
	
	public StudentD() { 
   
		super();
	}
	public StudentD(String name, String number, String sex) { 
   
		super();
		this.name = name;
		this.number = number;
		this.sex = sex;
	}
	
}

BeanUtils.populate() 方法测试类:

BeanUtilsTest.java

代码语言:javascript
复制
public class BeanUtilsTest { 
   

	public static void main(String[] args) { 
   
		StudentA studenta = new StudentA();
		StudentB studentb = new StudentB();
		StudentC studentc = new StudentC();
		StudentD studentd = new StudentD();
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("r_name", "xiaoming");
		map.put("r_number", "1600890105");
		map.put("r_sex", "boy");
		
		//map测试代码块
		{ 
   	// 遍历Map是否有值
			System.out.println("Map遍历开始:{");
			for (Entry<String, String> entry : map.entrySet()) { 
   
				System.out.println(" " + entry.getKey() + ":" + entry.getValue());
			}
			System.out.println(" }\n");
		}

		//使用BeanUtils将 map的值封装到 student对象
		try { 
   
			BeanUtils.populate(studenta, map);
			BeanUtils.populate(studentb, map);
			BeanUtils.populate(studentc, map);
			BeanUtils.populate(studentd, map);
		} catch (IllegalAccessException e) { 
   
			e.printStackTrace();
		} catch (InvocationTargetException e) { 
   
			e.printStackTrace();
		}
		
		//检测封装结果
		System.out.println("stundenta对象(成员与各自方法对应):");
		System.out.println(" 姓名:" + studenta.getName());
		System.out.println(" 学号:" + studenta.getNumber());
		System.out.println(" 性别:" + studenta.getSex());
		System.out.println(" r_name:" + studenta.getR_name());
		System.out.println(" r_number:" + studenta.getR_number());
		System.out.println(" r_sex:" + studenta.getR_sex());
		System.out.println();
		
		System.out.println("stundentb对象(有r成员,但r方法对应到不带r成员上):");
		System.out.println(" 姓名:" + studentb.getName());
		System.out.println(" 学号:" + studentb.getNumber());
		System.out.println(" 性别:" + studentb.getSex());
		System.out.println(" r_name:" + studentb.sysNa());
		System.out.println(" r_number:" + studentb.sysNu());
		System.out.println(" r_sex:" + studentb.sysSe());
		System.out.println();
		
		System.out.println("stundentc对象(无r成员,但有r方法且对应到不带r的成员上):");
		System.out.println(" 姓名:" + studentc.getName());
		System.out.println(" 学号:" + studentc.getNumber());
		System.out.println(" 性别:" + studentc.getSex());
		System.out.println(" r_name:" + studentc.getR_name());
		System.out.println(" r_number:" + studentc.getR_number());
		System.out.println(" r_sex:" + studentc.getR_sex());
		System.out.println();

		System.out.println("stundentd对象(有r成员,但没有r方法):");
		System.out.println(" 姓名:" + studentc.getName());
		System.out.println(" 学号:" + studentc.getNumber());
		System.out.println(" 性别:" + studentc.getSex());
		System.out.println(" r_name:" + studentd.sysNa());
		System.out.println(" r_number:" + studentd.sysNu());
		System.out.println(" r_sex:" + studentd.sysSe());
		System.out.println();
	}

}

测试结果:

代码语言:javascript
复制
Map遍历开始:{ 
   
   r_number:1600890105
   r_sex:boy
   r_name:xiaoming
 }

stundenta对象(成员与各自方法对应):
  姓名:null
  学号:null
  性别:null
 r_name:xiaoming
 r_number:1600890105
 r_sex:boy

stundentb对象(有r成员,但r方法对应到不带r成员上):
  姓名:xiaoming
  学号:1600890105
  性别:boy
 r_name:null
 r_number:null
 r_sex:null

stundentc对象(无r成员,但有r方法且对应到不带r的成员上):
  姓名:xiaoming
  学号:1600890105
  性别:boy
 r_name:xiaoming
 r_number:1600890105
 r_sex:boy

stundentd对象(有r成员,但没有r方法):
  姓名:null
  学号:null
  性别:null
 r_name:null
 r_number:null
 r_sex:null

结论:由测试结果可见,BeanUtils.populate() 方法的实现过程是通过 obj对象中与map中 key对应的 set方法来实现封装,与obj对象中是否存在名称与 map中key对的成员变量无关。当然,这只是针对BeanUtils.populate(object obj, Map<String, String[]> map)的用法的结论。

仔细一想,对象的所有成员变量都是私有的,在对象范围外根本无法在直接对成员变量赋值,也只有用对应的set方法,真是为自己智商感到捉急。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158765.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档