#The dependencies of some of the beans in the application context form a cycle Spring Boot

75 messages · Page 1 of 1 (latest)

ornate lance
#

Hello, I am using Springboot in a university project, and I am getting an error that says that some of the beans in the application are forming a cycle.
From what I see, the beans aren't forming a circular reference, as UserRepository doesn't reference any other bean, so I really don't understand what the problem happening is.
Thanks in advance for the help/your time.

sonic flareBOT
#

This post has been reserved for your question.

Hey @ornate lance! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

tough knot
#

Hey.
It seems like your SecurityConfig class references a UserRepository, and altought I don't see how your user references the security config, it says something about your repository implementation.
Can you post the code for these 2 classes, including the imports?

ornate lance
#

Of course! My SecurityConfig class and UserRepository were a part of the base project implementation so I don't really understand much of the SecurityConfig class but here is the code for both.

tough knot
#

Sorry for the delayed response...

#

If you do a search on your project for SecurityConfig, can you spot in which places it has been declared? And also, do you have any implementation for the UserRepository? If you do, what are its fields?

ornate lance
#

when searching for SecurityConfig I only found one match and its the class itself..
The UserRepository has three different implementations (one for each database type)

ornate lance
tough knot
#

Ah, I see some of those circular dependencies already

#

So, you have an interface for saving the users, which is the UserRepository. In it you have defined your methods to save and retrieve your users. But on your interface implementations, you are injecting the repository class into itself, and this is not allowed. Inside all these repositories, you don't need to have a field for your own repository class, you can remove those fields marked with @Autowired

#

I'm assuming that this User will come from a database somewhere, so now you need to provide a way to retrieve those from a database. In your project, do you have any other repositories which you can check, to see what they are using to interact with the database?

ornate lance
#

The @Autowired repositories are the interfaces that extend the specific Spring Data interfaces such as MongoRepository, JpaRepository and Neo4jRepository. The implementation that I did is so that I can use autowired and on start up only one of those repositories will be used depending on user interface

tough knot
#

I saw the interfaces that you mentioned, but they didn't seem to come from Spring Data. On your UserNeo4JRepositoryImpl, there is an import as you said that comes from a package from your application. How does the UserNeo4JRepository interface looks like?

ornate lance
#

this is the UserH2Repository interface that extends the JpaRepository. For the other repos, the logic is the same, extending the appropriate repository for each of the databases

#

this is so that the Strategy pattern can be employed an use only a single type of database and repository at a time on startup

tough knot
#

Ok, there are a few points here worth noticing then.
If you look closer to this class, you'll see as you said, that it extends a JpaRepository, which does indeed comes from the Spring Data JPA. In this case, you don't need to create an implementation for this class, since Spring Data JPA will take care of this for you.

#

Ok, but you don't have an implementation on this, I saw, sorry about that

ornate lance
#

yeah I'm not implementing it, the one thing I am in doubt if it might be causing some troubles is the @Repository and @Profile annotations that I have both on the interface for UserH2Repository and on the implementation that autowires the repository. Would that create some sort of conflict?

tough knot
#

I think it should in case you have the same class, or classes that implements the same interface, which doesn't seem to be the case here

#

What do you want to achieve with a single type of database? Isn't it against the purpose of having a mongo, a h2 and a neo4j reposiitories? Those interfaces, do they all extend from the same - like this last one you sent, which is JpaRepository<H2User, Long>?

ornate lance
#

No, h2 extends from JpaRepository<H2User,Long>, mongo extends from MongoRepository<MongoUser,Long> and neo4j from Neo4jRepository<Neo4jUser,Long>.
The thing is that only one of them will be running at a time, its like a sort of problem where you release a project to a public and some people would prefer a document type database, others a graph and others relational, and so the type of database running could be configured at startup, making it so that only one runs at a time

tough knot
#

Yeah, and the profiles will help you with instantiating one class or the other depending on which you you set as active

ornate lance
#

yeah thats it. I currently have all that properly defined, I think

#

If you want I can show the project in a video call if that would make it easier for you to help

tough knot
#

It would make it easier but unfortunately I cannot make the call now, sorry

ornate lance
#

its ok, no problem

tough knot
#

I don't think I'll be of any help for now. I'll need to experiment on it a bit further

#

During the weekend I'll try and setup a similar project and see how it goes, so I'll come back here, if no one had answered you already by then 😄

sonic flareBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

wicked quiver
#

Do you still get the same error?

#

If so, can you show UserRepositoryImpl?

#

as well as the UserRepository interface

ornate lance
#

here is the interface

#

and here is the impl

#

there is also the interface that UserH2RepositoryImpl implements which is just he methods needed of the repo

wicked quiver
#

Are you sure there is no type called UserRepositoryImpl?

#

oh I misread that

ornate lance
#

also since I am using profile to use only one database at a time, when I change to mongo for example, the same error occurs but for the mongo repositories.

wicked quiver
#

Can you try commenting out the

@Autowired
UserH2Repository uRepo
``` temporarily and change the methods to do nothing?
#

Just to see whether the application would start

wicked quiver
ornate lance
#

Its the same, but it changes from the H2Repository to the MongoRepository

#

when commenting for the user repo, I got error from other repos,

wicked quiver
#

can you show it?

ornate lance
wicked quiver
#

Can you show UserH2Repository?

wicked quiver
ornate lance
wicked quiver
#

which aren't geberated by Spring

ornate lance
#

nope, there is no class implementing this repository. It is passed by @Autowired

wicked quiver
# ornate lance

Is there any reason you use S extends H2User here? Just use H2User save(H2User entity)

ornate lance
wicked quiver
#

I see

#

yeah then you can't do that

#

Do you get a different error without the caching annotations?

ornate lance
#

still the same

wicked quiver
#

circular dependency between securityConfig and UserH2RepositoryImpl?

ornate lance
#

wait I have na idea of what might be the problem

#

lemme check

wicked quiver
#

Also try commenting out the @CacheConfig("users") from UserH2RepositoryImpl if your idea doesn't work

ornate lance
#

nope, still getting the same errro

#

When running in eclipse this is the error talking about circular references, in case it helps

wicked quiver
#

Can you show the full error?

ornate lance
ornate lance
#

If I set the application to allow circular dependencies this problem is solved, however new ones come out. From what I found I think I need to change the UserH2RepositoryImpl class to remove @Autowired from uRepo. When doing that I no longer get those errors, however new ones pop out saying:
"A component required a bean named 'entityManagerFactory' that could not be found."

sonic flareBOT
sonic flareBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.