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.
#The dependencies of some of the beans in the application context form a cycle Spring Boot
75 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @ornate lance! Please use
/closeor theClose Postbutton 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.
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?
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.
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?
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)
Also no problem with the delay, you are helping and thats enough 🙂
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?
Since you are using Spring, check if you have any occurences on your project of a JdbcTemplate. Here's some example configuration and usage: https://www.baeldung.com/spring-jdbc-jdbctemplate
Another way this can be done, is by using specific interfaces provided by Spring Data JPA that already has the implementations of those methods for you, so you just need to use it's interfaces instead. Again here is the reference for that: https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa
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
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?
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
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
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?
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>?
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
Yeah, and the profiles will help you with instantiating one class or the other depending on which you you set as active
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
It would make it easier but unfortunately I cannot make the call now, sorry
its ok, no problem
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 😄
💤 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.
Do you still get the same error?
If so, can you show UserRepositoryImpl?
as well as the UserRepository interface
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
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.
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
oh, interesting
and you get exactly the error here?
Its the same, but it changes from the H2Repository to the MongoRepository
when commenting for the user repo, I got error from other repos,
hm?
can you show it?
Can you show UserH2Repository?
and maybe also the types here
Do you have any implementations of that?
which aren't geberated by Spring
nope, there is no class implementing this repository. It is passed by @Autowired
btw you can ubdo that
Is there any reason you use S extends H2User here? Just use H2User save(H2User entity)
it shows a warning when I do that
I see
yeah then you can't do that
Do you get a different error without the caching annotations?
still the same
circular dependency between securityConfig and UserH2RepositoryImpl?
Also try commenting out the @CacheConfig("users") from UserH2RepositoryImpl if your idea doesn't work
nope, still getting the same errro
When running in eclipse this is the error talking about circular references, in case it helps
Can you show the full error?
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."
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
💤 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.