- package rong.propertyUtils ;
- import java.util.Map;
- import org.apache.commons.beanutils.MethodUtils;
- import org.apache.commons.beanutils.PropertyUtils ;
- public class TestPropertyUtils {
- public static void main(String[] args) throws Exception{
- Entity entity = new Entity();
- //通過 PropertyUtils 的getProperty方法獲取指定屬性的值
- Integer id = (Integer)PropertyUtils .getProperty(entity, "id" );
- String name = (String)PropertyUtils .getProperty(entity, "name" );
- System.out.println( "id = " + id + " name = " + name);
- //調用 PropertyUtils 的setProperty方法設置entity的指定屬性
- PropertyUtils .setProperty(entity, "name" , "心夢帆影" );
- System.out.println( "name = " + entity.getName());
- //通過 PropertyUtils 的describe方法把entity的所有屬性與屬性值封裝進Map中
- Map map = PropertyUtils .describe(entity);
- System.out.println( "id = " + map.get( "id" ) + " name = " + map.get( "name" ));
- //通過MethodUtils的invokeMethod方法,執行指定的entity中的方法(無參的情況)
- System.out.println( MethodUtils.invokeMethod(entity, "haha" , null ) );
- //通過MethodUtils的invokeMethod方法,執行指定的entity中的方法(1參的情況)
- MethodUtils.invokeMethod(entity, "sayHelle" , "心夢帆影" );
- //通過MethodUtils的invokeMethod方法,執行指定的entity中的方法(多參的情況)
- Object[] params = new Object[]{ new Integer( 10 ), new Integer( 12 )};
- String msg = (String)MethodUtils.invokeMethod(entity, "countAges" , params);
- System.out.println(msg);
- }
-
}
PropertyUtils MethodUtils用法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.