Spring框架中使用java反射无法实例化类,使用ReflectionUtils.findMethod

Spring框架中的反射问题

问题描述
在spring的框架的项目中,使用java的反射去实例化一个service类的时候获取不到该类的对象.

try {
       Class cla = Class.forName(apiName); //数据库配置完整的包名类名
        Object obj = cla.newInstance();//实例化这个类
        Method[] methods = cla.getMethods();// 获得这个类的所有方法
        // Method m = cla.getMethod(methodName); //类中的方法名
        for(Method m:methods){ 
              if( methodName.equals(m.getName())){
                  返回的对象类= (返回的对象类) m.invoke(obj,"1".equals(needargs) ? getAppJson : null); //调用方法 根据配置是否需要传递参数来传getAppJson或null
              }
        }
    } catch (Exception e) {
        e.printStackTrace();
        log.error("Controller invokeMethod error!!! functionId: " + functionId + " apiName: " + apiName + " methodName: " + methodName); 
    }

使用java的反射的时候 获取的反射的service对象为空,debug的时候看见obj 为null,后续操作无法进行.

解决方案

使用spring提供的ReflectionUtils来反射service类,执行service中的方法就可以了.
SpringContextUtil 这个类要implements ApplicationContextAware,里面的这几个方法是比较重要的.

public class SpringContextUtil implements ApplicationContextAware{
  private static ApplicationContext applicationContext;     //Spring应用上下文环境

    /**
     * 实现ApplicationContextAware接口的回调方法,设置上下文环境
     */
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }

    public static Object getBean(String name, Class requiredType) throws BeansException {
        return applicationContext.getBean(name, requiredType);
    }
}    

反射实现

  try {
            Object beanObj = SpringContextUtil.getBean(beanName);
            Method mh = ReflectionUtils.findMethod(beanObj.getClass(), methodName, 参数的类型.class);
            返回的对象类= (返回的对象类) ReflectionUtils.invokeMethod(mh, beanObj, "1".equals(needargs) ? getAppJson : null);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("Controller invokeMethod error!!! functionId: " + functionId + " beanName: " + beanName + " methodName: " + methodName);

        }

要实例化的那个service类 findMethod方法参数(类名,方法名,参数类型)

Method mh = ReflectionUtils.findMethod(beanObj.getClass(), methodName, 参数的类型.class);

这个地方不要被引导了,一开始我的参数类型写错了,报找不到方法.
参考
https://www.cnblogs.com/jiaoyiping/p/4629052.html
他里面的参数是这样的

Method method = ReflectionUtils.findMethod(springContextsUtil.getBean(beanName).getClass(), methodName, String.class, String.class, Boolean.class,String.class);

还有这种
http://topmanopensource.iteye.com/blog/542346

 Method method = ReflectionUtils.findMethod(convertor.getClass(), methodName, new Class[] { String.class });

这俩的参数类型不一样,根据自己的实际情况来写就行了.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值