根据反射获取springboot管理的对象,反射获取springboot管理bean,反射调用指定方法


    @Autowired
    private ApplicationContext applicationContext;
        String className = map.get("className").toString();
        Object bean = applicationContext.getBean(className);
        Class<?> aClass = bean.getClass();
        String methodName = map.get("methodName").toString();
        List<String> list = (List) map.get("argsTypes");
        List<Class> arrayList = new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList()).forEach(l->{
            try{
                Class<?> forName = Class.forName(l.toString());
                arrayList.add(forName);
            }catch (Exception e){}
        });
        Class [] argsTypes = arrayList.toArray(new Class[]{});
        List args = (List) map.get("args");
        Method method = aClass.getMethod(methodName, argsTypes);
        Object[] objects = args.toArray(new Object[]{});
        Object invoke = method.invoke(bean,objects );
        return (R)invoke;

调用参数案例

 

{
"className":"QRCodeServiceImpl",
"methodName":"getCabinetList",
"argsTypes":["java.lang.String"],
"args":["2608"]
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM