r/SpringBoot • u/OakAndCobble • Apr 28 '26
Question Question about dependency injection
How do I manually inject dependencies into RequestController classes? I just started learning spring and from my bit of research, all I've come up with is the Autowired and Component/Service annotations.
I am still having a hard time understanding how exactly I tell spring what to build. If the dependency of my controller needs dependencies injected into it, what do I do? How do I specify which implementation of a dependency I want built? And so on.
Essentially, how do I get a bit more control of dependency creation and injection in a non-trivial situation, like the ones seen in examples on the internet?
Thanks in advance for any responses.
View all comments
Show parent comment
4
u/OakAndCobble Apr 28 '26
Thanks for replying. I don't have a github project that I can share, sorry. I would like to do constructor injection, but it doesn't look that that is possible, at least in the way I envision it.
From my limited understanding, you never actually instantiate objects from your controllers. So where exactly would you be doing constructor injection?
Here is a bit of one of my controllers. Don't pay too much attention to the details. Given that this is the surface of the api, i.e. there is nothing above it, and an object is never instantiated from it, how would I pass any dependencies into it?
In my background in another language (not for web dev), from this point what I would typically do is have a root where I'd instantiate the controller and pass the use case in, but that's not how spring works I guess.