BeanUtils是Apache commons组件的成员之一,主要用于简化JavaBean封装数据的操作。它可以给JavaBean封装一个字符串数据,也可以将一个表单提交的所有数据封装到JavaBean中。BeanUtils工具常用工具类有两个:BeanUtils、ConvertUtils。BeanUtils用于封装数据,ConvertUtils用于处理类型转换,这里只谈BeanUtils.
BeanUtils有三个常用方法:
//User对象类
publicclass User implements Serializable{
private static final long serialVersionUID= 1L;
private int id;
private String username;
private String password;
private String[] hobbies;
public User(){}
public String[] getHobbies(){
return hobbies;
}
public void setHobbies(String[] hobbies){
this.hobbies = hobbies;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id +", username=" + username + ", password=" + password +", hobbies="
+ Arrays.toString(hobbies) +"]"; //这里用到数组工具类Arrays的方法toString(数组)
}
}
方法一:setProperty(Object
obj,String name,Object value)括号中参数obj: JavaBean的对象;name: JavaBean中的成员变量名;value:成员变量的值;代码如下:
@Test
publicvoid demo01()throws Exception{
User user = new User();
BeanUtils.setProperty(user, "id",3);
BeanUtils.setProperty(user,"username", "Tom");
BeanUtils.setProperty(user,"password", "Jerry");
System.out.println(user);
}
方法二:getProperty(Object
obj,String name)返回值为String,括号中参数obj: JavaBean的对象;name: JavaBean中的成员变量名
@Test
publicvoid demo02()throws Exception{
User user = new User();
user.setId(5);
user.setUsername("jack");
user.setPassword("rose");
String id = BeanUtils.getProperty(user,"id");
String username =BeanUtils.getProperty(user, "username");
String password = BeanUtils.getProperty(user,"password");
System.out.println(id+""+username+""+password);
}
方法三:populate(Object
bean,Map properties)括号中参数bean:
JavaBean对象;properties: Map键值对集合
@Test
publicvoid demo03()throws Exception{
User user = new User();
//创建Map集合,键是变量名,值是String数组
Map properties =new HashMap();
propertie.put("id", newString[]{"5"});
propertie.put("username", newString[]{"jack"});
propertie.put("password", newString[]{"rose"});
propertie.put("hobbies", newString[]{"篮球","跑步","跳绳"});
BeanUtils.populate(user, propertie);
System.out.println(user);
}
下来我们要做的是自定义BeanUtils工具类,将它封装在方法中.
版本一:
publicclass Utils {
public static void main(String[] args) {
User user = new User();
Map properties =new HashMap();
properties.put("id", newString[]{"007"});
properties.put("username",new String[]{"jack"});
properties.put("password",new String[]{"rose"});
properties.put("hobbies", newString[]{"吃饭","吃鱼","手游"});
populate(user, properties);
System.out.println(user);
}
/*
*静态方法,传递对象,和Map集合
*Map集合中的键值对,存储到JavaBean对象
*/
public static void populate(Objectobj,Map properties){
try{
BeanUtils.populate(obj, properties);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
版本二:调用者不用创建对象,方法中建立好对象(反射技术),然后返回
publicclass Utils_2 {
public static void main(String[] args) {
Map properties =new HashMap();
properties.put("id", newString[]{"1"});
properties.put("username",new String[]{"jack"});
properties.put("password",new String[]{"rose"});
properties.put("hobbies", newString[]{"吃饭","吃鱼","手游"});
User obj =(User)populate(User.class,properties);
System.out.println(obj);
}
/*
*静态方法,Map集合
*Map集合中的键值对,存储到JavaBean对象
*/
public static Object populate(Class clazz,Map properties){
try{
//反射方式,创建对象
Object obj = clazz.newInstance();
BeanUtils.populate(obj, properties);
return obj;
}catch(Exception ex){
//一旦数据操作失败,停止运行,修改代码
throw new RuntimeException("数据注入失败");
}
}
}
版本三:调用者不用创建对象,方法中建立好对象(反射技术),然后返回;增加泛型技术,避免调用者的类型强转,调用者传递什么类型,返回什么类型的对象
publicclass Utils_3{
public static void main(String[] args) {
Map properties =new HashMap();
properties.put("id", newString[]{"1"});
properties.put("username",new String[]{"jack"});
properties.put("password",new String[]{"rose"});
properties.put("hobbies", newString[]{"吃饭","吃鱼","手游"});
User u = populate(User.class,properties);
System.out.println(u);
}
/*
*静态方法,Map集合
*Map集合中的键值对,存储到JavaBean对象
*/
publicstatic T populate(Class clazz, Map properties){
try {
T t = clazz.newInstance();
BeanUtils.populate(t, properties);
return t;
} catch (Exception e) {
throw new RuntimeException("数据注入失败");
}
}
}
下面使用一个案例结合反射和xml对上面的内容做个加强:
要求:读取XML中的配置文件信息,使用BeanUtils工具类创建JavaBean对象,将XML中的数据保存到JavaBean类的属性中
packagecom.demo.bean;
publicclass User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" +username + ", password=" + password + "]";
}
}
packagecom.demo.bean;
publicclass Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" +name + ", age=" + age + "]";
}
}
publicclass TestJavaBean {
@Test
public void demo()throws Exception{
//SaxReader核心类对象
SAXReader sax = new SAXReader();
//读取xml文档,生成Document对象
Document document =sax.read("data.xml");//文件位置在工程根目录下
//document对象的方法,获取跟标签元素getRootElement()
Element rootElement =document.getRootElement();
//rootElement对象方法elements()获取子标签bean
List beanListElement =rootElement.elements();
for(Element beanElement :beanListElement){
//beanElement标签对象的方法attribValue()获取bean标签的属性className的值
String className =beanElement.attributeValue("className");
//className配置好的JavaBean类的类名,反射方式创建对象
Class clazz =Class.forName(className);
Object obj = clazz.newInstance();
//beanElement bean标签对象方法elements()获取子标签property
List proListElement= beanElement.elements();
for(Element proElement :proListElement){
//proElement子标签propertry,对象的方法attribValue标签的属性值
String name =proElement.attributeValue("name");
String value =proElement.attributeValue("value");
//属性值name,JavaBean对象中的成员变量名,属性值value,成员变量的值
//BeanUtils工具类的方法setProperty
BeanUtils.setProperty(obj,name, value);
}
System.out.println(obj);
}
}
}