Posted on 2010-05-30 10:01
断点 阅读(428)
评论(0) 编辑 收藏 所属分类:
Apache
MethodUtils的简单用法。
package com.ztf;

import java.util.Map;

import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.beanutils.PropertyUtils;


public class TestMethodUtils
{

public static void main(String[] args) throws Exception
{
Entity entity = new Entity();
entity.setId(1) ;
entity.setName("断点");
// 通过MethodUtils的invokeMethod方法,执行指定的entity中的方法(无参的情况)
MethodUtils.invokeMethod(entity, "sayHello", null);
// 通过MethodUtils的invokeMethod方法,执行指定的entity中的方法(1参的情况)
MethodUtils.invokeMethod(entity, "sayHello", "断点");
// 通过MethodUtils的invokeMethod方法,执行指定的entity中的方法(多参的情况)

Object[] params = new Object[]
{new Integer(10),new Integer(12)};
MethodUtils.invokeMethod(entity, "sayHello", params);
}
}

实体:
package com.ztf;


public class Entity
{
private Integer id;
private String name;

public void sayHello()
{
System.out.println("sayHello()---> 无参");
}

public void sayHello(String s)
{
System.out.println("sayHello()---> 有1个参数" );
}

public void sayHello(Integer a,Integer b)
{
System.out.println("sayHello()---> 有2个参数");
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public Integer getId()
{
return id;
}

public void setId(Integer id)
{
this.id = id;
}
}

输出:
sayHello()---> 无参
sayHello()---> 有1个参数
sayHello()---> 有2个参数