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

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

问题描述:

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

    @Autowired
    private PasswordEncoder passwordEncoder;

这样为什么会出现bean已经在注入中的问题

Error creating bean with name 'passwordEncoder': Requested bean is currently in creation: Is there an unresolvable circular reference?

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

这种写法一般是没问题的,只是具体到你这里,就不清楚哪里出现循环依赖了