使用spring提供的ReflectionUtils简化项目中反射代码的复杂性
2015-07-08 05:08
513 查看
在项目中有时候我们会使用到反射的功能,如果使用最原始的方法来开发反射的功能的话肯能会比较复杂,需要处理一大堆异常以及访问权限等问题。spring中提供了ReflectionUtils这个反射的工具类,如果项目使用spring框架的话,使用这个工具可以简化反射的开发工作。我们的目标是根据bean的名称、需要调用的方法名、和要传递的参数来调用该bean的特定方法。下面直接上代码:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.ReflectionUtils; import java.lang.reflect.Method; /** * Created with IntelliJ IDEA. * User: 焦一平 * Date: 2015/6/15 * Time: 18:22 * To change this template use File | Settings | File Templates. */ @Service public class ReInvokeService { @Autowired private SpringContextsUtil springContextsUtil; public void reInvoke(String beanName,String methodName,String[] params){ Method method = ReflectionUtils.findMethod(springContextsUtil.getBean(beanName).getClass(), methodName, String.class, String.class, Boolean.class,String.class); Object[] param1 = new Object[3]; param1[0]=params[0]; param1[1]=params[1]; param1[2]=true; ReflectionUtils.invokeMethod(method, springContextsUtil.getBean(beanName), param1); } }
ReflectionUtils.findMethod()方法的签名是这样的:
public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) 依次需要传入 class对象、方法名、参数类型
SpringContextsUtil 这个工具类实现了 ApplicationContextAware接口,可以访问ApplicationContext的相关信息,代码如下:
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * Created with IntelliJ IDEA. * User: 焦一平 * Date: 2015/6/15 * Time: 18:36 * To change this template use File | Settings | File Templates. */ @Component public class SpringContextsUtil implements ApplicationContextAware { private ApplicationContext applicationContext; public Object getBean(String beanName) { return applicationContext.getBean(beanName); } public <T> T getBean(String beanName, Class<T> clazs) { return clazs.cast(getBean(beanName)); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
相关文章推荐
- Java中的递归原理分析
- JavaWeb开发编码与乱码总结
- Spring MVC 与 web开发
- javaweb(三):使用response实现定时刷新
- JavaIO总结(二)
- JavaIO总结(一)
- SpringMVC 和Struts2的区别
- 使用shrio保护spring 应用
- Java学习笔记--集合
- HashMap源码分析(四)put-jdk8-红黑树的引入
- Java并发包
- Java正则表达式示例
- 一步一步跟我学习hadoop(2)----hadoop eclipse插件安装和运行wordcount程序
- Java集合的小抄 Java初学者必备
- Spring MVC防止数据重复提交
- Struts2 拦截器、防止重复提交
- Struts2
- 字符串时间格式转化-java Unit
- spring环境搭配
- Java基础---多线程