具有:
.私有的字段(Field)
.对私有字段提供存取方法(读写方法)
.数量任意的业务方法的类。
javaBean类:
package com.xiaohui.javabean; public class Person { private String name; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
操作javaBean的内省机制代码
package com.xiaohui.javabean; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import org.junit.Test; public class JavaBeanDemo1 { @Test public void test() throws Exception{ //通过反射创建对象。 Class clazz = Class.forName("com.xiaohui.javabean.Person"); Constructor ct = clazz.getConstructor(null); Object obj = ct.newInstance(null); //描述 Java Bean 通过一对存储器方法导出的一个属性 name PropertyDescriptor pdName = new PropertyDescriptor("name",Person.class); PropertyDescriptor pdAge = new PropertyDescriptor("age",Person.class); //获得写入方法。 Method wmt = pdName.getWriteMethod(); Method agewmt = pdAge.getWriteMethod(); //获得读取方法。 Method rmt = pdName.getReadMethod(); Method agermt = pdAge.getReadMethod(); //给属性写值。 wmt.invoke(obj, "jack"); agewmt.invoke(obj,20); //读取属性值 String val = (String) rmt.invoke(obj, null); int ageval = (Integer) agermt.invoke(obj, null); System.out.println("name = "+val); System.out.println("age = "+ageval); } @Test public void test2() throws Exception{ BeanInfo bi = Introspector.getBeanInfo(Person.class); PropertyDescriptor[] pd = bi.getPropertyDescriptors(); for (PropertyDescriptor pds : pd) { System.out.println(pds.getName()); } } }
test运行结果:
name = jack
age = 20
test2()运行结果:所有属性:
age
class
name
任何一个JavaBean都有一个class属性,来自于Object类。所以包含class属性。
javaBean类:
package com.xiaohui.javabean; import java.util.Date; public class Student { private String name; private int age; private Date birthday; public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
操作bean类的BeanUtils框架工具类代码
package com.xiaohui.javabean; 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; public class Demo2 { /** * @param args * @throws Exception * @throws IllegalAccessException */ public static void main(String[] args) throws Exception { Student s = new Student(); BeanUtils bu = new BeanUtils(); //向BeanUtils框架注册自己的转换器(String -> Date) ConvertUtils.register(new Converter() { @Override public Object convert(Class arg0, Object arg1) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { return sdf.parse((String) arg1); } catch (ParseException e) { e.printStackTrace(); } return null; }}, java.util.Date.class); bu.setProperty(s, "name", "小青"); bu.setProperty(s, "age", "20"); bu.setProperty(s, "birthday", "1992-02-05"); String nameVal = bu.getProperty(s, "name"); System.out.println("name = "+nameVal); String ageVal = bu.getProperty(s, "age"); System.out.println("age = "+ageVal); String bitVal = bu.getProperty(s, "birthday"); System.out.println("birthday = "+bitVal); } }
运行结果:
name = 小青
age = 20
birthday = Wed Feb 05 00:00:00 CST 1992