springboot出现Requested bean is currently in creation: Is there an unresolvable circular reference?(代码

Posted Roninaxious

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot出现Requested bean is currently in creation: Is there an unresolvable circular reference?(代码相关的知识,希望对你有一定的参考价值。

这是同时在spring配置类中使用@Autowired和@Bean注解出现bean已经注入的问题

如以下代码就会出现该错误

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
 
    @Autowired
    private PasswordEncoder passwordEncoder;

原因:

同一个类中已经存在bean对象,就不需要再依赖注入了。

两种解决方法

1.单独写一个配置类(或者写其他配置类中),将BCryptPasswordEncoder()加入到ioc容器中

@Component
public class PwdConfig{

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

2.直接使用passwordEncoder()通过.方法调用即可。

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    
    public void test() {
        passwordEncoder().encode("123");
    }

以上是关于springboot出现Requested bean is currently in creation: Is there an unresolvable circular reference?(代码的主要内容,如果未能解决你的问题,请参考以下文章

springboot 异常: Requested bean is currently in creation: Is there an unresolvable circular reference?

springboot 解决跨域请求,No 'Access-Control-Allow-Origin' header is present on the requested resour

Springboot The requested profile "pom.xml" could not be activated because it doesn't n

git 使用 push 提交到远程仓库出现 The requested URL returned error: 403 错误

Docker和K8s实战——往docker hub推送镜像出现denied: requested access to the resource is denied

Xamarin.Android 使用 SQLite 出现 Index -1 requested, with a size of 10 异常