r/SpringBoot • • 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.

17 Upvotes

25 comments sorted by

View all comments

Show parent comment

3

u/Character-Grocery873 Apr 29 '26

There's this thing we called Reflection, you see you annotated your class with @RestController? Spring takes care of automatically instantiating that with it's dependencies and becomes a Bean(basically means an Object that is managed by Spring) , since your constructor asks for PriceOrderUseCase, spring will look for that bean and automatically inject that.

Edit: The injection will only work if that dependency is reachable or is already a bean. So make sure PriceOrderUseCase is a bean.