Java BeanUtils操作JavaBean

BeanUtils操作JavaBean,需要先导入commons-beanutils包,可以到apache官方网站去下载 http://commons.apache.org/beanutils/download_beanutils.cgi
还得导入commons-logging包( http://commons.apache.org/logging/download_logging.cgi),因为commons-beanutils包用到了commons-logging包。
public class JavaBeanTest {

   /**
    * @param args
    * @throws Exception
    */
   public static void main(String[] args) throws Exception {
     // TODO Auto-generated method stub
    Point pt1 = new Point(3);
    
     //setProperty(Object bean, String name, Object value)
     //设置Javabean类对象的name属性为value值
    BeanUtils.setProperty(pt1, "x", "7");
    
     //getProperty(Object bean, String name)
     //得到JavaBean类对象的name属性的值
    System.out.println(BeanUtils.getProperty(pt1, "x"));
  }
}

public     class Point {
   private int x;
   /**
    * 构造函数
    * @param x 参数x
    */
   public Point( int x) {
     super();
     this.x = x;
  }
  
   //对每个成员变量设置get和set方法
   public int getX() {
     return x;
  }
   public void setX( int x) {
     this.x = x;
  }
}



你可能感兴趣的:(java,职场,BeanUtils,javabean,休闲)