栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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");
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/337726.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号