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);

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