java – How can I not have to search for a user every time in Spring?


In general, every time I access a controller in Spring, I have to look for a user, create an object, and already work with it. Those. let's say I go to /profile, I pull out the name of the authorized user, I look for it in the Base, if everything is fine, then I work with it. I click on /index, then again I have to look for the user in the database, create an object and again build a page for it. How to avoid searching and creating a new object?


You can try to cache users, but this will affect resources. You can also store them in the session, but this will reduce the security of the application.

And in fact, this is normal. And if I were you, I would start thinking about this issue if only it really had a big impact on the speed of your application. I think this is called premature optimization.

By the way, I thought that you might want to escape not from constant requests to the database, but from regular repeating code? in this case, you can already think about how to solve it from the side of the organization of the code itself.

Scroll to Top