Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单、易用的API操作Bean的属性——BeanUtils,在Beanutil中可以直接进行类型的自动转换。

BeanUtil工具包下载:

1,登录http://commons.apache.org/beanutils/

2,  点击Download

3, 点击commons-beanutils-1.9.2-bin.zip(commons-logging-1.1.3-src)进行下载就OK了

使用BeanUtil

在项目中导入commons-beanutils-1.9.2.jar包即可(PS:把此jar包复制到项目的lib文件夹,右击包,选build path==>add to build path 显示奶瓶即可)

另外:commons-beanutils-1.9.2.jar 要与 commons-logging-1.1.3.Jar共用。

 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.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test; //使用beanUtils操纵bean属性(第三方)
public class Demo { /**
*
* @throws Exception
*
*/
@Test
public void test1() throws Exception{ Person p = new Person(); BeanUtils.setProperty(p, "name", "zero"); System.out.println(p.getName()); } @Test
public void test2() throws Exception{ String name = "aaaa";
String password = "123";
String age = "34"; Person p = new Person(); BeanUtils.setProperty(p, "name", name);//只支持8种基本数据类型
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age); System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()); } @Test
public void test3() throws Exception{ String name = "aaaa";
String password = "123";
String age = "34";
String birthday = "1900-1-1"; //为了让日期赋到bean的birthday属性上,给beanUtils注册一个日期转换
ConvertUtils.register(new Converter(){ @Override
public Object convert(Class type, Object value) { if(value==null){
return null;
}
if(!(value instanceof String)){
throw new ConversionException("only support 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, "name", name);//只支持8种基本数据类型
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "birthday", birthday); System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()+"*"+p.getBirthday()); } @Test
public void test4() throws Exception{ String name = "aaaa";
String password = "123";
String age = "34";
String birthday = "1998-1-1"; ConvertUtils.register(new DateLocaleConverter(), Date.class); Person p = new Person(); BeanUtils.setProperty(p, "name", name);//只支持8种基本数据类型
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "birthday", birthday); Date date = p.getBirthday(); System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()+"*"+date.toLocaleString()); } @Test
public void test5() throws Exception{ Map map = new HashMap();
map.put("name", "zero");
map.put("password", "521212");
map.put("age", "33");
map.put("birthday", "1998-1-1"); ConvertUtils.register(new DateLocaleConverter(), Date.class); Person p = new Person(); BeanUtils.populate(p, map);//用map集合的值,填充bean的属性值 System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()+"*"+p.getBirthday()); } }
 import java.util.Date;

 public class Person {// javabean

     private String name;
private String password;
private int age;
private Date birthday; public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getAb(){
return null;
} }

使用beanUtils操纵javabean的更多相关文章

  1. 2016年8月17日 内省(1)18_黑马程序员_使用beanUtils操纵javabean

    8.内省(1):18_黑马程序员_使用beanUtils操纵javabean 1.导入两个包: 2.调用静态方法. 9.泛型 map.entrySet() :取出map集合的键值对组成一个set集合. ...

  2. [新手学Java]使用beanUtils控制javabean

    使用BeanUtils设置/读取属性的值以及默认支持的自动转化: @Test //使用BeanUtils设置/读取属性的值以及自动转化 public void test1() throws Illeg ...

  3. beanUtils操作bean的属性

    beanUtils操纵bean属性: 需要jar包commons-beanutils-x.x.x.jar    同时commons-beanutils-x.x.x.jar需要commons-loggi ...

  4. java-内省与javabean

    JavaBean 是一种JAVA语言写成的可重用组件.为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器.JavaBean 通过提供符合一致性设计模式的公共方法将内部域暴露成员属性 ...

  5. 几种封装javaBean的方法

    开发框架时,经常需要使用java对象(javaBean)的属性来封装程序的数据,封装javaBean的方法有很多,比如反射,内省,以及使用工具类.下面从反射开始介绍. 1.javaBean介绍: 简介 ...

  6. BeanUtils基本使用方法与原理

    使用BeanUtils的原因 因为setProperty是JSP中的标签,因此使用model 2模式JSP+Servlet+JavaBean的时候,JSP将form提交给Servlet程序,而Serv ...

  7. JavaBean内省与BeanInfo

    Java的BeanInfo在工作中并不怎么用到,我也是在学习spring源码的时候,发现SpringBoot启动时候会设置一个属叫"spring.beaninfo.ignore", ...

  8. java反射之-Javabean与Map的互转

    1.BeanUntils工具类的准备 /** * @ClassName: BeanUtils * @Description: * @Author: songwp * @Date: 9:02 2022/ ...

  9. Java内省详解

    内省和反射有什么区别: 反射式在运行状态把Java类中的各种成分映射成相应的Java类,可以动态的获取所有的属性以及动态调用任意一个方法,强调的是运行状态.  内省机制是通过反射来实现的,BeanIn ...

随机推荐

  1. css随记02布局

    布局 二栏布局 利用absolute, margin .container { position: relative; } nav { position: absolute; left: 0px; w ...

  2. 去除手机端触摸滑动事件ontouchmove

    window.ontouchmove=function(e){ e.preventDefault && e.preventDefault(); e.returnValue=false; ...

  3. iOS实现简书的账号识别方式(正则表达式)

    通过简书iOS客户端登录,我们会看到请输入手机号或者邮箱登录,但是我们随机输入1234567的时候,便会弹出手机格式不正确,同样也会识别我们的邮箱格式,那么我们在项目中怎么实现这种判断呢? 0E471 ...

  4. iOS开发代码规范(通用)

    1. 关于命名 1> 统一要求 含义清楚,尽量做到不需要注释也能了解其作用,若做不到,就加注释 使用全称,不适用缩写 2> 类的命名 大驼峰式命名:每个单词的首字母都采用大写字母 例子:M ...

  5. BZOJ3979 : [WF2012]infiltration

    答案是$O(\log n)$级别的,故答案不超过6. 当答案是12345时,暴力枚举+压位检验即可,否则直接输出6. 时间复杂度$O(n^5)$. #include<cstdio> #de ...

  6. ACM: 强化训练-Beautiful People-最长递增子序列变形-DP

    199. Beautiful People time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard ...

  7. 洛谷 P1262 间谍网络 Label: Kosarajn强联通

    题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他们就愿意交出手中掌握的全部情报 ...

  8. ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]的用法

    父类:public class BaseHibernateDaoSupport<T>{ private Class<T> entityClass; public BaseHib ...

  9. [BZOJ2792][Poi2012]Well

    2792: [Poi2012]Well Time Limit: 40 Sec  Memory Limit: 64 MBSubmit: 137  Solved: 61[Submit][Status][D ...

  10. POJ 2955 Brackets(区间DP)

    题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <vector& ...