在Spring中,处理这个问题的核心是在org.springframework.beans包中。而其中最为核心的部分,则是BeanWrapper。BeanWrapper,顾名思义,就是bean的包装器。所以,它的主要工作,就是对任何一个bean,进行属性(包括内嵌属性)的设置和方法的调用。在BeanWrapper的默认实现类BeanWrapperImpl中,虽然代码较长,但完成的工作却是非常的集中的。 我们看看这个BeanWrapper是如何发挥运作的,假设我们有两个bean: public class Company {
private String name;
private Employee managingDirector; public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Employee getManagingDirector() {
return this.managingDirector;
}
public void setManagingDirector(Employee managingDirector) {
this.managingDirector = managingDirector;
}
} public class Employee {
private float salary; public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
} 然后我们使用BeanWrapper来调用这两个bean: Company c = new Company();
BeanWrapper bwComp = BeanWrapperImpl(c);
// setting the company name...
bwComp.setPropertyValue( "name ", "Some Company Inc. ");
// ... can also be done like this:
PropertyValue v = new PropertyValue( "name ", "Some Company Inc. ");
bwComp.setPropertyValue(v); // ok, let 's create the director and tie it to the company: //***************这段代码*****************
Employee jim = new Employee();
BeanWrapper bwJim = BeanWrapperImpl(jim);
bwJim.setPropertyValue( "name ", "Jim Stravinsky "); // <--怎么总觉得这里有错误啊?
bwComp.setPropertyValue( "managingDirector ", jim);
//**************************************** // retrieving the salary of the managingDirector through the company
Float salary = (Float)bwComp.getPropertyValue( "managingDirector.salary ");
==================================================
这是我在网上看到的一篇介绍spring的文章,不知道是写错了还是我没有看明白。
上面的代码里写完整的话是不是应该这样的啊:
Employee jim = new Employee();
BeanWrapper bwJim = BeanWrapperImpl(jim);
bwJim.setPropertyValue( "salary ",100.00);
bwComp.setPropertyValue( "name ", "Jim Stravinsky ");// <--那行代码是不是应该这样写才对啊?
bwComp.setPropertyValue( "managingDirector ", jim);
private String name;
private Employee managingDirector; public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Employee getManagingDirector() {
return this.managingDirector;
}
public void setManagingDirector(Employee managingDirector) {
this.managingDirector = managingDirector;
}
} public class Employee {
private float salary; public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
} 然后我们使用BeanWrapper来调用这两个bean: Company c = new Company();
BeanWrapper bwComp = BeanWrapperImpl(c);
// setting the company name...
bwComp.setPropertyValue( "name ", "Some Company Inc. ");
// ... can also be done like this:
PropertyValue v = new PropertyValue( "name ", "Some Company Inc. ");
bwComp.setPropertyValue(v); // ok, let 's create the director and tie it to the company: //***************这段代码*****************
Employee jim = new Employee();
BeanWrapper bwJim = BeanWrapperImpl(jim);
bwJim.setPropertyValue( "name ", "Jim Stravinsky "); // <--怎么总觉得这里有错误啊?
bwComp.setPropertyValue( "managingDirector ", jim);
//**************************************** // retrieving the salary of the managingDirector through the company
Float salary = (Float)bwComp.getPropertyValue( "managingDirector.salary ");
==================================================
这是我在网上看到的一篇介绍spring的文章,不知道是写错了还是我没有看明白。
上面的代码里写完整的话是不是应该这样的啊:
Employee jim = new Employee();
BeanWrapper bwJim = BeanWrapperImpl(jim);
bwJim.setPropertyValue( "salary ",100.00);
bwComp.setPropertyValue( "name ", "Jim Stravinsky ");// <--那行代码是不是应该这样写才对啊?
bwComp.setPropertyValue( "managingDirector ", jim);
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货