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?(代码的主要内容,如果未能解决你的问题,请参考以下文章