当前位置: 代码迷 >> Java Web开发 >> 怎么根据类名和方法名调用方法
  详细解决方案

怎么根据类名和方法名调用方法

热度:174   发布时间:2016-04-17 10:56:55.0
如何根据类名和方法名调用方法?
我根据传入的类名用spring的IoC可以得到类,也可以调用类的已知的接口,如何可以根据传入的方法名调用类的方法?
比如:有test1---testn,
public class TestN extends myinterface{
......
public String method1(String par1,String par2){
....
}
public String method2(String par1,String par2){
....
}
public String method3(String par1,String par2){
....
}

在程序里:
用spring的方法:
ApplicationContext ctx=new FileSystemXmlApplicationContext(appStr);
myinterface myBean=(myinterface)ctx.getBean(beanName);
这样可以得到我要的类,如果我再有个参数method=..,method可能是method1---methodn,要怎样才能调用这个类的这个方法?类和方法都不一定.......

------解决方案--------------------
Java code
import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class TheReflect {    /**     * @param args     * @throws NoSuchMethodException     * @throws SecurityException     * @throws InvocationTargetException     * @throws IllegalAccessException     * @throws IllegalArgumentException     */    public static void main(String[] args) throws SecurityException,            NoSuchMethodException, IllegalArgumentException,            IllegalAccessException, InvocationTargetException {        // TODO Auto-generated method stub        /** 详细的API使用方法参加JDK的doc文档中的java.lang.Class和java.lang.reflect.Method */                /* 相当于你在Spring的Context中获取bean对象 */        TheBeanClazz theBean = new TheBeanClazz();        /* 通过获取到bean对象得到该对象的类定义 */        Class clazz = theBean.getClass();        /* 通过上面得到的类定义对象clazz得到指定名称和参数类型列表的方法定义 */        Method m = clazz.getMethod("sayHello", String.class);        /*         * 在指定的对象(theBean)上调用符合上面方法定义的方法,         *  并且制定方法的入口参数值列表(这个方法只有一个参数,         * 如果是多个参数用逗号分隔)         */        String msg = (String) m.invoke(theBean, "床上等你");        System.out.println("返回的结果 :" + msg);    }}class TheBeanClazz {    public String sayHello(String s) {        System.out.println("Hello :" + s);        return "Hello :" + s;    }}
------解决方案--------------------
使用java的反射机制,查找此类中是否有这个方法,有的话使用反射机制实现调用这个方法
------解决方案--------------------
Java code
import java.lang.reflect.Method;public class Test{        /**     * @param className 类名     * @param methodName 方法名     * @param param     方法参数     * @param parameterType 方法参数类型     * @throws Exception     */    private static void callMethod(String className,String methodName,Object[] param,Class...parameterType) throws Exception {        Class cObj = Class.forName(className);        Method m = cObj.getDeclaredMethod(methodName,parameterType);        Object o = m.invoke(cObj.newInstance(),param);    }        public static void main(String[] args) throws Exception {        callMethod("com.test.reflect.TestClass","method2",null);        Object[] param = new Object[]{"测试数据"};        callMethod("com.test.reflect.TestClass","method1",param,String.class);    }}class TestClass{    public void method2(){        System.out.println("method2");    }    public void method1(String str){        System.out.println(str);    }}
------解决方案--------------------
String msg = (String) m.invoke(theBean, "床上等你");
System.out.println("返回的结果 :" + msg);


......
------解决方案--------------------
获取method两个方法
getDeclaredMethod可以得到所有的私有公有类
但不能到父类,需要自己手动写一下
getMethod得到public方法,可以得到父类的

调用方法
menthod.invoke(object, new Object[] { value });

new Object[] { value }为一个object叔祖
------解决方案--------------------
  相关解决方案