在 Spring 中,@Autowired 注解用于自动装配依赖项。当使用 @Autowired(required = false) 时,注入的依赖项可以为空。\n\n如果您在 controller 中使用 @Autowired(required = false) 注解来注入 AuthenticationManager 依赖项,并且得到的值为空,可能有以下原因:\n\n1. 没有正确配置 AuthenticationManager bean:请确保您在配置文件(如 application.properties 或 application.yml)中正确地配置了 AuthenticationManager bean。例如,在 Spring Security 中,您可以使用 @Configuration 和 @EnableWebSecurity 注解来配置 AuthenticationManager bean。\n\n2. 在 controller 类中没有正确声明:请确保您正在使用 @Autowired 注解将 AuthenticationManager 注入到正确的 controller 类中。确保 controller 类上有 @Controller 或 @RestController 注解,并且确保该类是 Spring 上下文中的一个 bean。\n\n3. 依赖项的名称与注入的名称不匹配:请确保依赖项的名称与 @Autowired 注解中的名称匹配。如果它们不匹配,Spring 将无法找到正确的依赖项。\n\n4. 依赖项的类型与注入的类型不匹配:请确保依赖项的类型与 @Autowired 注解中的类型匹配。如果它们不匹配,Spring 将无法找到正确的依赖项。\n\n如果您仍然无法解决问题,建议您检查日志文件以获取更多详细的错误信息,以便更好地理解为什么无法正确注入 AuthenticationManager。

Spring Controller 中 @Autowired(required = false) 的 AuthenticationManager 为空:常见原因及解决方案

原文地址: https://www.cveoy.top/t/topic/p2kU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录