Spring MethodInvoker
直观的说,如果想通过字符串的配置来调用一个方法;那可以使用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>
Spring MethodInvoker相关推荐
- Spring MethodInvoker学习笔记
编写业务执行代码 @Component public class AMethodClass {public static String execute(String str) {return str. ...
- 解决Spring中使用quartz发生NotSerializableException methodInvoker的问题
Spring2.5 + quartz1.6.5 发生如下异常 Caused by: org.quartz.JobPersistenceException: Couldn't store job: Un ...
- Quartz 在 Spring 中如何动态配置时间
在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源调度组件,因为很多项目使用过,Spring结合Quartz静态配置调度任务时间 ...
- Quartz 在 Spring 中如何动态配置时间--转
原文地址:http://www.iteye.com/topic/399980 在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开 ...
- spring beans源码解读之--总结篇
spring beans下面有如下源文件包: org.springframework.beans, 包含了操作java bean的接口和类. org.springframework.beans.ann ...
- Spring 3整合Quartz 2实现定时任务--转
常规整合 http://www.meiriyouke.net/?p=82 最近工作中需要用到定时任务的功能,虽然Spring3也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之 ...
- Spring+Quartz 集群
转载 http://soulshard.iteye.com/blog/337886 单独的Quartz集群在http://unmi.blogjava.net/有Unmi翻译的Quartz Job Sc ...
- Spring 3整合Quartz 2实现定时任务(转)
http://www.meiriyouke.net/?p=82 最近工作中需要用到定时任务的功能,虽然Spring3也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之后,决定整 ...
- Spring MVC请求处理流程分析
2019独角兽企业重金招聘Python工程师标准>>> 一.简介 Spring MVC框架在工作中经常用到,配置简单,使用起来也很方便,很多书籍和博客都有介绍其处理流程,但是,对于其 ...
最新文章
- minhash算法检索相似文本_文本去重算法:Minhash/Simhash/Klongsent
- Isolation Forest
- windows找不到文件gpedit.msc_电脑文件搜索神器,没有找不到的东西
- LightOJ 1197 Help Hanzo 素数筛
- 一题多解 —— 二项式分布的期望和方差的计算
- C++ socket编程select模型
- 斐讯盒子刷android tv,【TV】斐讯盒子T1_YYFROM语音助手实用版(191129)
- 和谐Froala editor编辑器
- 【签约】ManageEngine卓豪签约中国大饭店 | 智能解析日志、洞察威胁,提升数据安全合规性
- 【图文详解】环境变量中Path环境变量作用详解
- 以图搜图-自动生成图模式匹配Cypher
- 电子琴节奏包制作_MIDI音乐制作基础必备
- 菜鸟打印自定义区域设置
- 餐饮日销售情况分析仪
- 海量数据搜索算法优化-存储/查询/排序算法
- PKI(公、私钥加密,数字签名、数字证书)技术理解
- 解决vtk中vtkStreamTracer例子无法显示流线的疑问
- STM32F1CAN通讯
- ICE之——IceGrid负载均衡部署
- 辛普森求积公式 和 复合辛普森求积公式 Matlab 实现