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

 


版权声明:本文为u011134399原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。