我根据传入的类名用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,要怎样才能调用这个类的这个方法?类和方法都不一定.......

解决方案 »

  1.   

    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;
    }
    }
      

  2.   

    使用java的反射机制,查找此类中是否有这个方法,有的话使用反射机制实现调用这个方法
      

  3.   


    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);
    }
    }
      

  4.   

            String msg = (String) m.invoke(theBean, "床上等你");
            System.out.println("返回的结果 :" + msg);

    ......
      

  5.   

    获取method两个方法
    getDeclaredMethod可以得到所有的私有公有类
    但不能到父类,需要自己手动写一下
    getMethod得到public方法,可以得到父类的调用方法
    menthod.invoke(object, new Object[] { value });new Object[] { value }为一个object叔祖
      

  6.   

    一楼已经正解、三楼已经正解,楼主照搬即可。
    若想了解Spring 是怎么干这事的,书店找厚书必定有解。
      

  7.   

    毫无疑问这题就是用反射,用hibernate作持久化操作时,也就是用的反射的方法。
      

  8.   

     String msg = (String) m.invoke(theBean, "床上等你");
    up..........
      

  9.   

    String msg = (String) m.invoke(theBean, "床上等你"); 
    这个嘛。