O
O
Orkhan Hasanli2020-11-13 02:56:24
Java
Orkhan Hasanli, 2020-11-13 02:56:24

How to correctly create Spring Beans dynamically?

Good day!
There is a graphhopper library for building routes.
https://github.com/graphhopper/graphhopper/
You can create beans at the stage of raising the application context and pass the desired map path, which the library should subtract and prepare files for working with it (create a cache). In this case, there are no problems with creating a bean. It is enough to create a bean with a scope Singleton that will work with a pre-configured map.
What if you need different bins to work with different maps? If you use the scope ProtoType, then you can get several bean instances, but again, this will be at the stage of raising the application context. But the user should be able to download maps from the admin panel and, accordingly, you need to dynamically add bins to work with the map selected and downloaded by him.
Hence the question - is it necessary to create beans dynamically or are there other options for solving the issue? And if so, what exactly needs to be stored in the database and at what stage should the created beans be subtracted from the database?
Thanks for the replies!

PS How to pass String path (the path to the map) into the arguments of the method for adding a dynamic bean.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2021-06-14
@azerphoenix

With regard to this task, which I described above, it was not necessary to dynamically create the beans.
Instead, it was decided to write a microservice application in which each of the countries / cities for the Grapphopper is a separate microservice running inside the docker.
Thus, the need to create beans at runtime has disappeared.
If suddenly someone still needs to create beans dynamically, you can look at this solution:
https://stackoverflow.com/questions/57157396/how-t...

I
Igor Cherny, 2020-11-14
@freeg0r

If you use scope ProtoType, then you can get custom beans, but again, this will be at the stage of raising the application context

where did you get this crap from? A bean with prototype scope will return a
different instance every time it is requested from the container

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question