Apache开源库BeanUtils操作JavaBean的属性

一、Apache BeanUtils介绍

    导入BeanUtils jar 和 commons-logging日志jar

二、BeanUtils代码操作

package com.java.a.introspector;

import java.util.Date;

public class Student {

	private String name;
	private int luckyNum;
	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 int getLuckyNum() {
		return luckyNum;
	}
	public void setLuckyNum(int luckyNum) {
		this.luckyNum = luckyNum;
	}
}

package com.java.a.introspector;

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;

/**
 * @author TS
 */
public class IBeanUtils {

	/**
	 * 通过BeanUtils设置Bean属性
	 * @throws Exception
	 */
	@Test
	public void test() throws Exception{

		Student s = new Student();
		//设置属性
		BeanUtils.setProperty(s, "name", "zhangsan");

		System.out.println(s.getName());
	}

	/**
	 * 通过BeanUtils设置Bean属性2
	 * @throws Exception
	 */
	@Test
	public void test2() throws Exception{

		//模拟web客户端传入后台的数据
		String name = "zhangsan";
		String luckyNum = "47";
		String birthday = "1993-05-12";

		//BeanUtils直接封装属性(只支持8种基本数据类型)
		//日期类型要自定义日期转换器进行操作
		ConvertUtils.register(new DateConverter(), Date.class);

		Student s = new Student();
		BeanUtils.setProperty(s, "name", name);
		BeanUtils.setProperty(s, "luckyNum", luckyNum);
		BeanUtils.setProperty(s, "birthday", birthday);

		System.out.println(s.getName());
		System.out.println(s.getLuckyNum());
		//警告:DateConverter does not support default String to 'Date' conversion.
		System.out.println(s.getBirthday());
	}


	/**
	 * BeanUtils高级用法
	 * @throws Exception
	 */
	@Test
	public void test3() throws Exception{
		//模拟web客户端传入后台的数据,Map封装
		Map<String,String> properties = new HashMap<>();
		properties.put("name", "zhangsan");
		properties.put("luckyNum", "47");
		properties.put("birthday", "1993-05-12");
		
		ConvertUtils.register(new DateConverter(), Date.class);
		
		Student bean = new Student();
		BeanUtils.populate(bean, properties);

		System.out.println(bean.getName());
		System.out.println(bean.getLuckyNum());
		System.out.println(bean.getBirthday());
	}
}

/**
 * 自定义BeanUtils日期转换器
 * @author TS
 *
 */
class DateConverter implements Converter{

	/**
	 * type是BeanUtils通过实例对象获取getClass对象来自动传入的
	 */
	@Override
	public Object convert(Class type, Object value) {

		if( value == null ){
			return null;
		}

		if(!(value instanceof String) ){
			throw new ConversionException("只支持String类型对日期的转换!");
		}

		String source = (String)value;

		if( source.trim().equals("") ){
			return null;
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		try {
			Date date = sdf.parse(source);
			return date;

		} catch (ParseException e) {

			throw new RuntimeException(e);
		}
	}
}


你可能感兴趣的:(Apache开源库BeanUtils操作JavaBean的属性)