@Autowired 不适用于同一控制器的其他方法中的一个控制器工作正常

@Autowired dosn't work into one controller in other method of the same controller works fine

当我在控制器中添加新的@RequestMapping 方法时@Autowired 突然停止工作。为什么我不明白。当我尝试使用这个新方法时,所有@Autowired 接口都变为空。当我将其他 @RequestMapping 用于其他方法时,一切正常。

当我尝试使用新的@RequestMapping 时,第一张图片显示所有@Autowired class 为空。

这个我用其他的@RequestMapping 都完美无缺。这是 spring 休息 Api 应用程序。我以前没有遇到过这种事情。什么可能是这样的应用程序。

Spring 通过以下方式创建您的控制器:

  1. 创建实例
  2. 注入 @Autowired 依赖项
  3. 用处理注释的代理包装此实例,如 @PreAuthorize@Transactional,然后将调用委托给真实实例。

由于代理库的性质,Spring 使用的是 (CGLIB) 并且通常也使用 java 语言本身。只允许覆盖 public 方法。在查看您的示例时,这很有意义。

您失败的方法用 @PreAuthorize 注释并且....它是 private,意思是:它是在代理上调用的,代理没有注入的依赖项,因为通常它会委托给真实的实例。但是因为它是一个private方法,cglib和java不能那样做。

By the way: When looking at your screenshots you can see that the this variable is different in both of your calls.