大家好,又见面了,我是你们的朋友全栈君。
最近在学习网站开发,在后端获取网站请求数据的时候用到了BeanUtils.populate()方法,具体用法是:
BeanUtils.populate(object obj, Map<String, String[]> map);
于是我就在想这个方法是怎么把map中的数据封装到obj对象里的。打开源码看,看别人写的代码是真难受,看了半天还是没看懂。上网搜了一下,发现多数都是在讲用法,关于方法的实现过程一部分人是说obj对象里要有名称与map中key对应的成员变量,另一部分说是要有与key对应的set方法,讲得都不太明白。
自己动手,丰衣足食。写几个类测试下,上代码。先构建4个Student对象:
StudentA.java
/* * 有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
/* * 有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
/* * 无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
/* * 有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
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();
}
}
测试结果:
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