https://blog.csdn.net/jy00733505/article/details/107524688
在工程中
/**
* 判断此角色是否已经分配给用户
* @param id
* @return
*/
@GetMapping("/existsUserByRoleId")
@ResponseBody
private R existsUserByRoleId(String id) {
try {
Map<String, Object> map = new HashMap<>();
boolean exists = roleService.existsUserByRoleId(id);
map.put("exists",exists);
return R.ok(map);
} catch (Exception e) {
e.printStackTrace();
return R.error(e.getMessage());
}
}
测试用例roleService是有实例的,而在
/**
* 判断此机构下是否已经分配了用户或角色
* @param id
* @return
*/
@GetMapping("/existsUserOrRoleByOrgId")
@ResponseBody
private R existsUserOrRoleByOrgId(String id) {
try {
boolean exists = managementService.existsUserOrRoleByOrgId(id);
Map<String, Object> map = new HashMap<>();
map.put("exists",exists);
return R.ok(map);
} catch (Exception e) {
e.printStackTrace();
return R.error(e.getMessage());
}
}
之中,managementService则成为了null,没有加载。
通过分析后发现,roleService为直接的普通类,并不需要实现,所以方法声明为private是可以的,而在managementService中,managementService是接口,其实现类为ManagementServiceImpl,在接口声明中使用private会导致:
因为 springboot 的缓存机制是通过切面编程 aop来实现的,对于SpringAOP的cglib是不会拦截private方法的(而JDK动态代理是基于接口的更不可能对private方法生效),因此不进切面逻辑,aop是拦截不到的。
由此导致managementService注入失败为null,当把接口声明改为public问题便解决了。
public R existsUserOrRoleByOrgId(String id) {