springboot 使用反射时spring注入无效

解决方法:

     在使用反射的类中注入  ApplicationContext,

     通过applicationContext.getBea(cls);  从spring中获取bean

    @Autowired

     private ApplicationContext applicationContext;

 

    Class<?> cls = Class.forName("xxx.xxx.xxx.xxx.classname");

    Object bean = applicationContext.getBean(cls);

    Method method = cls.getMethod(methodName, class<?>parameterTypes);

    Object invoke = method.invoke(bean, parameter);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章