总结classBeanUtils学习总结

文章结束给大家来个程序员笑话:[M]

    

1.BeanUtils介绍

    它是由Apache组织开发的一套操纵JavaBean的API。通过BeanUtils库能够完成内省的一切功能,并且优化。通过其才操纵JavaBean,可以大大简化代码的编写

    BeanUtils:http://commons.apache.org/proper/commons-beanutils/index.html

    Common Logging:http://commons.apache.org/proper/commons-logging/download_logging.cgi

    

2.下载及导入

    下载BeanUtils库

    commons-beanutils-1.8.3-bin

    commons-beanutils-1.8.3-src

    在项目中新建lib文件夹,将commons-beanutils-1.8.3复制过去,并build path到项目中

    下载Common Logging

    commons-logging-1.1.3-bin

    复制到lib文件夹,并build path到项目总

 

    

3.BeanUtils工具包中的常用类:

    BeanUtils

    ConvertUtils.register(Convert convert,Class clazz)

    自定义转换器

    内置转换器

 

    

4.Demo

    (1)假设有一个先生类

package web.java.beanutils;

import java.util.Date;

public class Student {
	private String name;
	private String sno;
	private int age;
	private Date birthday;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSno() {
		return sno;
	}
	public void setSno(String sno) {
		this.sno = sno;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	
}

 

    (2)利用BeanUtils来操纵JavaBean

package web.java.beanutils;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.junit.Test;

public class Demo1 {
	
	@Test
	public void test1(){
		Student s = new Student();
		BeanUtils bu = new BeanUtils();
		ConvertUtils.register(new Converter(){

			/*
			 * 参数一:java.util.Date.class(目标类型)
			 * 参数二:传入的参数类型,即java.lang.String 
			 */
			public Object convert(Class arg0, Object arg1) {
				String strBirthday = (String)arg1;
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				try {
					return sdf.parse(strBirthday);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				return null;
			}
			
		}, java.util.Date.class);
		
		try {
			bu.setProperty(s, "name", "well");
			bu.setProperty(s, "age", "21");
			bu.setProperty(s, "birthday","1990-10-15");
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		System.out.println(s.getName()+" "+s.getAge() + " "+ s.getBirthday());
		//output:well 21 Mon Oct 15 00:00:00 CST 1990
	}

}
    每日一道理
在每个人心中,都曾停留过那些值得怀念的人,也许还在,也许早已消逝,在茫茫人海中丢失,于是,那份怀念便得凄凉,因为模糊的记忆中只剩下一个“空壳”,没有什么,甚至连自己的心都装不下,时光把一切抹平,也把当日的泪水封锁,因为已没有,怀念只是悲凉!

 

    还有一种简化代码的方式:

    利用Converter接口的子类来直接实现,而非实现Converter接口来实现。

    如下,直接应用DateLocaleConverter()类

package web.java.beanutils;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;

public class Demo2 {
	
	@Test
	public void test1(){
		Student s = new Student();
		BeanUtils bu = new BeanUtils();
		
		ConvertUtils.register(new DateLocaleConverter(), java.util.Date.class);
		
		try {
			bu.setProperty(s, "name", "well");
			bu.setProperty(s, "age", "21");
			bu.setProperty(s, "birthday","1990-10-15");
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		System.out.println(s.getName()+" "+s.getAge() + " "+ s.getBirthday());
		//output:well 21 Mon Oct 15 00:00:00 CST 1990
	}

}

 

    还有一个问题是,输出的时光为CST时光,而非我们熟习的中国常用的时光表示情势,这里可以通过如下方法调用

s.getBirthday().toLocaleString()

    这样,可以修正时光的显示(注意,这个方法已过期了!)

    5.几点总结

    (1)BeanUtils框架能够对String<---->基本数据类型自动转化

    (2)BeanUtils框架能够自定义转换器:ConvertUtils.register( 转换规则 ,目标对象的Class)

    (3)向BeanUtils框架注册自定义转换器必须放在bu.setProperty()代码之前

    (4)应用BeanUtils内置String->Date的转换器:ConvertUtils.register(new DateLocaleConverter(),java.util.Date.class);

文章结束给大家分享下程序员的一些笑话语录: 刹车失灵
有一个物理学家,工程师和一个程序员驾驶着一辆汽车行驶在阿尔卑斯山脉 上,在下山的时候,忽然,汽车的刹车失灵了,汽车无法控制地向下冲去, 眼看前面就是一个悬崖峭壁,但是很幸运的是在这个悬崖的前面有一些小树 让他们的汽车停了下来, 而没有掉下山去。 三个惊魂未定地从车里爬了出来。
物理学家说, “我觉得我们应该建立一个模型来模拟在下山过程中刹车片在高 温情况下失灵的情形”。
工程师说, “我在车的后备厢来有个扳手, 要不我们把车拆开看看到底是什么 原因”。
程序员说,“为什么我们不找个相同的车再来一次以重现这个问题呢?”

--------------------------------- 原创文章 By
总结和class
---------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值