使用Apache的BeanUtils进行JavaBean的反射操作

本文介绍如何使用JavaBean和Apache的BeanUtils API进行操作,包括基本类型的转换和自定义转换器的注册,以及如何通过Map来填充Bean的属性。详细展示了通过BeanUtils设置属性值、支持的基本类型转换、注册自定义转换器进行复杂类型转换,并演示了对Map的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对JavaBean的操作,Java提供了一套自己的方法,但是用起来感觉不咋的,但是Apache自己出版了一套BeanUtilsAPI方法进行Bean操作,很牛逼,很好用,下面放上几个小例子,用来入门吧,以后慢慢研究,当然,等有用的时候。

当然,使用这个玩意需要去下载JAR包的,BeanUTils下载地址为点击打开链接,他需要另一个第三包,下载地址为点击打开链接,将他们导入到工程路径中

首先贴上JavaBean的代码

package com.bird.beanutils; import java.util.Date; /** * @use 一个简单的Java Bean * @author Bird * */ public class Person { public String name; private String sex; private String password; public int age; public Date birthday; public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }




下面是BeanUTils代码

package com.bird.beanutils; import java.lang.reflect.InvocationTargetException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConversionException; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; import org.junit.Test; /** * @use 使用Apache的BeanUtils操作JavaBean * @author Bird * */ @SuppressWarnings("unused") public class Demo1 { @Test public void test1() throws Exception{ Person p = new Person(); BeanUtils.setProperty(p, "name", "Bird");//使用BeanUtils设置name的值 System.out.println(p.getName()); } @Test public void test2() throws Exception{//BeanUtils支持8种基本类型之间的转换 Person p = new Person(); String name = "Bird"; String sex = "man"; String age = "19"; String password = "123";//由表单提交过来的都是String类型 BeanUtils.setProperty(p, "name", name); BeanUtils.setProperty(p, "sex", sex); BeanUtils.setProperty(p, "age", age);//虽然age是整形,但是BeanUtils可以转换赋值 BeanUtils.setProperty(p, "password", password); System.out.println(p.getAge()); System.out.println(p.getName()); System.out.println(p.getPassword()); System.out.println(p.getSex()); } @Test public void test3() throws Exception{//注册BeanUtils的转换器,进行转换其他非8种基本类型的类型 String birthday = "1992-01-01"; ConvertUtils.register(new Converter(){//匿名内部类,实现Converter接口 @SuppressWarnings("unchecked") public Object convert(Class type, Object value) { if(value == null){//如果传入值为空, return null; } if(!(value instanceof String)){//如果不是String类型,抛出异常 throw new ConversionException("不支持非String类型!!!"); } String str = (String) value; if(str.trim().equals("")){//如果没有值 return null; } SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); try { return df.parse(str); } catch (ParseException e) { throw new RuntimeException(e);//保持异常链表不断 } } }, Date.class);//注册转换器 Person p = new Person(); BeanUtils.setProperty(p, "birthday", birthday); System.out.println(p.getBirthday()); } @SuppressWarnings("unchecked") @Test public void test5() throws Exception{//对Map的操作 Map map = new HashMap(); map.put("age", "19"); Person bean = new Person(); BeanUtils.populate(bean, map);//用Map集合中的值填充Bean的值 System.out.println(bean.getAge()); } }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值