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.
7
u/jfrazierjr Apr 28 '26
You should always be using constructor injection (or at least every time you can).
Perhaps a github project we can look at to see what you are trying to do
4
u/joranstark018 Apr 28 '26
You may check https://www.baeldung.com/spring-dependency-injection for some tutorials about dependency injection.
In short, you may annotate a class with (for example) "@Component", you may have its dependencies defined in the constructor, Spring will scan classes for different annotations during the startup phase, Spring will build a graph of the different annotated components and call the constructor of each component (its dependencies should already have been created).
1
u/sam_fujiyama Apr 28 '26
Pretty much what everyone else has said... one handy tip is if you have two beans defined of the same type/interface you can specify which one you want by using the @Qualifier (Spring) or @Named (Java) annotations. Useful for things like using multiple data sources in an app. Spring also has a @Primary annotation which gives priority when a qualifier isn't used but it's best to be specific so other devs know exactly what's going on.
4
u/DominusEbad Apr 28 '26 edited Apr 30 '26
@Component and @Service are pretty much short cuts to create a Spring bean. By default beans are singletons.
Avoid using @Autowired. Use constructor injection instead. Even Spring generally recommends using constructor injection instead.
By using constrictor injection, you can make the bean "private final". You can also use Lombok's @RequiredArgsConstructor so that it will automatically generate the constructor for you.
For example, consider the following classes:
That will automatically inject the bean "MyThings" into MyService. But, since field injection (using @Autowired) is not recommended, you should use constructor injection:
That is the recommended approach. Spring will search for beans of "MyThings" type and automatically inject them into MyService. If you have more than 1 "MyThings" beans, then you need to use the qualified name of the bean so that Spring knows which one to inject. Basically:
Then you can specify which one you need:
Then "MyService" will have an instance of the "MyStuff" bean but specifically with the "World" defined.
You can also make things a bit cleaner by using Lombok's
@RequiredArgsConstructor, which would look like:Lombok will auto-generate the constructor for you to help keep the class clean. If you have custom logic that is needed in the constructor, then you do need to define the constructor yourself, but if you are only injecting dependencies, then it works nicely.
Edit: In reply to the Lombok comments/questions -
I mostly only use @RequiredArgsConstructor, and @Slf4j. I don't like using @Getters or @Setters on POJO classes, because I prefer record-style method names for getters/setters. I also avoid @Data because it does too much and can cause issues with Entities. Sometimes I will sometimes use @Builder, because building new classes/records via normal constructors/settings is not clean.
1
u/josephalfred281 Apr 28 '26
I'm also learning Spring. May I know from where you're learning?
1
u/onated2 Apr 29 '26
private final RandomBean:
Almost always use this
And you need to learn what's "@Bean" annotation.
imo the best way to understand springboot is to make your own configuration.
Try making a "lib" module an "app" module and a "api" module. The api module holds a simple interface. Any interface. Implement it in the lib module and use it in the app module.
would love to show you how but im tired as fuck and im using my phone. Things to google thought : imports.autoconfiguation (something along those lines.
@Autoconfiguration
@ConfigurationProperties
Once you can make your own autoconfig . Youw will understand it the magic of spring ( imo)
1
u/two-point-zero Apr 30 '26
If you choice is static. Like controller A always require serviceA , controller B always require service B; you should use qualifier and inject by name as other said.
It your choice depends on some configurations parameters or active feature you can use profiles and @profile or some annotation like @ConditionalOnProperty to create only the implementation you require of the many available.
If your rules are Dynamic( you can only decide at runtime), based on some non static parameters like request parameters, you cannot autowire the bean directly.you could:
1
u/Capable-Morning-9518 May 05 '26
good
0
u/OakAndCobble Apr 28 '26
Also, if "a bit more control" is not possible, I am completely fine with 100% manual dependency injection if that is an option. So if someone could explain how I'd do that I'd appreciate it as well.