SpringBoot 使用反射调用Service时Dao层为空

Class clazz  = Class.forName("com.zhc.nly.service.merchants.MerchantsService");
Object o = clazz.newInstance();
Method method = clazz.getMethod("listShopProductBySel",String.class);
Object object = method.invoke(o, shopWares.getWaresId());

在Controller层通过反射调用service方法,Dao层为null。

使用的是SpringBoot,Dao层注入是使用SpringBoot的使用这种方式无效

@Autowired
private ApplicationContext applicationContext;
Class<?> cls = Class.forName("com.zhc.nly.service.merchants.MerchantsService");
// 获取spring中的bean对象
Object bean = applicationContext.getBean(cls);
// 获取mybatis方法.形参是待执行方法名
Method method = cls.getMethod("listShopProductBySel", String.class);
// 执行方法
Object object =  method.invoke(bean, shopWares.getWaresId());

猜你喜欢

转载自blog.csdn.net/u011134399/article/details/109474008