/**
*@ author StormMaybin
*@ date 2016-10-06
*/
生命不息,奋斗不止!
使用内省API操作Bean的属性
API
package com.stormma.introspector;
import java.util.Date;
public class Person
{
private String name;
private int age;
private Date birthday;
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;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
}
操作bean的属性
package com.stormma.introspector;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Demo1
{
public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
// getBean();
setGetBean();
}
private static void setGetBean() throws IntrospectionException,
IllegalAccessException, InvocationTargetException
{
Person p = new Person ();
PropertyDescriptor pd = new PropertyDescriptor ("age", Person.class);
//得到bean的属性的set方法
Method method = pd.getWriteMethod();
method.invoke(p, 20);
//得到bean的属性的get方法
Method getMethod = pd.getReadMethod();
System.out.println(getMethod.invoke(p, null));
System.out.println(p.getAge());
}
/**
* 得到bean的属性
* @throws IntrospectionException
*/
private static void getBean() throws IntrospectionException
{
//剔除Object的bean的属性
BeanInfo info = Introspector.getBeanInfo(Person.class, Object.class);
PropertyDescriptor [] pds = info.getPropertyDescriptors();
for (PropertyDescriptor pd : pds)
{
System.out.println(pd.getName());
}
}
}
使用apache 的BeanUtils操作bean的属性
ps: 需要jar包commons-beanutils-x.x.x.jar同时commons-beanutils-x.x.x.jar。需要commons-logging.jar日志记录器来支持。所以用beanUtils的时候需要导入两个jar包。
package com.stormma.beanutils;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
public class Main
{
public static void main(String[] args)
{
try
{
// test1();
// test2();
// test3();
// test4();
test5();
} catch (IllegalAccessException | InvocationTargetException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 用BeanUtils操作bean的属性
*
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
private static void test1() throws IllegalAccessException,
InvocationTargetException
{
Person p = new Person();
BeanUtils.setProperty(p, "name", "StormMa");
System.out.println(p.getName());
}
/**
* 找到数据封装对象
*
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void test2() throws IllegalAccessException,
InvocationTargetException
{
Person p = new Person();
String name = "StormMa";
String age = "20";
BeanUtils.setProperty(p, "name", name);
// 转换器(String --> int)
// 但是只支持八种基本数据类型的转换
// 如果要转换其他的类型,可以自定义转换器
BeanUtils.setProperty(p, "age", age);
System.out.println(p.getName() + " " + p.getAge());
}
/**
* 有问题代码String cannot convert to Date
*
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void test3() throws IllegalAccessException,
InvocationTargetException
{
Person p = new Person();
String name = "StormMa";
String age = "20";
String birthday = "1996-02-16";
BeanUtils.setProperty(p, "name", name);
// 转换器(String --> int)
// 但是只支持八种基本数据类型的转换
// 如果要转换其他的类型,可以自定义转换器
BeanUtils.setProperty(p, "age", age);
// 错误,只支持八种基本数据类型转换
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName() + " " + p.getAge() + " "
+ p.getBirthday());
}
/**
* 自定义转换器
*
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void test4() throws IllegalAccessException,
InvocationTargetException
{
Person p = new Person();
String name = "StormMa";
String age = "20";
String birthday = "1996-02-16";
// 注册日期转换器
ConvertUtils.register(new Converter()
{
@Override
public <T> T convert(Class<T> type, Object value)
{
// TODO Auto-generated method stub
// 先检查数据
if (value == null)
{
return null;
} else if (!(value instanceof String))
{
throw new ConversionException("只支持String转换");
} else if (((String) value).trim().equals(""))
{
return null;
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try
{
return (T) df.parse((String) value);
} catch (ParseException e)
{
// TODO Auto-generated catch block
// 异常链不能断
throw new RuntimeException(e);
}
}
}, Date.class);
BeanUtils.setProperty(p, "name", name);
// 转换器(String --> int)
// 但是只支持八种基本数据类型的转换
// 如果要转换其他的类型,可以自定义转换器
BeanUtils.setProperty(p, "age", age);
// 错误,只支持八种基本数据类型转换
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName() + " " + p.getAge() + " "
+ p.getBirthday().toLocaleString());
}
/**
* apache内置Date转换器,有小bug
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void test5() throws IllegalAccessException,
InvocationTargetException
{
Person p = new Person();
String name = "StormMa";
String age = "20";
String birthday = "1996-02-16";
//提供转换器
ConvertUtils.register(new DateLocaleConverter(), Date.class);
BeanUtils.setProperty(p, "name", name);
// 转换器(String --> int)
// 但是只支持八种基本数据类型的转换
// 如果要转换其他的类型,可以自定义转换器
BeanUtils.setProperty(p, "age", age);
// 错误,只支持八种基本数据类型转换
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName() + " " + p.getAge() + " "
+ p.getBirthday().toLocaleString());
}
}
附上jar下载地址