zoukankan      html  css  js  c++  java
  • 使用spring提供的ReflectionUtils简化项目中反射代码的复杂性

    在项目中有时候我们会使用到反射的功能,如果使用最原始的方法来开发反射的功能的话肯能会比较复杂,需要处理一大堆异常以及访问权限等问题。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;
        }
    }


  • 相关阅读:
    Div在BOdy中居中
    c_lc_填充每个节点的下一个右侧节点指针 I~II(递归)
    c_pat_哈密顿回路 & 最大点集 & 是否是旅行商路径 & 欧拉路径 & 最深的根(邻接矩阵存图)
    c_lc_二叉搜索树的最近公共祖先 & 二叉树的最近公共祖先(利用性质 | 从p,q开始存储每个结点的父亲)
    c_pat_树题大杂烩(利用性质)
    现在的我,理解了这种「激情」
    b_pat_排成最小的数字 & 月饼(字符串拼接比较a+b<b+a)
    c_lc_二叉搜索树中的众数(中序遍历+延迟更新前驱结点)
    b_pat_分享 & 链表排序 & 链表去重(链表模拟)
    b_pat_弹出序列(栈模拟)
  • 原文地址:https://www.cnblogs.com/jiaoyiping/p/4629052.html
Copyright © 2011-2022 走看看