当前位置:web 服务器 > Apache >>

javaBean类,以及Sun的java内省机制与apache的BeanUtils框架/工具简单应用

一、什么是符合规则的javaBean类?
具有:

  .私有的字段(Field)
  .对私有字段提供存取方法(读写方法)
  .数量任意的业务方法

的类。

二、站在反射角度下的 内省机制(SUN公司开发)
javaBean类:

[java]
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; 
    } 

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的内省机制代码

[java]
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());           
        }    
    } 

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属性。

三、BeanUtils框架/工具(APACHE开源组织开发)
javaBean类:

[java]
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) {&

补充:软件开发 , Java ,
Apache
IIS
Nginx
Tomcat
如果你遇到web 服务器难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,