title: Trouble with "The dependencies of some of the beans in the application context form a cycle"
body: <p>I'm working on the course &quot;Information System&quot; and follow the operation of the prof, but it turns a strange error:</p>
  <p><a href="https://i.stack.imgur.com/LQQQ9.png" rel="nofollow noreferrer">The dependencies of some of the beans in the application context form a cycle:
  </a></p>
  <p>here's my classes:</p>
  <pre><code>@Service
  public class UsersService implements UserDetailsService {
  
  @Autowired
  private  UsersRepository _usersDb;
  
  @Autowired
  private  BCryptPasswordEncoder _passwordEncoder;
  public boolean registerNewUser(String username, String password, Role role) {
      Optional&lt;User&gt; oldUser = _usersDb.findByUsername(username);
      if(oldUser.isPresent()) {
          return false;
      }
      
      
      String encodedPassword = _passwordEncoder.encode(password);
      User newUser = new User(null, encodedPassword, username, role);
      
      _usersDb.save(newUser);
      return true;
  }
  
  
  public List&lt;User&gt; getAllUsers() {
      return _usersDb.findAll();
  }
  
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
      Optional&lt;User&gt; user = _usersDb.findByUsername(username);
      
      if(!user.isPresent()) {
          throw new UsernameNotFoundException(&quot;User not found&quot;);
      }
      
      return user.get();
  
  
  }
  
  public boolean isAdminNotExists(String adminUsername) {
      return _usersDb.findAll((r, q, b) -&gt; b.or(
              b.equal(r.get(&quot;role&quot;), Role.ADMIN),
              b.equal(r.get(&quot;username&quot;), adminUsername)
      )).isEmpty();
  }
  </code></pre>
  <p>then:</p>
  <pre><code>@Configuration
  @EnableWebSecurity
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
  
  
  @Autowired
  private  UsersService _usersService;
  
  
  @Override
  protected void configure(HttpSecurity httpSec) throws Exception {
      httpSec.csrf()
              .disable()
              .authorizeRequests()
                  .antMatchers(&quot;/registration&quot;).not().fullyAuthenticated()
                  .antMatchers(&quot;/admin/**&quot;).hasRole(&quot;ADMIN&quot;)
                  .antMatchers(&quot;/dishes/**&quot;).hasAnyRole(&quot;USER&quot;, &quot;MANAGER&quot;)
                  .antMatchers(&quot;/&quot;, &quot;/resources/**&quot;).permitAll()
              .anyRequest().authenticated()
              .and()
                  .formLogin()
                  .loginPage(&quot;/login&quot;)
                  .defaultSuccessUrl(&quot;/&quot;)
                  .permitAll()
              .and()
                  .logout()
                  .permitAll()
                  .logoutSuccessUrl(&quot;/&quot;);
      
  }
  
  protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      auth.userDetailsService(_usersService).passwordEncoder(bCryptPasswordEncoder());
  }
  
  @Bean
  public BCryptPasswordEncoder bCryptPasswordEncoder() {
      return new BCryptPasswordEncoder();
  }
  </code></pre>
  <p>}</p>
  <p>and this:</p>
  <pre><code>@Component
  public class IsProjectAppDataInitializier implements ApplicationListener&lt;ContextRefreshedEvent&gt;{
      
  private boolean _alreadyInitialized = false;
  
  
  private final  UsersService _usersSvc;
  
  public IsProjectAppDataInitializier(UsersService usersSvc) {
      _usersSvc = usersSvc;
  }
  
  @Override
  public void onApplicationEvent(ContextRefreshedEvent event) {
      
      if(!_alreadyInitialized) {
          _alreadyInitialized = true;
          this.initializeApp();
      }
      
  }
  
  private void initializeApp() {
      if (_usersSvc.isAdminNotExists(&quot;admin&quot;)) {
          _usersSvc.registerNewUser(&quot;admin&quot;, &quot;password&quot;, Role.ADMIN);
      }
      
  }
  </code></pre>
  <p>@Autowired works in the video of prof, but not works on mine....
  Really need guys your help!</p>