`

PropertyUtils MethodUtils用法

阅读更多
  1. package  rong.propertyUtils ;   
  2.   
  3. import  java.util.Map;   
  4. import  org.apache.commons.beanutils.MethodUtils;   
  5. import  org.apache.commons.beanutils.PropertyUtils ;   
  6.   
  7. public   class  TestPropertyUtils {   
  8.        
  9.      public   static   void  main(String[] args)  throws  Exception{   
  10.            
  11.         Entity entity =  new  Entity();   
  12.            
  13.          //通过 PropertyUtils 的getProperty方法获取指定属性的值   
  14.         Integer id = (Integer)PropertyUtils .getProperty(entity,  "id" );   
  15.         String name = (String)PropertyUtils .getProperty(entity,  "name" );   
  16.         System.out.println( "id = "  + id +  "  name = "  + name);   
  17.            
  18.          //调用 PropertyUtils 的setProperty方法设置entity的指定属性   
  19.         PropertyUtils .setProperty(entity,  "name" "心梦帆影" );   
  20.         System.out.println( "name = "  + entity.getName());   
  21.            
  22.          //通过 PropertyUtils 的describe方法把entity的所有属性与属性值封装进Map中   
  23.         Map map = PropertyUtils .describe(entity);   
  24.         System.out.println( "id = "  + map.get( "id" ) +  "  name = "  + map.get( "name" ));   
  25.            
  26.          //通过MethodUtils的invokeMethod方法,执行指定的entity中的方法(无参的情况)   
  27.         System.out.println( MethodUtils.invokeMethod(entity,  "haha" null ) );   
  28.            
  29.          //通过MethodUtils的invokeMethod方法,执行指定的entity中的方法(1参的情况)   
  30.         MethodUtils.invokeMethod(entity,  "sayHelle" "心梦帆影" );   
  31.            
  32.          //通过MethodUtils的invokeMethod方法,执行指定的entity中的方法(多参的情况)   
  33.         Object[] params =  new  Object[]{ new  Integer( 10 ), new  Integer( 12 )};   
  34.         String msg = (String)MethodUtils.invokeMethod(entity,  "countAges" , params);   
  35.         System.out.println(msg);   
  36.            
  37.     }   
  38.   

分享到:
评论
1 楼 ningwuyu 2011-12-01  
Entity 类是那个jar里面的

相关推荐

Global site tag (gtag.js) - Google Analytics