Saurin
Saurin

Reputation: 81

Getting this error in spring boot: The dependencies of some of the beans in the application context form a cycle:

I'm new to spring. In attempt to learn it I was following an example of Josh Long to create an OAuth server. The difference between his example and the code that I'm working on is that I'm trying to use a MySQL rather than in memory database. I'll leave relevant files in the post.

Thanks in advance.

Oh, and here's the link to the video https://www.youtube.com/watch?v=EoK5a99Bmjc

Here's my class that implements Repository

@Service
public class AccountRepositoryImpl implements UserDetailsService {

    @Autowired
    private final AccountRepository accountRepository;

    public AccountRepositoryImpl(AccountRepository accountRepository){
        this.accountRepository = accountRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return accountRepository.findByUsername(username)
                .map(account -> new User(account.getUsername(),
                        account.getPassword(), account.isActive(), account.isActive(), account.isActive(), account.isActive(),
                        AuthorityUtils.createAuthorityList("ROLE_ADMIN", "ROLE_USER")
                        ))
                .orElseThrow(() ->new UsernameNotFoundException ("Couldn't find the username " + username + "!"));  
    }
}

Here's the Repository

public interface AccountRepository extends JpaRepository<Account, Long> {
    Optional<Account> findByUsername(String username);
}

Upvotes: 0

Views: 13105

Answers (2)

hadi.mansouri
hadi.mansouri

Reputation: 828

What is your implementation of AccountRepository interface?

If your are not provide implementation for AccountRepository spring could not initialize it.

Your fist Exception (dependencies form cycle) may be occurred if you autowired AccountRepositoryImpl in your implementation of AccountRepository.

What is your log content when Exception (The dependencies of some of the beans in the application context form a cycle) is occurred ?

Upvotes: 0

Solace
Solace

Reputation: 2189

These two parts do the exact same thing:

@Autowired
private final AccountRepository accountRepository;

and

public AccountRepositoryImpl(AccountRepository accountRepository){
    this.accountRepository = accountRepository;
}

When you added the @Autowired annotation above accountRepository, Spring automatically injects an instance of AccountRepository into your AccountRepositoryImpl class. So you need to remove the 2nd selection because it most likely conflicts with the @Autowired annotation.

EDIT -----------------------------

@Component 
public class ApplicationStartUp implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private AccountRepository accountRepository;

    @SuppressWarnings("null") 
    @Override 
    public void onApplicationEvent(final ApplicationReadyEvent event) { 
        Account account = new Account("sPatel", "Spring", true); 

        accountRepository.save(account); 
        return; 
    }

}

Upvotes: 1

Related Questions