现在的位置: 首页 > 综合 > 正文

JavaBean的一些总结

2013年09月02日 ⁄ 综合 ⁄ 共 3012字 ⁄ 字号 评论关闭

最近在学习JavaBean下面是我的一些总结,如果有错的希望大家指出来

一、定义:

     把一个拥有对属性进行set和get方法的类,我们就可以称之为JavaBean。实际上JavaBean就是一个java类,在这个java类中就默认形成了一种规则——对属性进行设置和获得。而反之将说ava类就是一个JavaBean,这种说法是错误的,因为一个java类中不一定有对属性的设置和获得的方法(也就是不一定有set和get方法)。
二、从JavaBean中就引出了一个内省的概念
     内省:把一类中需要进行设置和获得的属性访问权限设置为private(私有的)让外部的使用者看不见摸不着,而通过public(共有的)set和get方法来对其属性的值来进行设置和获得,而内部的操作具体是怎样的?外界使用的人不用不知道,这就称为内省。
三、使用内省解释JavaBean中setProperty()和setProperty()方法的实现原理。实际上也是用反射的那一套做法

package itcast.Test5;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.crypto.spec.PSource;

import org.apache.commons.beanutils.BeanUtils;

/*5、写一个方法,public void setProperty(Object obj, String propertyName, Object value){},
此方法可将obj对象中名为propertyName的属性的值设置为value。*/
//这题中用了内省的简单应用,理解到了JavaBean中setProperty()方法和getProperty()方法的实现
public class SetPropertyTest {

 /**
  * @param args
  * @throws NoSuchMethodException
  * @throws InvocationTargetException
  * @throws IllegalAccessException
  */
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  Point pt1 = new Point(3, 5);
  String propertyName = "x";
  Object value = 7;
  
  getProperty(pt1, propertyName);
  
  setProperty(pt1, propertyName, value);
  //下面是使用BeanUtil工具包进行set和get属性的值
  /*//System.out.println(BeanUtils.getProperty(pt1, propertyName));
  BeanUtils.setProperty(pt1, propertyName, "9");
  System.out.println(pt1.getX());
  //System.out.println(pt1.getX());
*/ }
 //setProperty()方法的实现
 private static void setProperty(Object object, String propertyName, Object value) {
  try {
   //使用java.beans包中的PropertyDescriptor(属性操作符类)
   PropertyDescriptor pd2 = new PropertyDescriptor(propertyName, object.getClass());
   //通过反射来得到Point这个javabean的set方法
   Method methodSetX = pd2.getWriteMethod();//getWriteMethod()得到JavaBean中可写的方法也就是set方法
   methodSetX.invoke(object,value);//对方法进行赋值设置
  } catch (Exception e) {
   // TODO: handle exception
  }
 }
 //getProperty()方法的实现
 private static void getProperty(Object object, String propertyName) {
  
  try {
   PropertyDescriptor pd1 = new PropertyDescriptor(propertyName, object.getClass());
   Method methodGetX = pd1.getReadMethod();
   Object retVal = methodGetX.invoke(object);
   System.out.println(retVal);
   //下面所注析的时一种比较复杂的方法来获得JavaBean中的属性值
  /* BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
   //通过beanInfo对象调用getPropertyDescriptors()方法来获得所有的属性描述
   PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
   Object retVal = null;
   通过上面的操作得到了对JavaBean中的所有的属性的描述,下面就对所有的属性进行迭代,通过每个属性的getName()方法来得到属性名,
   和我要找的那个属性名来进行比较,如果找到了,就进行相应的操作
   for (PropertyDescriptor pd : pds) {
    if(pd.getName().equals(propertyName)){
     Method methodGetX = pd.getReadMethod();
     retVal = methodGetX.invoke(object);
     break;
    }
   }*/
   //System.out.println(retVal);
  } catch (Exception e) {
   // TODO: handle exception
  }
  
 }

}

class Point {
 private int x;
 private int y;
 
 public Point(int x, int y) {
  super();
  this.x = x;
  this.y = y;
 }

 public int getX() {
  return x;
 }

 public void setX(int x) {
  this.x = x;
 }

 public int getY() {
  return y;
 }

 public void setY(int y) {
  this.y = y;
 }
 
}

抱歉!评论已关闭.