直观的说,如果想通过字符串的配置来调用一个方法;那可以使用spring提供的 MethodInvoker这个对象
示例
package test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.MethodInvoker;
public class AMethodClass {
public static String execute(String str) {
return str.concat("-result");
}
public String execute2(String str) {
return str.concat("-result2");
}
public static void main(String[] args) {
BeanFactory beanfactory = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
/*
* 注意看配置文件
* 获取配置的MethodInvoker;这个bean配置的是targetClass,要求targetMethod必须是静态方法
* */
MethodInvoker method = (MethodInvoker) beanfactory.getBean("aMethod");
//下来可以自己手工设置方法参数
Object[] arguments = new Object[1];
arguments[0] = "test";
method.setArguments(arguments);
try {
// 准备方法
method.prepare();
//执行方法
Object result = method.invoke();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
/*
* 注意看配置文件
* 获取配置的MethodInvoker;这个bean配置的是targetObject,则需要先配置一个bean,这里ref到这个bean
* targetMethod可以不是静态的
* */
MethodInvoker method2 = (MethodInvoker) beanfactory.getBean("aMethod2");
//下来可以自己手工设置方法参数
Object[] arguments2 = new Object[1];
arguments2[0] = "test2";
method2.setArguments(arguments2);
try {
// 准备方法
method2.prepare();
//执行方法
Object result = method2.invoke();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
spring配置
<bean id="aMethod" class="org.springframework.util.MethodInvoker"> <property name="targetClass" value="test.AMethodClass" /> <property name="targetMethod" value="execute" /> </bean> <bean id="aMethod2Class" class="test.AMethodClass" /> <bean id="aMethod2" class="org.springframework.util.MethodInvoker"> <property name="targetObject" ref="aMethod2Class" /> <property name="targetMethod" value="execute2" /> </bean>
但一般情况不直接使用MethodInvoker;而是使用MethodInvokingFactoryBean,具体可参看这2个类的javadoc
示例
package test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.MethodInvoker;
public class AMethodClass {
public static String execute(String str) {
return str.concat("-result");
}
public String execute2(String str) {
return str.concat("-result2");
}
public static void main(String[] args) {
BeanFactory beanfactory = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
/*
* 使用MethodInvokingFactoryBean;相当于spring帮你做了上述逻辑;
* 可以直接获取到方法的执行结果;
* 注意这里是方法的返回值;而不是class本身;这是由于MethodInvokingFactoryBean实现了FactoryBean接口;
* 由接口方法getObject()来获取最终返回的对象
*/
Object aMethod3 = beanfactory.getBean("aMethod3");
System.out.println(aMethod3);
}
}
spring配置
<bean id="aMethod3" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass"> <value>test.AMethodClass</value> </property> <property name="targetMethod"> <value>execute</value> </property> <property name="arguments"> <list> <value>test3</value> </list> </property> </bean>