根據反射獲取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